如何修复:NUnit 单元测试未显示在 Visual Studio 社区 (Mac) 的测试板中 [英] How to fix: NUnit unit tests not showing up in testing pad in Visual Studio Community (Mac)
问题描述
尽管将 NuGet 中的 NUnit 添加到现有 .Net Core 项目中,但测试板中未显示任何单元测试.
In spite of adding NUnit from NuGet to an existing .Net Core project, no unit tests are being shown in the Test Pad.
注意:我将这些图片作为链接发布,因为我发布图片的声誉太低.这是怎么回事?
Note: I posted these images as links because I have too low of a reputation to post images. What's up with that?
- 项目 > 添加 NuGet 包...
- 选择 NUnit 包 (3.11.0) 并点击添加包"
- 检查是否已添加到解决方案中
- 在解决方案中创建了一个新的空类文件
- 向此类添加了测试
- 测试板中未显示任何测试
我已尝试重新启动 Visual Studio 并重新安装软件包.
I've tried restarting Visual Studio and reinstalling the package.
我也尝试删除 Project/obj 目录 - 仍然没有运气.
I've also tried deleting the Project/obj directory -- still no luck.
using NUnit.Framework;
namespace ExampleLib
{
[TestFixture]
public class ExampleTestFixture
{
[Test]
public void ExampleTest()
{
Assert.AreEqual(2, 2);
}
}
}
预期:测试填充单元测试板实际:空的测试垫.
Expected: Tests fill the Unit Test pad Actual: Empty test pad.
推荐答案
那个项目有多个需求,所以 VS for Mac 可以识别和执行测试用例.
There are multiple requirements of that project, so that VS for Mac can identify and execute the test cases.
- 该项目必须是 .NET Core 的控制台应用程序.
- 该项目必须具有
Microsoft.NET.Test.Sdk
的引用(VSTest 基础架构需要). - 项目必须有
NUnit
的引用. - 该项目必须具有
NUnit3TestAdapter
的引用(VSTest 基础结构需要).
- The project must be a console application of .NET Core.
- The project must have a reference of
Microsoft.NET.Test.Sdk
(required by VSTest infrastructure). - The project must have a reference of
NUnit
. - The project must have a reference of
NUnit3TestAdapter
(required by VSTest infrastructure).
示例,
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>
</Project>
请注意,VS for Windows 和 VS for Mac(和 JetBrains Rider)都使用 VSTest 进行单元测试,因此此设置适用于所有此类 IDE.
Note that VS for Windows and VS for Mac (and JetBrains Rider) all use VSTest for unit testing, so this setup works for all such IDEs.
还要注意,如果你不是从dotnet new nunit
创建这个控制台项目,而是一个普通的控制台应用模板,你需要手动删除Main
方法.
Also note that if you didn't create this console project from dotnet new nunit
, but a normal console application template, you need to manually delete the Main
method.
这篇关于如何修复:NUnit 单元测试未显示在 Visual Studio 社区 (Mac) 的测试板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!