Why is the Visual Studio 2015/2017/2019 Test Runne

2020-01-24 19:39发布

问题:

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.

UPDATE: Adding a 2019; the discovery/runner integration mechanism is same as per 2017 & 2015, so the key things that can go wrong are the same.


I've read Why is the xUnit runner not finding my tests, which covers reasons xUnit would never be able to find your tests but my problem is different - I'm confident there's nothing subtle going on with my tests; (they have worked in other environments, this seems to be just my machine) - the Visual Studio Test Runner in Visual Studio 2015 [Community Edition] is simply not showing any of my tests. I'm not doing anything remotely exciting; the tests target xUnit.net v2 on the Desktop.

I've looked in the Output window and am not seeing anything in at all under Test in the Show output from tabs.

回答1:

  1. Eliminate discovery exceptions from your inquiries; go to the output Window (Ctrl-Alt-O), then switch the show output from dropdown (Shift-Alt-S) to Tests and make sure there are no discovery exceptions

  2. As suggested in this answer(upvote it if the technique helps) running the desktop console runner (instructions) can be a good cross check to eliminate other possibilities, e.g. mangled config files:-

    packages\xunit.runner.console.2.2.0\tools\xunit.console <tests.dll>

    NOTE The xunit.runner.console package is deprecated - when you get stuff working in VS, you'll be able to have dotnet test run them in CI contexts too

  3. Test|Test settings|Default processor architecture can help if your tests are x86/x64 specific and discovery is triggering bittedness-related exceptions, i.e. not AnyCpu


Go read the documentation - it's comprehensive, up to date, includes troubleshooting info and takes PRs:-

Important note: If you've previously installed the xUnit.net Visual Studio Runner VSIX (Extension), you must uninstall it first. The Visual Studio runner is only distributed via NuGet now. To remove it, to go Tools > Extensions and Updates. Scroll to the bottom of the list, and if xUnit.net is installed, uninstall it. This will force you to restart Visual Studio.

If you're having problems discovering or running tests, you may be a victim of a corrupted runner cache inside Visual Studio. To clear this cache, shut down all instances of Visual Studio, then delete the folder %TEMP%\VisualStudioTestExplorerExtensions. Also make sure your project is only linked against a single version of the Visual Studio runner NuGet package (xunit.runner.visualstudio).

The following steps worked for me:

  1. (Only if you suspect there is a serious mess on your machine - in general the more common case is that the visual studio integration is simply not installed yet)

    Do the DEL %TEMP%\VisualStudioTestExplorerExtensions as advised :-

    PS> del $env:TEMP\VisualStudioTestExplorerExtensions

  2. Install the NuGet Package xunit.runner.visualstudio in all test projects

    • Paket:

      .paket\paket add nuget xunit.runner.visualstudio -i
      

      You need to end up with the following in your paket.dependencies:

      nuget xunit.runner.visualstudio version_in_path: true

      Note the version_in_path: true bit is important

    • Nuget: Go to Package Manager Console (Alt-T,N,O) and

      Install-Package xunit.runner.visualstudio)
      

    Rebuild to make sure xunit.runner ends up in the output dir

  3. Close Test Explorer <- this was the missing bit for me

  4. Re-open Test Explorer (Alt-S,W,T)

  5. Run All tests (Ctrl R, A)



回答2:

I had to change the Test Settings after changing the test projects CPU to x64. Then the tests where detected again.



回答3:

None of the above solutions worked for me (dotnetcore 1.1, VS2017). Here's what fixed it:

  1. Add NuGet Package Microsoft.TestPlatform.TestHost
  2. Add NuGet Package Microsoft.NET.Test.Sdk

Those are in addition to these packages I installed prior:

  • xunit (2.3.0-beta1-build3642)
  • xunit.runner.visualstudio (2.3.0-beta1-build1309)


回答4:

Install xunit.runner.visualstudio package for the test project



回答5:

Follow this steps :

  1. Update your MsTest.TestAdapter and MsTest.TestFramework dll's from nugget package manager.
  2. Clean your solution
  3. Build your solution.


回答6:

I've been struggling with this all afternoon whilst working with an ASP Core project and xUnit 2.2.0. The solution for me was adding a reference to Microsoft.DotNet.InternalAbstractions

I found this out when trying to run the test project manually with dotnet test which failed but reported that InternalAbstractions was missing. I did not see this error in the test output window when the auto discovery failed. The only info I saw in the discovery window was a return code, which didn't mean anything to me at the time, but in hindsight was probably indicating an error.



回答7:

It's happened to me a couple of times - when I Clean the project and Build it again it tends to be fine.



回答8:

The reason in my case was the target build was not the same between project debugger and test runner. To unify those elements:

  1. Test>Test Settings>Default Processor Architecture. then select either X64 or X86.
  2. Project>(your project)Properties>Build(tab)>platform target.

After they are identical, rebuild your solution then test methods will appear for you.



回答9:

After spending 2 days... none of the above worked for me. The only "solution" was: Go to project properties -> Build Tab. Then click Advanced button on the right bottom corner of the pane. Change "Debug Info:" to "full" and click OK.

Here are the screen shots:



回答10:

I am using xUnit 2.2.0.

My issue was my solution was not able to find certain dlls and app.config was trying to resolve them. The error was not showing up in the test output window in Visual Studio.

I was able to identify the error when I installed xunit.runner.console and tried to run the tests through command line.

How to run xunit tests in CLI.



回答11:

I can provide a solution for an edge case I ran into a few days ago. It's not gonna be the solution that fits all of the scenarios described above, however, for the edge case I had it fixed it.

I had the same issue with most recent VS 2017 (version 15.5.7) and XUnit 2.3.1. The xunit.runner.visualstudio package was installed, however, the tests didn't show up in VisualStudio's built-in test explorer.

I was working on a legacy project that was targeting .NET framework 4.5. However, beginning with version 2.2. XUnit does not support .NET frameworks lower thant 4.5.2 (see Release Notes - XUnit 2.2: February 19, 2017

Changing the test project's target framework to a version >= 4.5.2 worked for me. You don't have to change the project's version that you're testing, it's just about the test project itself.



回答12:

Make sure that your test class is public.



回答13:

I had the same issue with Visual Studio 2019. Just Installed the following NuGet packages and the issue was solved.

1). xUnit

2). xunit.runner.visualstudio

3). Microsoft.TestPlatform.TestHost

4). Microsoft.NET.Test.Sdk



回答14:

This can also be due to the build check box not being ticked for the current platform project in the build configuration. Click Build | Configuration manager, then make sure the test projects have a tick in the build column for the platform that you're using (for example 'x86').

This was definitely the solution that worked for me.



回答15:

Do make sure that you haven't written your unit tests in a .NET Standard 2.0 Class Library. The visualstudio runner does not support running tests in netstandard2.0 class libraries at the time of this writing.

Check here for the Test Runner Compatibility matrix:

https://xunit.github.io/#runners



回答16:

In my case, I had 2 different test projects in the solution. Project 1 tests could be found, but Project 2 tests could not. I found that first Unloading the test Project 1, then closing VS > clearing my temp files > re-open solution > rebuild, allowed VS to discover my Project 2 tests.

I'm assuming something must be conflicting between the two test projects and this was the quickest way to get me up and running in a few minutes. The kinks can be worked out later :).



回答17:

Ran into a similar problem with VS not discovering test methods. In my case I had the static keyword with the method, which I removed and it worked.

[TestMethod]

Before: public static void Test1()

After: public void Test1()


回答18:

I was suffering from this problem for long times.

  • I had about 100 projects different version was deployed in different server.

  • Updating xunit from 2.2.0 to 2.3.1 was not a solution because build was failing in 2.3.1.

Then I just updated xunit.runner.visualstudio to 2.3.1 and everything started to work fine. I have used this command in my package manager console to updated my xunit.runner.visualstudio package

Get-Project ComapanyName.ProjectName.*.Tests | Install-Package xunit.runner.visualstudio -Version 2.3.1


回答19:

The most common culprit for me has been Visual Studio trying to run the tests using a different architecture than the library it's testing. Unfortunately there are multiple places where it seems this can go wrong.

In VS 2017, try creating a Run Settings file, e.g. Default.runsettings in your test project. If your main lib is x64, the contents should be:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <RunConfiguration>
    <TargetPlatform>x64</TargetPlatform>
  </RunConfiguration>
</RunSettings>

Then choose this file from Test -> Test Settings -> Select Test Settings File.

Then, under Test -> Test Settings, Default Processor Architecture, choose the correct architecture again.

Be sure to Clean and Build the entire solution. You may need to close and reopen the Test Explorer window. Look for any additional errors in the Output -> Test window, for more clues about incorrect architecture types.

FYI additional Test Settings entries can be found here.



回答20:

There is one other reason that can cause Test Explorer not showing any tests, and it has to do with the new portable .pdb file format introduced with Visual Studio 2017 / for .NET Core which can break some VS tooling. (Background: See the bug report "Mono.Cecil causes OutOfMemoryException with new .csproj PDBs".)

Are your tests not found because of the new portable .pdb (debug symbols) format?

  • Open the Output window.
  • Change drop-down selection for Show output from to Tests.
  • If you see output like to the following (possibly repeated once for each of your tests), then you've got the problem described in this answer:

    Exception System.OutOfMemoryException, Exception converting <SignatureOfYourTestMethod>
    Array dimensions exceeded supported range.
    

If yes, do this to resolve the problem:

  • Open your test project's Properties (select the test project in Solution Explorer and press Alt+Enter).
  • Switch to the Build tab.
  • Click on the Advanced... button (located at the very end of that tab page).
  • In the drop-down labelled Debugging information, choose none, pdb-only, or full, but NOT portable. It is this last setting that causes the tests to not be found.
  • Click OK and clean & rebuild your project. If you want to be extra sure, go to your test project's output directory and clean all .pdb files before rebuilding. Now your tests should be back.


回答21:

Happend to me when i took my first first walking attempts with IntelliTest in VS 2017.

Sometimes, when the test project gets auto-created by IntelliTest, the assembly reference to Microsoft.ExtendedReflection (...\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\Pex\Microsoft.ExtendedReflection.dll) is missing. When added, the generated Tests will show up in test explorer after recompile.



回答22:

Disclaimer: it is not about xunit with visual studio 2015, but Visual Studio 2017 with a UWP unit test application (MSTest). I got to this thread searching the same thing so maybe someone else will do the same :)

The solution for me was to update the nuget packages for MSTest.TestAdapter and MSTest.TestFramework. It seems that when you create a unit test app for UWP you don't automatically get the latest versions.



回答23:

My problem was resolved by installing the nuget xunit.runner.visualstudio



回答24:

In my case I have multiple test projects in the same solution, and only one of the projects was not displaying the "Test Explorer"

I went to the "Manage Nuget Package for Solution" by right-clicking on the solution.

I noticed under the "Consolidate" tab there was some "Test" nuget packages that were out of sync between the projects. I clicked on "Install" and my missing tests showed up.



回答25:

I tried most of the suggestions above and nothing worked. In my case, I'm on a team and tests were appearing for other devs for the same solution. So, I attempted just deleting my .vs folder, but no luck there either.

I ended up deleting my local folder entirely and re-cloning the repo. That solved it for me.



回答26:

Here is the solution that worked for us. Not the best but maybe one can benefit.

Background:

  • Our scripts were developed with VS 2013 and used NUnit VS Adapter 2.1..
  • Recently we migrated to VS 2017 and when open the same solution - test wouldn't show in the Test Explorer

Upon Build we would see this message:

[Informational] NUnit Adapter 3.10.0.21: Test discovery starting
[Informational] Assembly contains no NUnit 3.0 tests: C:\ihealautomatedTests\SeleniumTest\bin\x86\Debug\SeleniumTest.dll
[Informational] NUnit Adapter 3.10.0.21: Test discovery complete

Solution (temporary):

  • Uninstall NUnit Adapter 3.10...
  • Install NUnit VS Adapter 2.1..

Now the Tests are shown.



回答27:

  1. Close all Visual Studio instances
  2. Go to %TEMP%\VisualStudioTestExplorerExtensions\
  3. Delete specrun related folders
  4. Try again

let me know, thanks



回答28:

Also check if a completely empty app.config file (completely blank with absolutely no markup) is within the test project. This was the culprit in my case.



回答29:

In my case, I created a new "Solution Configuration" like shown in the image. So when I select my custom one as "Prod", it doesnt recognize TestMehods for some reason. Changing back to "Debug" solves the problem



回答30:

I don't know if some of you also use JustMock, but I had to disable the profiler in VS 2017 for test detection to work.