使用NUnit AutoMoq的AutoFixture可防止测试运行

问题描述 投票:4回答:1

更新:AutoFixture团队released a fix为版本3.51。

只需扩展AutoDataAttribute这样做:

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

然后在NUnit测试中使用这个新属性而不是内置的AutoData

从v4开始,此行为是默认行为。


上一篇文章

我正在尝试将AutoFixture与NUnit和Moq一起使用,使用以下AutoMoqDataAttribute:

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

但是当我运行这个测试时:

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

测试永远不会运行。正确命中了AutomMoqData,但测试中的代码永远不会执行,一切都会结束 没有任何警告 以下消息:

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

该测试也未出现在测试运行器列表中。

但是,如果我删除参数:

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

一切运行正常,但如果没有传递参数,这就不太有用了:)

我在这里错过了什么吗?

以下是Nuget软件包版本的列表:

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

编辑:按照@ MarkSeemann的建议,我filed an issue on Github

nunit moq autofixture automoq
1个回答
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 console runner

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

这也成功地执行了测试。

Interim conclusion

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

© www.soinside.com 2019 - 2024. All rights reserved.