使用具有 NSubstitute 自动数据属性的 AutoFixture 时测试丢失

Eri*_*ler 2 xunit autofixture nsubstitute

按预期发现具有以下测试的测试类:

[Theory]
[AutoData]
public void MyDiscoveredTest() { }
Run Code Online (Sandbox Code Playgroud)

但是,缺少以下测试:

[Theory]
[AutoNSubstituteData]
public void MyMissingTest() { }
Run Code Online (Sandbox Code Playgroud)

有趣的是,如果我把 放在MyDiscoveredTest 后面 MyMissingTest,那么MyDiscoveredTest现在也丢失了。我尝试了 xUnit Visual Studio 运行程序和 xUnit 控制台运行程序,得到了相同的结果。

我的AutoNSubstituteData属性在这里定义:

internal class AutoNSubstituteDataAttribute : AutoDataAttribute
{
    internal AutoNSubstituteDataAttribute()
        : base(new Fixture().Customize(new AutoNSubstituteCustomization()))
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

一个相关的问题:由于AutoNSubstituteDataAttribute上述似乎是一个相当常见的属性,我想知道为什么它不包含在 AutoFixture.AutoNSubstitute 中。同样有用的是InlineAutoNSubstituteDataAttribute. 我应该提交这些请求请求吗?

使用的 Nuget 包版本:
AutoFixture 3.30.8
AutoFixture.Xunit2 3.30.8
AutoFixture.AutoNSubstitute 3.30.8
xunit 2.0.0
xunit.runner.visualstudio 2.0.0
xunit.runner.console 2.0.0
NSubstitute 1.8.2.0

我正在使用 Visual Studio 2013 Update 4 并面向 .NET 4.5.1 Framework

更新:按照建议,我尝试了 TestDriven.NET-3.9.2897 Beta 2。缺少的测试现在正在运行,但似乎仍然存在一些错误。新示例:

[Theory]
[AutoData]
public void MyWorkingTest(string s)
{
    Assert.NotNull(s); // Pass
}

[Theory]
[AutoNSubstituteData]
public void MyBrokenTest(string s)
{
    Assert.NotNull(s); // Fail
}

[Theory]
[AutoData]
public void MyWorkingTestThatIsNowBroken(string s)
{
    Assert.NotNull(s); // Fail even though identical to MyWorkingTest above!
}
Run Code Online (Sandbox Code Playgroud)

MyBrokenTestMyWorkingTestThatIsNowBroken失败Assert.NotNull,而尽管MyWorkingTest与 相同,但仍通过MyWorkingTestThatIsNowBroken。因此,该属性不仅AutoNSubstituteData无法正常工作,而且还会导致下游测试行为异常!

Update2:AutoNSubstituteDataAttribute更改to的定义public而不是internal修复所有问题。xunit runner 现在可以像 TestDriven.Net 一样发现并通过所有测试。对这种行为有什么想法吗?是预期的吗?

Eri*_*ler 5

xUnit Visual Studio 运行程序和 TestDriven.Net 运行程序都会导致这些奇怪的问题,因为AutoNSubstituteDataAttribute类和构造函数是internal. 更改这些即可public解决所有问题。如果该属性被忽略,我会期望出现如下错误:System.InvalidOperationException : No data found for ...

AutoNSubstituteData这并不能解释为什么下游测试会受到完全不同测试的违规属性的影响。在这种情况下,单元测试运行者似乎应该更加健壮。

为了完整起见,这里是以下的工作实现AutoNSubstituteDataAttribute

public class AutoNSubstituteDataAttribute : AutoDataAttribute
{
    public AutoNSubstituteDataAttribute()
        : base(new Fixture().Customize(new AutoNSubstituteCustomization()))
    {
    }
}
Run Code Online (Sandbox Code Playgroud)