How to fix: NUnit unit tests not showing up in testing pad in Visual Studio Community (Mac)

纵饮孤独 提交于 2021-01-20 08:50:45

问题


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?

  1. Project > Add NuGet Packages...
  2. Selected NUnit Package (3.11.0) and clicked "Add Package"
  3. Checked to see if added to solution
  4. Created a new empty class file within the solution
  5. Added tests to this class
  6. No tests show up in the test pad

I've tried restarting Visual Studio and reinstalling the package.

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.


回答1:


There are multiple requirements of that project, so that VS for Mac can identify and execute the test cases.

  1. The project must be a console application of .NET Core.
  2. The project must have a reference of Microsoft.NET.Test.Sdk (required by VSTest infrastructure).
  3. The project must have a reference of NUnit.
  4. The project must have a reference of NUnit3TestAdapter (required by VSTest infrastructure).

Example,

<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>

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.

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.




回答2:


The NuGet package only allows you to use the NUnit framework to write tests. In other words, it's like adding references to the NUnit DLLs.

You need to install the adapter to see the tests.




回答3:


As lex-li comments, you should install the nuget version of the adapter (https://www.nuget.org/packages/NUnit3TestAdapter/3.12.0).
You install this into your solution, preferably for all test projects.

(The adapter on the marketplace is the vsix adapter, which can be installed into VS itself - but this practice is not recommended and as you points out, doesn't even seem to work for Mac Community version.)



来源:https://stackoverflow.com/questions/54564654/how-to-fix-nunit-unit-tests-not-showing-up-in-testing-pad-in-visual-studio-comm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!