XUnit.net在运行后立即捕获每个测试的结果

Hyd*_*axy 7 c# unit-testing xunit.net

我试图使用XUnit.net作为自定义本土测试调度程序的替代品.本地调度程序的一个特性是,对于长时间运行的测试,它会在测试通过后立即将测试结果(通过/失败以及导致失败的异常)输出到数据库失败.

因为可能存在大量长时间运行的测试,所以在运行过程中查看测试进度非常有用(在完成所有测试后,不必等待完整的测试通过,直到看到所有结果为止可能需要时间).

XUnit.net源代码来自:https://github.com/xunit/xunit

我看了一眼并看到了BeforeAfterTestAttribute,但是"After"方法并没有提供对测试结果的访问,只是对测试方法的访问.我想要类似的东西,但也可以访问测试结果,这样我就可以立即向数据库报告结果(而不是等待完整的测试套件完成).

似乎(从源代码)可以访问实际测试结果的唯一东西是TestRunner,但据我所知,测试运行器没有可扩展性模型.

我提出的一个可能的解决方法如下:

[Fact]
TestMethod()
{
    //This method takes a lambda, handles exceptions, uploads to my
    //database, and then rethrows.
    RunTestWithExtraLogging(() => 
    {
        //Actual test goes here
    }
}
Run Code Online (Sandbox Code Playgroud)

上述解决方案并不理想,因为它要求每个测试的作者调用"RunTestWithExtraLogging"方法.

PS:我愿意考虑一个不同的测试框架(xUnit.net除外),如果它支持这个...

Ale*_*lex 5

是的,我相信你需要创建自己的跑步者.下面提供了一个关于如何完成此操作的简单示例(您需要在TestMethodRunnerCallback方法中添加所需的逻辑:

namespace MyTestRunner
{
    using System;
    using System.Linq;
    using Xunit;        

    public class Program
    {
        public static int Main(string[] args)
        {
            var testAssembly = TestAssemblyBuilder.Build(
                new ExecutorWrapper(args[0], null, false));

            var tests = testAssembly.EnumerateTestMethods(x =>  x
                .DisplayName
                .ToLowerInvariant();

            var testMethods = (args.Length > 1 && !string.IsNullOrEmpty(args[1])
                ? tests.Contains(args[1].ToLowerInvariant())).ToList()
                : tests.ToList();

            if (testMethods.Count == 0)
                return 0;

            var runnerCallback = new TestMethodRunnerCallback();
            testAssembly.Run(testMethods, runnerCallback);

            return runnerCallback.FailedCount;
        }

        public class TestMethodRunnerCallback : ITestMethodRunnerCallback
        {
            public int FailedCount { get; private set; }

            public void AssemblyFinished(TestAssembly testAssembly, int total, int failed, int skipped, double time)
            {
                FailedCount = failed;
            }

            public void AssemblyStart(TestAssembly testAssembly)
            {
            }

            public bool ClassFailed(TestClass testClass, string exceptionType, string message, string stackTrace)
            {
                return true;
            }

            public void ExceptionThrown(TestAssembly testAssembly, Exception exception)
            {
            }

            public bool TestFinished(TestMethod testMethod)
            {
                return true;
            }

            public bool TestStart(TestMethod testMethod)
            {
                return true;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑: 似乎在xUnit.net 2.0中TestAssemblyBuilder已被替换XunitFrontController.下面的代码显示了在测试运行时如何捕获测试传递结果的片段:

public class Program
{
    public static void Main(string[] args)
    {
        string assemblyPath = @"path";
        XunitFrontController controller = new XunitFrontController(
            assemblyPath);

        TestAssemblyConfiguration assemblyConfiguration = new TestAssemblyConfiguration();

        ITestFrameworkDiscoveryOptions discoveryOptions = TestFrameworkOptions.ForDiscovery(assemblyConfiguration);
        ITestFrameworkExecutionOptions executionOptions = TestFrameworkOptions.ForExecution(assemblyConfiguration);

        IMessageSink messageSink = new CustomTestMessageVisitor<ITestMessage>();

        Console.WriteLine("Running tests");

        controller.RunAll(
            messageSink: messageSink,
            discoveryOptions: discoveryOptions,
            executionOptions: executionOptions);
    }
}

public class CustomTestMessageVisitor<T> : TestMessageVisitor<T> where T : ITestMessage
{
    protected override bool Visit(ITestFinished message)
    {
        Console.WriteLine("Test {0} finished.",
            message.Test.DisplayName);
        return true;
    }

    protected override bool Visit(ITestPassed message)
    {
        Console.WriteLine("Test {0} passed", message.Test.DisplayName);
        return true;
    }

    protected override bool Visit(ITestFailed message)
    {
        StringBuilder stringBuilder = new StringBuilder();
        foreach (string exceptionType in message.ExceptionTypes)
        {
            stringBuilder.AppendFormat("{0}, ", exceptionType);
        }

        Console.WriteLine("Test {0} failed with {1}", 
            message.Test.DisplayName, 
            stringBuilder.ToString());
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)