I have a simple dotnet core class library with a single XUnit test method:
TestLib.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.SDK" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit.runners" Version="2.0.0" />
</ItemGroup>
</Project>
BasicTest.cs:
using Xunit;
namespace TestLib
{
public class BasicTest
{
[Fact(DisplayName = "Basic unit test")]
[Trait("Category", "unit")]
public void TestStringHelper()
{
var sut = "sut";
var verify = "sut";
Assert.Equal(sut, verify);
}
}
}
If I enter the project on the CLI and type dotnet build
the project builds. If I type dotnet test
I get this:
C:\git\Testing\TestLib> dotnet test
C:\git\Testing\TestLib\TestLib.csproj : warning NU1701: Package 'xunit.runner.visualstudio 2.4.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.
Build started, please wait...
C:\git\Testing\TestLib\TestLib.csproj : warning NU1701: Package 'xunit.runner.visualstudio 2.4.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.
Build completed.
Test run for C:\git\Testing\TestLib\bin\Debug\netstandard2.0\TestLib.dll(.NETStandard,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 16.0.0-preview-20181205-02
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
Unable to find C:\git\Testing\TestLib\bin\Debug\netstandard2.0\testhost.dll. Please publish your test project and retry.
Test Run Aborted.
What do I need to change to get the test to run?
If it helps, VS Code is not displaying the tests in its test explorer, either.
In my case the problem was that I have an extension project for xunit. There is also a test project to test the extensions. When I ran
dotnet test
on my solution, my extension project was also picked up as a unit test project (it took me some time to realize this). The reason for this is that it references some xunit packages. One of these xunit packages automatically sets the<IsTestProject>true</IsTestProject>
property in you csprj file. This is actually a good thing since 99.99% of the projects that reference xunit are actually unit tests. I could finally solve this by explicitly settingManually in my csproj file. Then the problem went away.
In my case, the problem was that I was targeting .NET Core 2.0 and switching to .NET Core 2.1 solved the problem. However I was using Microsoft.NET.Test.SDK v16.4.0 instead of 15.9.0.
Ran into this error, the root cause was the tests were hitting the maximum length for a Windows path (MAX_PATH), which is defined as 260 characters.
same issue i faced for Nunit (.net core 3.1) project . I was using Microsoft.NET.Test.SDK v16.6.1, I downgraded the version to 15.9.0. And it start working
If you are targeting netstandard2.0 this will not work. If are using .NET Core. make sure the .csproj contains the following lines:
<TargetFramework>netcoreapp3.0</TargetFramework>
and also contains the package
Microsoft.NET.Test.Sdk
Installing
Microsoft.NET.Test.Sdk
package from nuget package manager solved my issue.