Reputation: 12423
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.
Upvotes: 248
Views: 112074
Reputation: 61
After trying all the items listed here, I found that my base class was:
class GenerateNewToken
and it should have been
public class GenerateNewToken
Upvotes: 0
Reputation: 151
In my case the issue was somehow related to the installed packages. The thing is that I switched between different branches locally and some have some different packages versions.
I had to clean nuget cache and use dotnet restore to get everything back in order.
Upvotes: 0
Reputation: 1
I added this code in csproj test file and worked
> <PackageReference Include="coverlet.collector" Version="3.0.2" />
> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
> <IncludeAssets>runtime; build; native; contentfiles; analyzers;
> buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets>
> </PackageReference>
Upvotes: 0
Reputation: 899
Thanks! solved the issue of my testing project targeting netcore3.1
projects installed:
Upvotes: -1
Reputation: 6251
As of today 18.05.2023 with VS 2022 you have to install the following in addition to xunit because otherwise VS will not be able to detect the tests.
Upvotes: 11
Reputation: 2437
I was receiving the error due to multiple dlls being picked up by the filter instead of just the one dll which contained the tests.
Upvotes: 0
Reputation: 127
Restart Visual Studio and do a rebuild. Visual Studio seems to get confused sometimes after switching branches.
Upvotes: 0
Reputation: 31
In my case Microsoft.NET.Test.Sdk was already installed, I had to reinstall it on Nuget Package Manager and the issue got fixed.
Upvotes: 0
Reputation: 9
I downloaded .Net.Test.SDK and .Testplatform.TestHost via Nuget to my project but test kept not working. xunit.runner.visualstudio did the trick.
Upvotes: -2
Reputation: 358
I was using .NET Core Framework 6.0 I had to downgrade Microsoft.NET.Test.Sdk
from Version 17.1.0 to 16.5.0 to resolve the issue.
Upvotes: 0
Reputation: 5173
In my case it was a silly mistake. I was referencing xunit
also in my source project (not only in my test project)
Deleting the xunit
dependency in my source project fixed the problem.
Upvotes: 1
Reputation: 20852
I found 5 factors to be crucial.
(3 of whom were spread throughout the other answers here in mixed variations.)
1 to 4:
Add these Nuget Packages in versions that work for your .NET Core version:
5:
Now make sure that Microsoft.NET.Test.Sdk is set to NOT ExcludeAssets. To do so use one of these methods:
ExcludeAssets="All"
from the Sdk package entry, if present:<!-- Bad: -->
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" ExcludeAssets="All" />
<!-- Good: -->
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
Upvotes: 9
Reputation: 2256
I found a very interesting compatibility issue with a version. I did upgrade just as a normal practice, my code, and I switched to xUnit.runner.visualstudio 2.4.2. It stopped working for .Net Core 3.1. I had to downgrade to 2.4.1 and it started working again.
Additional information after one of my comments.
The package xunit.runner.visualstudio versions <= 2.4.1 includes a reference to Microsoft.NET.Test.Sdk. Later versions don't, so you need to add the reference to your project.
See https://stackoverflow.com/a/63786758/3248302
Upvotes: 21
Reputation: 620
This error can occur if you upgrade xunit.runner.visualstudio
to a version greater than 2.4.1. Versions up to and including 2.4.1 include a reference to Microsoft.NET.Test.Sdk
but later ones don't, so you need to include the reference in your own project.
Interestingly, I found that NCrunch still ran my tests without the additional reference, even though I couldn't run them via the CLI.
Upvotes: 6
Reputation: 71
In my case, it was necessary to include a reference to MsTest.TestAdapter module using nuget.
A fresh project with MSTest.TestFramework and Microsoft.Net.Test.Sdk was not enough to run a single unit test.
Noticing in my case I was using a test project targeting .NET framework 4.8 and not .NET core. Although, I strongly believe this fix might apply to that platform as well
Upvotes: 0
Reputation: 91
Had to add Microsoft.TestPlatform.TestHost
to get testhost.dll
.
I found that in this answer https://github.com/dotnet/sdk/issues/7171#issuecomment-261506546
Upvotes: 9
Reputation: 109
Got this error, when trying to debug a unit test. Below are the steps I tried.
Yay!!! it worked :-) Never miss to try the last step ;-) Hope this helps someone like me.
Upvotes: 1
Reputation: 612
This could also be caused by inadvertently trying to run a non-test project, this usually happens when your test files filter is too wide.
Upvotes: 1
Reputation: 357
I've encountered this a couple of times and I always forget what's up. Most recently I had:
Packages for my test project:
I was seeing:
Unable to find C:\PATH\bin\Debug\netstandard2.0\testhost.dll. Please publish your test project and retry.
And all that I needed to do was add to my test project the missing nuget package: "Microsoft.NET.Test.SDK"
Everything was back to normal at this point.
Upvotes: 16
Reputation: 61
If you are running a project by cloning than Solution is to install the Microsoft.NET.Test.Sdk. How to : Tools>Nuget Package Manager>Manage Nuget Packages For Solution...>Search for Microsoft.NET.Test.Sdk and install for your test project.
Upvotes: 0
Reputation: 21
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
Upvotes: 2
Reputation: 562
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
Upvotes: 6
Reputation: 1672
I was building a netcoreapp2.2 test project and then trying to run dotnet vstest
from the bin folder. I noticed that the Microsoft Test DLLs from:
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
were not being output to my bin folder. Instead of just building, I ran a publish instead which did include the necessary DLLs in the output folder and I was then able to run dotnet vstest
from there.
Upvotes: 7
Reputation: 1363
If you are using xUnit, make sure your project type is not as netstanderd. As xUnit doesn't support netstanderd, change it to coreapp2.0 or others.
Upvotes: 14
Reputation: 6465
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.
Upvotes: 41
Reputation: 401
This happened to me after updating Microsoft.NET.Test.Sdk from v16.2.0 to v16.4.0 with <TargetFramework>netcoreapp2.0</TargetFramework>
. Updating to <TargetFramework>netcoreapp3.0</TargetFramework>
resolved the issue for me.
Upvotes: 12
Reputation: 2266
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.
Upvotes: 1
Reputation: 1759
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 setting
<PropertyGroup>
...
<IsTestProject>false</IsTestProject>
...
</PropertyGroup>
Manually in my csproj file. Then the problem went away.
Upvotes: 33
Reputation: 17382
Installing Microsoft.NET.Test.Sdk
package from nuget package manager solved my issue.
Upvotes: 427