简体   繁体   English

.NET在Visual Studio中如何识别一个项目用的是什么单元测试框架?

[英]How to recognize what unit testing framework is used in a project in Visual Studio, .NET?

I am working with a Visual Studio C# project and I need to know what unit testing framework is it using, but there is no one to ask about it.我正在处理一个 Visual Studio C# 项目,我需要知道它使用的是什么单元测试框架,但没有人问。

How could I check it?我怎么检查呢?

The most reliable thing here would be to look at the project references that the test project has (either in the IDE by expanding "References" in the solution explorer, or by looking at the csproj directly).这里最可靠的方法是查看测试项目拥有的项目引用(在 IDE 中通过在解决方案资源管理器中展开“引用”,或直接查看 csproj)。 You will probably find something like xunit, nunit, mstest, or something else indicative of what you're using.您可能会发现诸如 xunit、nunit、mstest 之类的东西,或者其他表明您正在使用的东西。

Note that this will often be in addition to Microsoft.NET.Test.Sdk , which is related to just making it testable;请注意,这通常是Microsoft.NET.Test.Sdk补充,这与使其可测试有关; for example, I have an xunit project that includes:例如,我有一个 xunit 项目,其中包括:

    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />

The bottom two are for integration purposes;底部两个用于集成目的; the top one is the actual test framework reference.最上面的一个是实际的测试框架参考。

There are two different things to pay attention to.有两个不同的事情需要注意。

First is the testing framework used in the code.首先是代码中使用的测试框架。 Most common are nUnit, xUnit and MSTest.最常见的是 nUnit、xUnit 和 MSTest。 This is easily checked by looking at references of the project, or installed NuGet packages.这可以通过查看项目的引用或已安装的 NuGet 包轻松检查。

But, Visual Studio needs to be able to discover and run those tests.但是,Visual Studio 需要能够发现并运行这些测试。 For that, each testing framework usually has an adapter that is installed either as visual studio plugin (usually older version) or as a reference or a NuGet package.为此,每个测试框架通常都有一个适配器,可以作为 Visual Studio 插件(通常是旧版本)或作为参考或 NuGet 包安装。 For nUnit, that would be NUnit3TestAdapter or for xUnit Xunit.Runner.VisualStudio .对于 nUnit,这将是NUnit3TestAdapter或 xUnit Xunit.Runner.VisualStudio

If you don't care about spending some seconds you should use the official way of determining if a csproj file contains tests:如果您不关心花几秒钟,您应该使用官方方法来确定 csproj 文件是否包含测试:

dotnet test --list-tests <project.csproj>

Related documentationhttps://learn.microsoft.com/en-us/do.net/core/tools/do.net-test相关文档https://learn.microsoft.com/en-us/do.net/core/tools/do.net-test

This will list all the tests in that project without executing them.这将列出该项目中的所有测试而不执行它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于单元测试 .net 标准库的框架/视觉工作室项目类型? - Framework/visual studio project type to use for unit testing .net standard libray? 如何使用Visual Studio单元测试框架或NUnit框架在C#中模拟对象? - How to mock objects in C# using Visual Studio Unit Testing framework or NUnit framework? Visual Studio 2019 中的单元测试项目模板仅适用于 .NET Framework 是否重要? - Does it matter that Unit Test project template in Visual Studio 2019 is for .NET Framework only? 如何将 .Net 框架添加到 Visual Studio preview 2019 项目 - How to add .Net framework to Visual Studio preview 2019 project 使用Visual Studio单元测试框架比较两个对象 - Comparing two objects with Visual Studio Unit Testing Framework Visual Studio 安装程序项目 .NET 框架 - Visual Studio Installer Project .NET Framework 无法在Visual Studio 2012中调试单元测试项目 - Cannot debug a unit testing project in Visual Studio 2012 我可以使用Visual Studio单元测试框架强制对属性进行单元测试吗? - Can I force unit testing of properties using the Visual Studio unit testing framework? C#中的单元测试按钮-.NET Visual Studio - Unit Testing Buttons in C# - .NET Visual Studio 如何在视觉工作室之外对项目进行单元测试 - How to unit test a project outside visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM