AutoFixture with NUnit AutoMoq prevents tests from running

时间:2017-06-15 09:52:06

标签: nunit moq autofixture automoq

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.

1 个答案:

答案 0 :(得分:6)

Visual Studio Test Runner repro

这看起来像NUnit Visual Studio测试适配器的问题。当我还将NUnit3TestAdapter包添加到我的repro解决方案时,我可以重现这个问题。

我还假设测试类具有[TestFixture]属性,因此整个repro类看起来像这样:

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

当我尝试使用Visual Studio 2015的测试运行器运行所有测试时,测试永远不会运行,这是测试输出窗口的输出:

------ 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

另一方面,如果我尝试使用TestDriven.Net运行它,它运行得很好:

------ Test started: Assembly: Ploeh.StackOverflow.Q44564377.dll ------

1 passed, 0 failed, 0 skipped, took 0,79 seconds (NUnit 3.7.1).

TestDriven.Net有时会非常容忍测试代码中的小错误,所以这本身可能不是那么说明。

NUnit 3控制台跑步者

由于TestDriven.Net可能过于宽松,因此更好的测试是试用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

这也成功地执行了测试。

中期结论

由于官方控制台运行程序和TestDriven.Net都成功执行了测试,我暂时得出结论,这看起来像是NUnit3TestAdapter包中的缺陷。我可以建议为它提出问题吗?