【问题标题】:AutoFixture Tests Stop Showing Up in Test RunnerAutoFixture 测试停止显示在测试运行器中
【发布时间】:2021-01-16 18:10:51
【问题描述】:

AutoFixture(或我对它的误用)似乎导致 xunit 测试运行器停止在树视图中为每个内联数据实例显示单独的测试。通常,如果我使用 [Theory] ​​和 [InlineData],我会在测试运行器树视图中显示单独的测试,每个 [InlineData] 一个。在以下之后不再发生这种情况:

我创建了一个自定义 AutoMoqDataAttribute 类以始终使用 AutoMoqCustomization 并禁用递归:

public sealed class AutoMoqDataAttribute : AutoDataAttribute
{
    /// <summary>
    /// Automaticaly adds the "Auto Moq" customization to fixures that use the
    /// AutoDataAttribute attribute.
    /// </summary>
    public AutoMoqDataAttribute() : base(() =>
    {
        // Create a fixture we can customize.
        var fixture = new Fixture();

        // Remove the recursion checks as we have objects that require recursion.
        var throwingRecursionBehavior = fixture.Behaviors.FirstOrDefault(behavior => behavior.GetType() == typeof(ThrowingRecursionBehavior));
        if (throwingRecursionBehavior != null) fixture.Behaviors.Remove(throwingRecursionBehavior);

        // Have to also add in the OmitOnRecursionBehavior to avoid a stack overflow error
        // presumably, since we removed the above, and without this, AutoFixture follows every
        // object and just keeps trying to auto fill its data. By following the circular
        // references it just goes on forever.
        fixture.Behaviors.Add(new OmitOnRecursionBehavior());

        // Use the "Auto Moq" customization so we have AutoFixture automatically mock objects for us.
        fixture.Customize(new AutoMoqCustomization());

        return fixture;
    }) { }
}

然后,当我使用 Theory/InlineData 时,我希望它能够工作,所以我也创建了这个:

    public class InlineAutoMoqDataAttribute : InlineAutoDataAttribute
    {
        public InlineAutoMoqDataAttribute(params object[] values) : base(new AutoMoqDataAttribute(), values) { }
    }

最后,我尝试运行一些测试:

    public class TestClass
    {
        public bool Val { get; set; }
    }

    [Theory]
    [InlineData("blah 1", true)]
    [InlineData("blah 2", true)]
    public void Test1(string dummy, bool expected)
    {
        Assert.Equal(expected, true);
    }

上面的工作正常......但后来我运行这个:

    [Theory]
    [InlineAutoMoqData("blah 1", false)]
    [InlineAutoMoqData("blah 2", false)]
    public void Test2(string dummy, bool expected, TestClass sut)
    {
        Assert.Equal(expected, sut.Val);
    }

测试运行,但我没有像往常一样在测试运行器中看到两个测试,每个 InlineAutoMoqData 一个。如果我单击测试并查看“测试详细信息摘要”面板,我可以看到它们,这几乎已经足够了,除非我不能从那里重新运行失败的测试(我的意思是针对单个 InlineData 案例的测试)。我可以从树视图中看到,但同样,它们没有出现在那里,所以这是一个大问题,因为它运行所有 InlineAutoMoqData 测试,我想隔离并运行例如一个失败的。

如果我添加这个属性:

[DataDiscoverer("Xunit.Sdk.InlineDataDiscoverer", "xunit.core")]

对于我的 InlineAutoMoqDataAttribute,单个测试(每个 InlineAutoMoqData 一个)显示在测试资源管理器中,但随后出现此错误:

“InvalidOperationException : 测试方法需要 2 个参数值,但提供了 1 个参数值。”

意思是 AutoFixture 自动模拟值不起作用。

有没有什么方法可以让我的自定义 InlineAutoMoqData 属性仍然在测试资源管理器中显示每个单独的 InlineAutoMoqData 测试?这是我应该向 AutoFixture 报告的错误,还是我做错了什么?

【问题讨论】:

    标签: c# unit-testing autofixture


    【解决方案1】:

    这是 AutoFixture 中的一个已知问题,但我不确定 GitHub 存储库中是否存在正式问题。如果没有,欢迎您创建一个问题并在那里跟踪它的进度。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    相关资源
    最近更新 更多