xUnit.net Class Fixture 需要即将运行的测试类的名称

ted*_*ode 5 c# xunit.net

我有一个项目,其中多个测试类共享一个基类固定装置。

正如预期的那样,fixture 的构造函数在测试类中的任何测试运行之前运行。

有没有办法让此类固定装置的构造函数知道即将运行的测试类的名称?

背景:我的项目不进行单元测试,而是使用 xUnit.net 来运行替代类型的测试。所有类和测试都将按顺序运行,因此保证一个类在另一个类尝试运行之前完全运行。

这是我希望能够执行的操作的示例:

using Xunit;
using Xunit.Abstractions;

namespace TestingProject
{
    // In my project, the classes are guaranteed to run in order, one after the other.  Tests won't randomly execute between classes.
    // This fixture is used by multiple test classes.  It handles logging test class starts and stops to the db.
    public class SharedTestingClassFixture : IDisposable
    {
        // This will run at the start of every testing class.
        public SharedTestingClassFixture()
        {
            // Need to get name of class that is about to execute, i.e. "TestingClassName"
            var nameOfTestingClass = "";

            // Log name of class that is about to start to the db BEFORE any tests run.
            var logMessage = $"{nameOfTestingClass} class starting";
        }

        // This will run at the end of every testing class.
        public void Dispose()
        {
            // Log class ending to db after all tests in this class have finished.
        }
    }

    public class TestingClassName : TestClassBase, IDisposable, IClassFixture<SharedTestingClassFixture>
    {
        // This will run before each individual test.
        public TestingClassName(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
        {
            // At this point, I need to already have written the class name to the db; BEFORE this constructor is called.
            // The TestClassBase (not shown) constructor then extracts the test name from testOutputHelper and logs to db.
        }

        // This will run after each individual test.
        public void Dispose()
        {

        }

        [Fact]
        public void Test1()
        {
            // Testing code here
        }

        [Fact]
        public void Test2()
        {
            // Testing code here
        }
    }
}
Run Code Online (Sandbox Code Playgroud)