Search code examples
visual-studio-2015nunit

NUnit3TestDiscoverer throws an exception, no tests visible in Test Explorer


I want to use NUnit v3 test cases. For this I wrote an test case in c# with everything as simple as possible. When I load the solution my tests are visible in the test explored but once I build the project none is visible.

I've searched the internet. There are reports of similar behaviour but likely not the same problem. Most of the reports are dated, closed and may not be relevant to the problem I have. (But I tested all suggestions given anyway, to no result.)

Just to name two links. Suggestions from An exception occurred while test discoverer 'NUnit3TestDiscoverer' was loading tests do not apply or do not work. And suggestions from https://github.com/nunit/nunit3-vs-adapter/issues/771 do not work.

This is my project details:

  • Compiler: VS2015 (I have to use this).
  • Target framework: .NET Framwork 4.6.2 (also tried 4.6)
  • Output: .dll with NUnit tests.

Packages.config:

<packages>
  <package id="Microsoft.CodeCoverage" version="17.6.0" targetFramework="net462" />
  <package id="Microsoft.NET.Test.Sdk" version="17.6.0" targetFramework="net462" />
  <package id="NUnit" version="3.13.3" targetFramework="net462" />
  <package id="NUnit3TestAdapter" version="3.16.1" targetFramework="net462" developmentDependency="true" />
</packages>

TestCase.cs

using NUnit.Framework;
namespace As.NUnit3
{
    [TestFixture]
    public class TestCase //: AssertionHelper
    {
        [Test]
        public void IsNull()
        {
            string nada = null;

            // classic syntax
            Assert.IsNull(nada);

            // constraint syntax
            Assert.That(nada, Is.Null);

            // inherited syntax
            //Expect(nada, Null);
        }
    }
}

The project builds without problems.

When I open the solution one test is visible in the Test Explorer. If I press 'Run All' the test passes and the following output is given.

------ Run test started ------
NUnit Adapter 3.17.0.0: Test execution started
Running all tests in ...\NUnit3\sources\libNUnit3Cs\bin\Debug\libNUnit3Cs.dll
   NUnit3TestExecutor discovered 1 of 1 NUnit test cases
NUnit Adapter 3.17.0.0: Test execution complete
========== Run test finished: 1 run (0:00:00,8113143) ==========

When I (re)build the solution no tests are visible and the following output is given.

1>------ Rebuild All started: Project: libNUnit3Cs, Configuration: Debug Any CPU ------
1>  libNUnit3Cs -> ...\NUnit3\sources\libNUnit3Cs\bin\Debug\libNUnit3Cs.dll
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
------ Discover test started ------
An exception occurred while test discoverer 'NUnit3TestDiscoverer' was loading tests. Exception: Object reference not set to an instance of an object.
========== Discover test finished: 0 found (0:00:00,1269915) ==========

Suggestions from An exception occurred while test discoverer 'NUnit3TestDiscoverer' was loading tests
Close VS and run as administrator.
No result.

Delete %temp%\VisualStudioTestExplorerExtensions\NUnit3TestAdapter.version
VisualStudioTestExplorerExtensions does not exist.

Also tried: delete .vs
No result.

And suggestions from https://github.com/nunit/nunit3-vs-adapter/issues/771
(It is a known issue. Try to downgrade to https://github.com/nunit/nunit3-vs-adapter/releases/tag/V3.16.1)

This link give more details about the problem and advises me to downgrade nunit3-vs-adapter which I did. No result.
Also, this link is from sept 2020, the issue is 'closed' and the current release is v4.4.2, one may expect that the underlaying problem is resolved and I have another issue with the same effect.

I don't know how to continue. Should I issue a bug report with NUnit or is there something that I simply do not see? Is there anything I can do before I report it?

Any advise is welcome!

Extra: The project builds for (Debug, Any CPU)

On the internet there are reports that Any CPU can cause problems or changing from x86 to x64 (or the other way arount) resolves the issue.

Building for (Debug, x64) does not change anything, most of the time. But I've seen it working in at least one case.

Extra 2: Found one procedure to get it working.

  1. flip Test -> Test Settings -> Default CPU Architecture to one of { x86, x64 }
  2. flip [Solution Platform] to identical setting as from 1)
  3. build or rebuild. Works the first time, repeat steps 1 and 2 for a second time.

Solution

  • I noticed the following fragment NUnit Adapter 3.17.0.0: Test execution complete. That is reporting v3.17.0.0 while I was assuming v3.16.1.0 was active.

    Using tools -> Extensions and Updates ... I uninstalled NUnit Adapter 3.17.0.0 and now I can build and rebuild without a problem using (NuGet package) v3.16.1.0

    I consider this build 'problem' now as solved.

    Also, step 2 in the work around I found before is not required.

    Upgraded the NuGet package to use v4.4.2, running the tests still reports v3.16.1 active.

    ------ Run test started ------
    NUnit Adapter 3.16.1.0: Test execution started
    Running all tests in ...\NUnit3\sources\libNUnit3Cs\bin\x64\Debug\libNUnit3Cs.dll
       NUnit3TestExecutor converted 1 of 1 NUnit test cases
    NUnit Adapter 3.16.1.0: Test execution complete
    ========== Run test finished: 1 run (0:00:01,5743354) ==========
    

    Still reporting v3.16.1 as active but that is another issue.