nunitmoqautofixtureautomoq

AutoFixture with NUnit AutoMoq prevents tests from running


UPDATE: AutoFixture team released a fix for this in version 3.51.

Simply extend the AutoDataAttribute doing so:

public class AutoDataFixedNameAttribute : AutoDataAttribute
{
    public AutoDataFixedNameAttribute()
    {
        this.TestMethodBuilder = new FixedNameTestMethodBuilder();
    }
}

Then use this new attribute instead of the built-in AutoData in your NUnit tests.

Starting from v4, this behavior is the default one.


Previous post

I'm trying to use AutoFixture with NUnit and Moq, using the following AutoMoqDataAttribute :

public class AutoMoqDataAttribute : AutoDataAttribute
{
    public AutoMoqDataAttribute()
        : base(new Fixture().Customize(new AutoMoqCustomization()))
    {
    }
}

But when I run this test :

[Test, AutoMoqData]
public void Test(Mock<IUser> user)
{
    // do stuff with user
}

The test never runs. AutomMoqData is hit correctly, but the code inside the test is never executed and everything ends without any warning with the following message :

Test adapter sent back a result for an unknown test case. Ignoring result for 'Test(Mock<Sandbox.IUser>)'

The test also doesn't appear in the test runner list.

But if I remove the parameter :

[Test, AutoMoqData]
public void Test()
{
    // do stuff without user
}

Everything runs fine, but this is less useful without the parameters passed :)

Am I missing something here ?

Here is the list of the Nuget packages versions :

<package id="AutoFixture" version="3.50.2" targetFramework="net452" />
<package id="AutoFixture.AutoMoq" version="3.50.2" targetFramework="net452" />
<package id="AutoFixture.NUnit3" version="3.50.2" targetFramework="net452" />
<package id="Moq" version="4.5.3" targetFramework="net452" />
<package id="NUnit" version="3.7.1" targetFramework="net452" />

EDIT: Following @MarkSeemann's advice, I filed an issue on Github.


Solution

  • Visual Studio Test Runner repro

    This looks like an issue with the NUnit Visual Studio test adapter. I can reproduce the issue when I also add the NUnit3TestAdapter package to my repro solution.

    I'm also assuming that the test class has the [TestFixture] attribute, so that the entire repro class looks like this:

    [TestFixture]
    public class Tests
    {
        [Test, AutoMoqData]
        public void Test(Mock<IUser> user)
        {
            Assert.NotNull(user);
        }
    }
    

    When I attempt to run all tests using Visual Studio 2015's test runner, the test never runs, and this is the output to test output window:

    ------ Run test started ------
    NUnit Adapter 3.7.0.0: Test execution started
    Running all tests in C:\Users\mark\Documents\Stack Overflow\44564377\44564377\bin\Debug\Ploeh.StackOverflow.Q44564377.dll
    NUnit3TestExecutor converted 1 of 1 NUnit test cases
    NUnit Adapter 3.7.0.0: Test execution complete
    Test adapter sent back a result for an unknown test case. Ignoring result for 'Test(Mock<Ploeh.StackOverflow.Q44564377.IUser:8e33>)'.
    ========== Run test finished: 0 run (0:00:01,1763498) ==========
    

    TestDriven.Net

    If, on the other hand, I try to run it with TestDriven.Net, it runs just fine:

    ------ Test started: Assembly: Ploeh.StackOverflow.Q44564377.dll ------
    
    1 passed, 0 failed, 0 skipped, took 0,79 seconds (NUnit 3.7.1).
    

    TestDriven.Net is sometimes extraordinarily tolerant of small errors in the test code, so this may not be that telling in itself.

    NUnit 3 console runner

    Since TestDriven.Net may be too liberal in what it accepts, a better test would be to try with the official NUnit 3 console runner:

    $ packages/NUnit.ConsoleRunner.3.6.1/tools/nunit3-console.exe 44564377/bin/Debug/Ploeh.StackOverflow.Q44564377.dll
    NUnit Console Runner 3.6.1
    Copyright (C) 2017 Charlie Poole
    
    Runtime Environment
       OS Version: Microsoft Windows NT 10.0.15063.0
      CLR Version: 4.0.30319.42000
    
    Test Files
        44564377/bin/Debug/Ploeh.StackOverflow.Q44564377.dll
    
    
    Run Settings
        DisposeRunners: True
        WorkDirectory: C:\Users\mark\Documents\Stack Overflow\44564377
        ImageRuntimeVersion: 4.0.30319
        ImageTargetFrameworkName: .NETFramework,Version=v4.6.1
        ImageRequiresX86: False
        ImageRequiresDefaultAppDomainAssemblyResolver: False
        NumberOfTestWorkers: 4
    
    Test Run Summary
      Overall result: Passed
      Test Count: 1, Passed: 1, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0
      Start time: 2017-06-15 11:09:21Z
        End time: 2017-06-15 11:09:22Z
        Duration: 0.933 seconds
    
    Results (nunit3) saved as TestResult.xml
    

    This, too, successfully executes the test.

    Interim conclusion

    Since both the official console runner and TestDriven.Net successfully executes the test, I'd tentatively conclude that this looks like a defect in the NUnit3TestAdapter package. May I suggest filing an issue for it?