ChatGPT解决这个技术问题 Extra ChatGPT

Unit Tests not discovered in Visual Studio 2017

I have been struggling with VS 2017 since I installed it. Now it seems Unit Tests will only run from the command line "dotnet test."

My project is .NET Core 1.1.1. I have the SDK and the framework update for 1.1.1 installed.

I have tried the sample at MSDN (https://msdn.microsoft.com/en-us/library/ms182532.aspx) which also fails exactly the same way.

All NuGet packages for the tests and the main project are current. And both the test project and the main project build without errors. An the tests run successfully from the command line.

Has anyone gotten Unit Tests to run in VS 2017, if so how?

Thanks, John

Update - Extend

Here is an example of a simple test project that is not working on GitHub. This is an example with xUnit but I have tried NUnit and visual studio built in MS tests. No matter what testing or what changes I make I cannot get the VS test runner to find any tests.

What I've Tried

Deleting VS test cache files DEL %TEMP%\VisualStudioTestExplorerExtensions

Restarting VS

Closing/Opening test explorer

for xUnit installed Microsoft.DotNet.InternalAbstractions (see SO post)

for NUnit ensure adapter installed and same version (3) as NUnit package

test -> test settings -> default processor architecture is set to x86

The Question Can anyone please provide a working example of a .Net Core 1.1.0 solution in VS2017 (.csproj project files) where the VS test explorer successfully finds the unit tests OR show me the issue in the example given.

I found out that VS2017 does not install all required packages. When I tried to move my MonoGame from old PC to new one with freshly installed windows 10 and VS 2017 it started throwing weird errors about missing packages. After installing VS2015 alongside with VS2017 all problems were gone. Maybe try to install VS2015 additionaly.
Try to install Tests packages with Visual Studio installer
I'm looking into if VS 2017 has all the environment variables correctly set.
For NUnit, you must use the NuGet package for the adapter and it must be 3.8.0-alpha1 or newer.
In my case it was the mere presence of an app.config file in my test project: stackoverflow.com/a/47497668/67824.

Q
Quality Catalyst

In my case, it turned out that I simply had to upgrade my test adapters and test framework. Done.

Example using the NuGet Package Manager:

https://i.stack.imgur.com/zbYwq.png


This helped me too! Note that you can "Manage Nuget Packages" at a solution level and do this for all the projects where this is required. You might then get "ambigous reference" errors - for these, just remove the old DLL (Microsoft.VisualStudio.QualityTools.UnitTestFramework) from references
These things should be extensions of Visual Studio, not NuGet packages.
We have a lot of old MSTest projects, and I wasn't aware it moved to a NuGet package. This solved it for me as well, originally thought it was a bug with the newer releases of ReSharper until I realized the VS Test Explorer also could not discover my tests.
I did the exact same thing as this answer states. In my VS2017 solution, I added an MSTest project, added a few tests, but building the solution would result in: Discover test finished: 0 found. So, for the test project, in NuGet Package Manager (you can also do at solution level), I updated the MSTest.TestAdapter and MSTest.TestFramework both from v1.1.18 to v1.2.0. Then, after doing a build, my tests now appear in Test Explorer.
Worked great for me I had to go into the nuget package manager in VS2017 for the particular test project and simply updated the various packages I had like nunit etc then build > rebuild and all okay.
P
PmanAce

This just worked for me (don't know if it is the result of changing workspaces that corrupted something):

Deleting VS test cache files in %TEMP%\VisualStudioTestExplorerExtensions and restart VS2017.


This worked once, not afterwards. What (this time) fixed it for me was deleting TestResults folder and bin/obj (along with this temp directory cleanup)
Anyone that wonders where %TEMP% - bring command window and type echo %TEMP%
Folder doesn't exist in temp :/
so what is the cause of such behavior?
Easiest way to access %TEMP% is to win+R and type %TEMP%
P
Pang

The API for test adapters for .NET Core changed with the release of Visual Studio 2017 and the move from the project.json format to the csproj format. This made the existing dotnet-test-* adapters like dotnet-test-nunit obsolete.

The adapters have been updated, but the way you set up and run tests in Visual Studio or on the command line with dotnet test requires different references in your test projects. Beware of any documentation you find that reference packages in the dotnet-test-* format because they are outdated.

First, your test project must target a specific platform, either .NET Core or .NET Framework. It cannot target .NET Standard even if the code you are testing is .NET Standard. This is because the target of the tests indicates which platform to run the tests under. .NET Standard is like a PCL (Portable Class Library) in that it can run on many platforms.

Next, you need to add references to Microsoft.NET.Test.Sdk, your test framework of choice and a compatible test adapter. For NUnit, your references will look like this,

<itemgroup>
    <packagereference Include="Microsoft.NET.Test.Sdk" Version="15.0.0"></packagereference>
    <packagereference Include="NUnit" Version="3.7.1"></packagereference>
    <packagereference Include="NUnit3TestAdapter" Version="3.8.0"></packagereference>
</itemgroup>

A comment above mentions adding,

<ItemGroup>
    <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

This isn't strictly required, but can help. It is added automatically to all unit test projects by Visual Studio to help it quickly find projects with tests.

If your tests don't appear in Visual Studio, the first thing to try is closing your solution and then re-opening them. There appear to be bugs in Visual Studio not detecting changes to projects when you edit them.

For more information, see Testing .NET Core with NUnit in Visual Studio 2017.


Targeting .NET Framework rather than .NET Standard worked for me. Thanks.
That Microsoft.NET.Test.SDK reference was missing in my project and there was no indication anywhere that anything relied on it to display. Added it via the nuget console and everything started working. Thanks for the reference listing!
I had to copy this folder from a coworker to my temp directory and restart VS: %TEMP%\VisualStudioTestExplorerExtensions\MSTest.TestAdapter.1.1.18
How can I test a netstandard 2.0 project if I change my Target Framework ? I cannot compile anymore because a project netstandard2.0 cannot be referenced by a project that tagets net46
This worked for me. Thanks for the detailed solution.
P
Pang

Forgetting to make the test class public prevents the test methods inside to be discovered.

I had a default xUnit project and deleted the sample UnitTest1.cs, replacing it with a controller test class, with a couple of tests, but none were found.

Long story short, after updating xUnit, Test.Sdk, xUnit.runner packages and rebuilding the project, I encountered a build error:

Error xUnit1000 Test classes must be public

Thankfully, the updated version threw this exception to spare me some trouble.

Modifying the test class to be public fixed my issue.


not sure why down voted, but before my morning coffee 100% this was overlooked by me.
Tried all other answers to this question/problem and this was the one that finally worked!
That one is incredibly embarassing but.. whatever. The hilarious thing is that if you create a test suite case in VS2017 it will not generate the public class, but just the class, so it won't discover it until you add the public identifier.
of course. my bad - mstest should have this feature.
P
Pang

I had the same issue and got it to work by doing the following:

First, close all open Visual Studio instances and delete this folder: %TEMP%\VisualStudioTestExplorerExtensions (Running tests with Visual Studio)

Go to your NuGet package manager and install Microsoft.NET.Test.Sdk (15.3.0-preview-20170425-07) first and then install xunit.runner.visualstudio (2.3.0-beta1-build1309). See attached NuGet screenshot to see all the packages I had to install to get the latest VS 2017 to detect my tests.


Deleting %Temp%\VisualStudioTestExplorerExtensions was enough for me.
Yeap. Just deleting that and restarting VS fixed it.
Does anyone know what causes this in the first place? I've had it happen twice to me now, but deleting that folder and restarting VS worked. It's just weird.
@PmanAce - I did, actually. I'm using two different TFS instances (one per project), so the workspace changes automatically when I switch projects.
deleting the folder, and adding the nuget Microsoft.NET.Test.Sdk seemed to work for me.. thank you StackOverflow. (.NET Framework WebApi 2 solution)
P
Pang

In my case, I target the test project to x64 Architecture and the test setting Architecture (test > Default Processor Architecture) changed was set to x86. They didn't match.

After setting the test setting Architecture back to x64 and rebuilding, all tests were discovered again.


in vs2017 the setting in from menu Test -> Test Settings -> Default Processor Architecture
P
Pang

For me, the issue was that I mistakenly placed test cases in an internal class

[TestClass]
internal class TestLib {
}

That was causing test cases not being identified.


h
hartmape

I had trouble with VS 2017 finding my UnitTest as well. It wasn't the exact problem John was asking - but this was the first result in google that I came looking for so I wanted to share my issue.

I had a legacy solution coming back from VS2010 going over VS2013, VS2015. Now in VS2017 it seems namespaces for the [TestMethod] Attribute have changed.

Before it was using

Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0

I created a new Test.dll in the project and that one used by default

Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0

So my solution was to create a new UnitTest project from within VS2017. Maybe changing assembly references for the old test project would have worked as well. With the new reference VS2017 did discover those unit tests.


Sadly even a new unit test project doesn't have it's test show up for me :/
L
Lex Li

Don't read out of date articles under MSDN. .NET Core relevant materials are under docs.microsoft.com

https://docs.microsoft.com/en-us/dotnet/articles/core/testing/

Generally speaking you need a .NET Core console app to contain the unit test cases.


Thanks much, Lex. If this article is correct, the only way to test .NET Core is from the command line -- thus loosing all the VS integration we had with running tests in VS 2015. Am I correct in this?
Do you use xUnit.net or MSTest?
@JohnPezzanite you have to show more of what you did (probably a GitHub repo if possible). I do have projects at GitHub which works flawlessly, and many others also.
Follow the example in the line I supplied. I have tried it with .NET standard and .NET Core, with Microsoft's unit tests as in the example and with xUnit. .NET standard integrates with VS 2017 while .NET Core will only run from the command line. But I'm repeating what I've stated above. It sounds like Microsoft has removed all .NET Core unit test integration form VS 2017.
@JohnPezzanite test GitHub.com/lextm/sharpsnmplib and its NetStandard solution.
P
Pang

Be sure that you are using the correct Microsoft.NET.Test.Sdk:

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />

Do not use pre-release ones. Or you have to change to console app (not library). I have the similar issue, but with the latest release (15.0.0), it starts working again.

Also, you may have to add:

<ItemGroup>
  <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

but I do not think it is nessesary.


What file is this located in? I can find the "Service Include" part in my project (*.csproj) file but not the PackageReference.
@ChrisBennet in your *test.csproj file.
@evgeni-nabokov is right. All these changes are in the [project].test.csproj file. Right-click on the project in solution and select "Edit [project].test.csproj" See example at: github.com/RenetConsulting/angularcore.net/blob/master/Business/…
M
Marcin Tarsier

I know that OP has listed this on his checklist, but it's easy to overlook that point while doing clean install of Visual Studio 2017 and setting up new project. Apart from NUnit project template and NUnit Framework one needs to install NUnit adaptor separately, e.g. using NuGet command Install-Package NUnit3TestAdapter -Version 3.9.0. After that Visual Studio Community 2017 started discovering unit tests without any issues.


This helped me!
OMG, this one did it for me. If I could drench you with bounties, I would.
This was the only solution that worked for me, thanks!
j
jnt

In my case the Test Explorer couldn't find my tests after I moved the project to a new solution.

The answer was simply that I had a reference to the old MS Test Adapter in my project.

I had a duplicate of the line below for version 1.1.11 of the MS Test Adapter in my cs.proj file:

<Import Project="..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.18\build\net45\MSTest.TestAdapter.props')" />

To fix the problem,

Right click on project and select 'Unload Project'. Right click project and select 'Edit' Remove line that imports old version of adapter. Right click on project and select 'Reload Project'. Rebuild Solution/Project


Had the same issue. Removing and rebuilding solution didn't work. Restarted VS and tests were discovered!
d
dahui

Just had this problem with visual studio being unable to find my tests, couldn't see the button to run them besides the method, and they weren't picked up by running all tests in the project.

Turns out my test class wasn't public! Making it public allowed VS to discover the tests.


J
Jaider

For me was easier to create a new test project that works perfectly fine with Visual Studio 2017... and just copy the test files, add references, and NuGet packages as required.

https://i.stack.imgur.com/9Ti2J.png


creating new project probably saved hours of headache!
d
domenu

Solution was removing my app.config file from my unit test project. The tests will re-appear!

This file referenced some dll's in the bindingredirects that were not actually present in the project references. Re-add the assemblybindings that are strictly necessary for your project.


a
axcha15

I was facing the same issue, in my case in order to resolved

I opened the windows console (windows key + cmd). Navigate to the folder where the project was created. Executed the command "dotnet test" it is basically the same test that visual studio executes but when you run it thru console it allows you to see the complete trace. I got this error message "TestClass attribute defined on non-public class MSTest.TestController.BaseTest" So I went to the test case and mark it as public, build again and my tests are being displayed correctly


P
Pang

In my case, it was a project I had upgraded the test project from an earlier .NET version. in the app.config I had assemblybindings to previous versions of the dependant assemblies.

After fixing the assembnlybindings in the app.config, my tests got discovered.


P
Pang

Discovery

The top answers above did not work for me (restarting, updating to version 1.1.18 ... I was already updated, deleting the temp files, clearning NuGet cache etc).

What I discovered is that I had differing references to MSTest.TestAdapter and MSTest.Framework in different test projects (my solution has two). One was pointed to 1.1.18 like...

packages.config

<package id="MSTest.TestAdapter" version="1.1.18" targetFramework="net461" />
<package id="MSTest.TestFramework" version="1.1.18" targetFramework="net461" />

... but another has the references to 1.1.11. Some of the answers above lead to this discovery when two versions of the libraries showed up in my temp directory (%TEMP%\VisualStudioTestExplorerExtensions\) after restarting Visual Studio.

Solution

Simply updating my packages.config to the 1.1.18 version is what restored my unit tests functionality in VS. It appears that there are some bugs that do not allow side-by-side references of the MSTest libraries. Hope this helps you.

More info:

Visual Studio 2017 Ent: 15.5.6 (I had updated from 15.0.1 with hopes to fix this issue, but I had it in both)


L
Liero

In my case, it was UWP project present in the solution causing the issue.

When I unloaded the UWP project, tests were discovered. When I loaded it back, test disappeard again.

Try to unload all projects and keep test project only. Ten rebuild solution and test shound appear in Test Runner. Load projects one by one and rebuild solution each time to find out what project are causing the problem

sample repo

VS bug report


Appreciate the response but this is not my issue. If you look at the example repo I linked to in my question there is only a single project in the solution. There are not other projects to remove. That solution is a test though so I did try what you said unloading projects on my actual solution but it did not work.
r
rayepps

The Issue

The problem is that Visual Studio is getting 'confused' over the dotnet core versions on the machine. When I went to control panel -> uninstall programs I had 8 different dotnet core SDK's and Runtimes installed. This was somehow causing VS to silently have an error when trying to find tests.

Validate The Issue

You can validate the issue by going to the command line and getting the version of dotnet your on $ dotnet --version. If you see anything except the latest version you have installed then your machine has some mismatch and is not using the correct version. Example...If you have dotnet core 1.0.1 installed but when you get the version at command prompt and it says 1.0.0 thats a problem.

The Solution

Delete all the old stuff. I started with only what I though I needed to remove (the oldest dotnet rc versions) but it still gave the wrong version when testing the issue. Eventually I conceded to do a full clean. I...

Uninstalled all visual studio applications (on my machine VS2015 and VS2017)

Uninstalled all versions of dotnet core (even most recent)

After my machine was completely empty of all VS and donet I installed only VS2017 (it comes packaged with latest dotnet). I created a xUnit test project and the test explorer found the test immediately SOLVED

This may seem like overkill but I spent two weeks trying to fix this in other ways. If your having the issue just do it, even though it may take you hours to uninstall/reinstall items it will probably save you time.

References

See @epestic blog post where he gives more detail on fixing the issue.


f
feng

For C++:

As there is no special question for C++ tests, but the topic is very much the same, here is what helped me when I had the trouble with test discovery.

If you have only installed Desktop development with C++, then the solution is to also install Universal Windows Platform development with the optional C++ Universal Windows Platform tools. You can select these in the visual studio web installer.

Afterwards, rebuild your test project and the test discovery should work.

Btw, I created the unit test project in VS2017. It might be important, because some users mentioned, that they had discovery issues in projects, that were migrated from VS2015 to VS2017.


T
Tanya Fomenko

Removing old .dll should help. Clearing temp files located in the %TEMP% directory at C:\Users(yourusername)\AppData\Local\Temp


F
FLICKER

I had the same issue. My solution was OK but suddenly when I opened the solution I found out the tests are gone.

Finally I downgraded Microsoft.VisualStudio.TestPlatform.TestFramework and Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions packages to a very old version (using NuGet manager) and test methods showed up. Then I upgraded to latest version and still there were there.

So just downgrade and upgrade packages.


v
vlatko606

On my case none of the above help to me. But, i downgrade NUNit3TestAdapter to version 3.8.0, then upgrade to the latest (3.10.0)


R
Rohan

Sometimes changing the namespace of the tests work. I had the folder structure as follows:

A |___B | |___D |___C___E

The namespace was flat like Tests.< name > and they did not show up in the test window. When I changed the namespace to the structure of the directory, all the tests showed up. Now I could revert back to any other namespace structure I want.

Do not forget to build your project!


U
U.Savas

In the case of .NET Framework, in the test project there were formerly references to the following DLLs:

Microsoft.VisualStudio.TestPlatform.TestFramework
Microsoft.VisualStudio.TestPlatform.TestFramework.Extentions

I deleted them and added reference to:

Microsoft.VisualStudio.QualityTools.UnitTestFramework

And then all the tests appeared and started working in the same way as before.

I tried almost all of the other suggestions above before, but simply re-referencing the test DLLs worked alright. I posted this answer for those who are in my case.


P
Pang

I've tried everything but nothing helped. In my case, I had a solution with several test projects and some of them were using the old ms-test framework so Visual Studio found only those.

I installed the test framework packages for all test projects as showed in this answer, then removed the references to the old quality-tools, restarted Visual Studio, and now I can see all tests.


P
Pang

For me, changing the TargetFramework in the test project's .csproj file from

<PropertyGroup>
  <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

to

<PropertyGroup>
  <TargetFramework>net46</TargetFramework>
</PropertyGroup>

worked.


O
Oguzhan Kircali

At first, i've tried to use MSTest. After that, i change it to Nunit test. Then i wanted to back MSTest. I removed all nUnit codes and references but Test Explorer did not show MSTest methods. Solution: I removed all mstest nuget references and reinstalled. Done.


I
Ike

Sometimes, I find if you have stackoverflow exceptions in your unit test code, visual studio will mark that unit test case as not run and will stop running other test cases that follow this case.

In this case, you have to find out which case is causing the stackoverflow exception.