dotnet test does not recognize xunit tests

4.2k Views Asked by At

I know that this question has been asked multiple times before I raise it again, However I still could not get the answer for dotnet 5.0 and xunit

What have I tried.

  • I have the following test defined
public class IntegrationTests
{
    [Theory]
    [MemberData(nameof(Tests), MemberType = typeof(IntegrationTests))]
    public void Test(IntegrationTest test)
    {
       Assert.True(test.Expected, test.Actual);
    }
}
  • Visual Studio 2019 recognizes all the tests and runs them without any issues
  • dotnet test command says
dotnet test <path to>.csproj

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
No test is available in C:\<path>\bin\Debug\net5.0\<projectname>.dll. 
Make sure that test discoverer & executors are registered and platform & framework 
version settings are appropriate and try again.

I don't fully undertand what "test discoverer and executors mean here.

My .csproj file has following nuget packages (since many of the similar questions got resolved by adding one of these)

    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.console" Version="2.4.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
2

There are 2 best solutions below

0
On BEST ANSWER

I was working on a windows machine and I did the following to make it work

  1. Closed all my vscode / visual studio instances
  2. Deleted C:\Users\<username>\.nuget\packages folder
  3. Deleted references for testrunner packages in <projectname>.csproj

and it started picking up the tests again

0
On

Step 1 - Remove all references in the .csproj file

Step 2 - Run the command: dotnet restore

Step 3 - Run dotnet build

Step 4 - Add references again

Step 5 - Run dotnet restore

Step 6 - Run the dotnet test

Step 7 - have fun

Note: Use Control + X(Cut) to remove the references so you can paste them back in step 4. =)