【问题标题】:How to debug theories in xunit tests如何在 xunit 测试中调试理论
【发布时间】:2016-05-29 22:25:48
【问题描述】:

我有大量类似的测试,我使用 MemberData 属性作为理论来实现。如何导航到每个失败的测试用例并对其进行调试?

这里是一个例子:

    public const int A = 2;
    public const int B = 3;
    public const int C = 2;

    public static IEnumerable<object[]> GetTestCases
    {
        get
        {
            // 1st test case
            yield return new object[]
            {
                A, B, 4
            };

            // 2nd test case
            yield return new object[]
            {
                A, C, 4
            };
        }
    }

    [Theory]
    [MemberData("GetTestCases")]
    public void TestMethod1(int operand1, int operand2, int expected)
    {
        // Q: How can I debug only test case, which is failed?
        //...and break execution before exception will be raised
        var actual = operand1 + operand2;
        Assert.Equal(actual, expected);
    }

【问题讨论】:

    标签: c# unit-testing xunit


    【解决方案1】:

    好吧,你可以在TestMethod1中设置条件断点,然后尝试寻找失败的测试用例。但在很多情况下,它并不那么舒服。

    这里有一个技巧可能会有所帮助:

        public const int A = 2;
        public const int B = 3;
        public const int C = 2;
    
        public static IEnumerable<object[]> GetTestCases
        {
            get
            {
                // 1st test case
    
                // This line will be in stack trace if this test will failed
                // So you can navigate from Test Explorer directly from StackTrace.
                // Also, you can debug only this test case, by setting a break point in lambda body - 'l()'
                Action<Action> runLambda = l => l();
    
                yield return new object[]
                {
                    A, B, 4,
                    runLambda
                };
    
    
                // 2nd test case
    
                runLambda = l => l();
    
                yield return new object[]
                {
                    A, C, 4,
                    runLambda
                };
    
                // ...other 100500 test cases...
            }
        }
    
        [Theory]
        [MemberData("GetTestCases")]
        public void TestMethod1(int operand1, int operand2, int expected, Action<Action> runLambda)
        {
            // pass whole assertions in a callback 
            runLambda(() =>
            {
                var actual = operand1 + operand2;
                Assert.Equal(actual, expected);
            });
        }
    

    想法是将目标逻辑和断言放入回调中,并通过在每个测试用例中注入的特殊的类似 lambda 调用它。每个 lambda 都将作为参数传递并在测试方法中调用,因此它将出现在堆栈跟踪中。当某些测试用例将落下时,您可以通过单击相应的行轻松地通过 StackTrace 导航到它(在本例中,它看起来像 'at UnitTestProject1.ExampleTests2.c.b__4_0(Action l)')

    此外,您可以在该测试用例的 lambda 中设置断点,以便对其进行调试并查看数据发生了什么。

    【讨论】:

    • 这还取决于您使用的 IDE,因为每个 xunit 扩展都有必要的支持,让您可以通过 IDE 集成轻松调试理论。
    • 我使用 Visual Studio 和 ReSharper。但是他们不能(或者我没有找到如何)导航到代码行,其中定义了选定测试用例的数据。你能推荐这样的 xUnit 扩展吗?
    猜你喜欢
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2015-04-11
    相关资源
    最近更新 更多