Visual Studio 2013 doesn't discover unit tests

93,153

Solution 1

Some things I've noticed I have to do from time to time to get tests to show up properly.

  1. If your solution is in a protected drive that you need administrator access to read/write, sometimes only a portion of the tests come up. Definitely run VS as administrator in that case.

  2. If your solution is 64 bit, make sure that Test > Test Settings > Default Processor Architecture is set to x64. Sometimes it gets set to x86. Set it to x64, then rebuild.

  3. Sometimes just restarting Visual Studio does the trick because the test explorer will start up again.

  4. Don't forget to actually build the test project/solution. (If you want it to get built with the rest of the projects, right-click on your solution > Properties > Configuration Properties > Configuration > check the "Build" box for your test project)

  5. Ensure the tests are in a public section of your test class

Solution 2

If you using NUnit, make sure to download NUnit Adapter first.

Go to Tools → Extensions and Updates… → Online → search for "NUnit Test Adapter".

Solution 3

Make sure your test class is public so it can be found. And if you're referencing another class, make sure of the same.

Also, sometimes if you have no Asserts or you're not decorating the test with a [TestMethod], a test might not be recognized.

2 more things: 1) Async unit tests act funny at best, and none at all at worst. Have a look at this article by Stephen Cleary and keep from there if it interests you.

2) If you use NUnit and you run into the same issues, keep in mind it's [TestCase] for Nunit, instead of [TestMethod]

Having said the above, here's an article I've posted on the code project, with both MSTest & NUnit, in case you want to give it a spin and make sure you're not missing anything.

Solution 4

I had the same issue but none of the other solutions worked. Turns out that I was using the NUnit 3 framework with the 2 adapter.

If you're using NUnit 3, go to Extensions and Updates and install the NUnit3 Test Adapter.

Solution 5

I'm having this issue from time to time. What works for me is to shutdown Visual Studio and go to folder:

%LocalAppData%\Microsoft\VisualStudio\12.0\ComponentModelCache

and delete it content.

Once you open Visual Studio and load your project again Test Explorer should contain back your tests

Share:
93,153

Related videos on Youtube

miguelbgouveia
Author by

miguelbgouveia

I am developer with 10 years of experience. Interested in open source and sharing ideas. Coding using TDD process.

Updated on February 08, 2021

Comments

  • miguelbgouveia
    miguelbgouveia about 3 years

    I have a simple solution in visual studio 2013 that is composed by one web project, one library project and one unit test project. When I open the solution and try to run the unit tests they are not discover by visual studio. To run the tests I try to go to the menu and choose Test -> Run -> Run all tests or by opening the test explorer window. By those to methods visual studio doesn’t discover any tests in the solution.

    Creating first a simple unit tests project and try to run the test, visual studio know discover the test and I can run it. Then, if I open my previous solution visual studio now discovers all the tests. I try to save my solution but closing it and reopening, without creating a unit test project first, the visual studio doesn’t find the tests again. This is a very strange behave that I don’t know why this is happening.

    I used to working alone in this project that was using the source control git integrated with the visual studio team foundation. The problem of visual studio not discover the unit tests start when a new element came to the project and when I need to recreate the solution through the source control online. Before this, all tests always been discovered by visual studio.

    For creation the unit tests I use the dll Microsoft.VisualStudio.QualityTools.UnitTestFramework. My version of visual studio is: Microsoft Visual Studio Express 2013 for Web Version 12.0.30723.00 Update 3. My version of .net framework is 4.5.50938.

    All of my tests are like this:

    [TestClass] 
    public class Service1Test 
    { 
        [TestMethod] 
        public void Test1() 
        {
            Assert.IsTrue(True); 
        } 
    }
    
    • Jamie Keeling
      Jamie Keeling over 9 years
      Are these Async based Unit tests?
    • Sriram Sakthivel
      Sriram Sakthivel over 9 years
      Not sure what the issue was, but running as administrator fixed the issue for me.
    • miguelbgouveia
      miguelbgouveia over 9 years
      All sync based unit tests
    • Random Dev
      Random Dev over 9 years
      Have you tried an external test-runner (like ReSharpers or NCrunch)? Maybe your install is bugged (so reinstall VS)
    • miguelbgouveia
      miguelbgouveia over 9 years
      I already try to reinstall visual studio and make all the updates but the problem maintains. I will try an external tool but I think it will work fine. The problem seems to be in visual studio.
    • Jeroen Vannevel
      Jeroen Vannevel over 9 years
      Did you clean your solution and rebuild everything? Perhaps rebuild all projects inside it separately? Do you get any output in the Tests and Build output pane?
    • miguelbgouveia
      miguelbgouveia over 9 years
      I try to install the external tool NCrunch. I think because I’m using VSExpress version I have to install this tool manually. After I run the command: VWDExpress.exe /setup /nosetupvstemplates to install the tool miraculously the tests start appearing in VS. Closing a reopening the solution, the tests disappear again. Funny that after all the installation of the tool doesn’t result.
    • miguelbgouveia
      miguelbgouveia over 9 years
      @JeroenVannevel, I already try to clean my solution and rebuild everything without results. Rebuild all projects also doesn’t work. When building the projects I get the following message: ------ Discover test started ------ The system cannot find the file specified ========== Discover test finished: 0 found (0:00:00,2675781) ==========
    • Adam
      Adam over 7 years
      None of these fixed the issue for me :( What a disaster. I've given up NUnit and am relying on UnitTestFramework - bizarrely the opposite problem from the OP
    • miguelbgouveia
      miguelbgouveia over 7 years
      The same for me @Adam. I use known Xunit.
    • Ruben
      Ruben about 7 years
      for me it was not working because my project was located on a network drive, moving it to a local folder fixed it
  • miguelbgouveia
    miguelbgouveia over 9 years
    All of my tests are like this: [TestClass] public class ServicesUtilsTest { [TestMethod] public void Test1() { Assert.IsTrue(True); } }
  • Noctis
    Noctis over 9 years
    this is not very clear . Put it in a code block in your question, so it can be understood :)
  • miguelbgouveia
    miguelbgouveia over 9 years
    All my tests is for sync code, and I think my problem is not in the unit tests code but more in visual studio sometimes not discovering the tests.
  • Noctis
    Noctis over 9 years
    Try using this instead: using Microsoft.VisualStudio.TestTools.UnitTesting;
  • miguelbgouveia
    miguelbgouveia over 9 years
    I am already using Microsoft.VisualStudio.TestTools.UnitTesting. This namespace are defined in the dll Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
  • Jeroen Vannevel
    Jeroen Vannevel over 9 years
    Asynchronous unit tests are supported in VS 2013, which is what the OP is using.
  • Noctis
    Noctis over 9 years
    @JeroenVannevel Yep, but they might not act as he expects, unless he's aware of what's happening.
  • AndyG
    AndyG about 9 years
    I realize this answer is a little late, but my Google search brought me here, and nothing mentioned solved my problem. Eventually I figured out it was #2 in my list, so I wanted to leave that knowledge, plus the other tricks I've picked up over time.
  • miguelbgouveia
    miguelbgouveia almost 9 years
    I'm using UnitTests from Microsoft. In this case I suppose that isn't necessary install anything.
  • Nate Barbettini
    Nate Barbettini almost 9 years
    A combination of #2 and #3 did it for me. Visual Studio complained about a number of (non-test) projects in my solution being excluded from the test discovery step because they were built for x86, but that was fine.
  • user1807768
    user1807768 almost 9 years
    I found that my settings were all correct and restarting did not work. What did fix the problem for me was simply building the solution. I know this may seem dumb but it is not obvious that is necessary; I have not seen any of the official docs mention this step.
  • Frank
    Frank almost 9 years
    It does say this in the description of the Nuget package. But if you're like me and don't read, I'm hoping that this will help: "This package includes the NUnit 3.0 framework assembly, which is referenced by your tests. You will need to install version 3.0 of the nunit-console program or a third-party runner that supports NUnit 3.0 in order to execute tests. Runners intended for use with NUnit 2.x will not run 3.0 tests correctly."
  • miguelbgouveia
    miguelbgouveia almost 9 years
    But where is the solutions platform menu? Is that in Visual Studio? I'm using Visual Studio Express 2013 for Web and I can't find that menu.
  • miguelbgouveia
    miguelbgouveia almost 9 years
    For me that doesn't work. I am using Visual Studio 2013 Express with update 5 install recently. Still no unit tests appearing.
  • miguelbgouveia
    miguelbgouveia over 8 years
    I don't have any Configuration Manager option in my project menu. I can only find the project properties option. I very the platform of the unit tests project and it is the same of the others projects. So, for me this solution doesn't work.
  • miguelbgouveia
    miguelbgouveia over 8 years
    For me, the dll's for the tests also weren't build because visual studio doesn't find any defined tests. I'm using Visual Studio Express and I don't have the Build menu entry. But in my configuration manager I have all build options checked. So, I think that is no the problem for my case.
  • mcolegro
    mcolegro over 8 years
    I had the same problem in VS2015. #2 fixed the problem for me.
  • Rich Shealer
    Rich Shealer over 8 years
    In my case I knew it had something to do with the NUnit3 update, but one set of my test passed and the others weren't seen. Upon closer inspection there were a lot of exceptions in the output, even though the tests all passed.
  • amalgamate
    amalgamate over 8 years
    Define "matches the rest of the project"
  • Matas Vaitkevicius
    Matas Vaitkevicius over 8 years
    This did it for me - much obliged.
  • Eduardo Brites
    Eduardo Brites over 8 years
    I just had to build the Test project using Any CPU, the other projects remained x64
  • Jens
    Jens over 8 years
    This was the case for me too. I had run nuget "update-package" without realizing it updated from NUnit 2.x to 3.x.
  • Matthew Hoggan
    Matthew Hoggan over 8 years
    I have no idea why people pay so much money for a product that fails so often. I had to upgrade my project to 2015 and do #3 2 times before it discovered my test.
  • Chris Hawkes
    Chris Hawkes over 8 years
    I hate the test explorer with a passion
  • miguelbgouveia
    miguelbgouveia over 8 years
    I known that using another unit test framework will resolve my problem. But if I wanted continuing use the Microsoft Unit Tests framework that is not a solution.
  • miguelbgouveia
    miguelbgouveia about 8 years
    I'm using UnitTests from Microsoft. In this case I suppose that isn't necessary install anything.
  • Admin
    Admin about 8 years
    @miguelbgouveia, it's the other way around - VS builds DLLs, and then scans those for tests. So if you have no test project DLLs, you will definetely find no tests.
  • miguelbgouveia
    miguelbgouveia about 8 years
    That already happen to me. But in this they that is not the case.
  • miguelbgouveia
    miguelbgouveia about 8 years
    My classes are not abstract. That's not my problem.
  • vauhochzett
    vauhochzett about 8 years
    Currently the NUnit 3.0 Test Adapter can't be found via NuGet (see the NUnit 3.0 Wiki). It can stil be installed as an extension though.
  • miguelbgouveia
    miguelbgouveia about 8 years
    I am not using NUnit.
  • Ryan Buddicom
    Ryan Buddicom about 8 years
    #4 for me, had forgotten that I had set the test project to only build in debug.
  • FuckStackoverflow
    FuckStackoverflow about 8 years
    Of all the things.... This was my problem and the params bit makes sense. After all, what would the test system know to pass in? The solution there is to make a test method where you manually call the method you want to test. If you're testing a WebAPI project and have a Get with params, you still have to have the matching Get call but it won't show up in explorer.
  • metalhead
    metalhead about 8 years
    5. Make sure your assembly isn't also in the GAC. The test explorer will find the one in the GAC first and not see your new tests in your solution.
  • Jonathan Gilbert
    Jonathan Gilbert almost 8 years
    Technically, C#/VB.NET always compile to MSIL. The project setting "x86", "x64" or "Any CPU" (and, in newer versions, "Prefer 32-bit") is just flags at the top of the EXE/DLL. Point still stands, though; NUnit won't list tests it can't load into the execution engine, on account of them being marked as requiring a particular architecture to run.
  • tyrion
    tyrion almost 8 years
    I had a NUnit 2 Test Adapter installed with NUnit 3 framework as nuget package. Reading your answer made me realize this mistake. After installing Nunit 3 Test Adapter, it works fine for me. Thanks for the hint.
  • santosh kumar patro
    santosh kumar patro almost 8 years
    Thanks a lot Frank. It solved my issue. Now I am able to see the unit test results in the console window :)
  • Jacob McKay
    Jacob McKay almost 8 years
    Hmmm I had private fields in my test class, and when I switched em to public after reading this my test was rediscovered. Thanks @Noctis
  • Siva Sankar Gorantla
    Siva Sankar Gorantla almost 8 years
    In my case, class was not public.. Thank you
  • C-F
    C-F almost 8 years
    This does not answer the question above, but this is exactly the problem I was trying to solve. So you got accident +1 :) Thank you so much!
  • escape-llc
    escape-llc over 7 years
    got me too! i forgot to make the test context property public.
  • duyn9uyen
    duyn9uyen over 7 years
    I had to switch my solution configuration to Debug. It was on one of my other transform configs.
  • AndyG
    AndyG over 7 years
    @duyn9uyen: That's interesting, is there are preprocessor macro that prevents the tests from compiling unless it's for DEBUG?
  • Sipke Schoorstra
    Sipke Schoorstra over 7 years
    Number 2 + 3 did the trick for me. I was experiencing this issue in VS 2015, where my solution contains Service Fabric projects (x64), but my unit test class libraries were set to "Any CPU". So as soon as I referenced an x64 project from the unit test project, the tests would simply fail to be discovered. So in addition to #2 and #3, I had to change the target platform of the unit test project to x64.
  • MartijnK
    MartijnK over 7 years
    It's in the solution of your project. Right-click your project file in the Solution Explorer -> Open Folder in File Explorer. Go one directory up from there and delete the /TestResults directory. You may have to shut down Visual Studio to delete everything. It will rebuild the directory the next time the project is opened.
  • Vort3x
    Vort3x over 7 years
    Same problem with VS2015 Community edtion. I actually had this issue after the Windows 10 Anniversary update, I needed to run as administrator (previously this wasn't necessary). So #1 for me. Thanks for the thorough answer @AndyG
  • amarnath chatterjee
    amarnath chatterjee over 7 years
    I was facing the same issue , had a hard time in finding which dlls were not loading....
  • causa prima
    causa prima over 7 years
    #3 was my problem. Thanks.
  • Tom
    Tom over 7 years
    To elaborate on step #1: I had NUnit installed but it was for unknown reasons disabled. Uninstalling didn't seem possible and then it hit me: I've been running VS with Administrator privileges whilst NUnit was installed without it.
  • some bits flipped
    some bits flipped over 7 years
    @metalhead - I did not need to check the GAC, but if you're right about that please edit the answer to add it. And man, i'll add, that's insane. How this (relatively expensive) product can fail silently like this, requiring (for me) some arbitrary combination of #2 and a vs-restart (#3) ...
  • Aaron
    Aaron over 7 years
    This triggered the solution for me; the test function was marked static. Removing the modifier and rebuilding the project immediately made it appear in the Test Explorer.
  • Noctis
    Noctis over 7 years
    @Aaron hmm ... interesting ...wonder why a static method will not be picked up ... you using mstest or nunit? and which version btw ?
  • Aaron
    Aaron over 7 years
    @Noctis MSTest, "MSTest.TestAdapter" version="1.1.8-rc" and "MSTest.TestFramework" version="1.0.8-rc".
  • Stephan Møller
    Stephan Møller over 7 years
    This was also what worker for me. Even worked without restarting visual studio. Hours down the drain finally had an end.
  • jjthebig1
    jjthebig1 about 7 years
    That fixed it for me. Thanks.
  • jao
    jao about 7 years
    Don't forget to make the test method public. Took me a good part of an hour to figure that out!
  • Owain Glyndŵr
    Owain Glyndŵr about 7 years
    Upvoted as did resolve issue but is it necessary to have NUnit adapter or should Visual Stdio work with NUnit out-the-box.
  • whiteshooz
    whiteshooz about 7 years
    Adding the following dependency fixed it for me: "Microsoft.DotNet.InternalAbstractions": "1.0.1-beta-003206"
  • Nox
    Nox about 7 years
    You are a god sir.
  • Skystrider
    Skystrider over 6 years
    This didn't work for me. Test explorer just does not find my test and as far as I can tell I'm not doing it wrong and I've tried some of the solutions here without success.
  • Noel Widmer
    Noel Widmer over 6 years
    This directory does no longer exist in VS2017.
  • Nathan
    Nathan over 5 years
    Using MsTestV2 - this was the only thing that fixed the problem
  • Ravi Kumar Mistry
    Ravi Kumar Mistry about 4 years
    Thanks it works for me for any of those this folder can have diffrent version depending on the installation of visual studio like for me it is %LocalAppData%\Microsoft\VisualStudio\16.0_03b7a93c\Componen‌​tModelCache