如何通过 NUnit 中的 C# 代码 运行 特定测试 class

How to run specific test class via c# code in NUnit

我需要 运行 来自特定 class 的单元测试并显示结果。我正在使用 NUnit。

我可以 运行 汇编中的所有单元测试:

 public IntegrationTestsResults Run(Assembly assembly)
    {
        CoreExtensions.Host.InitializeService();

        TestPackage testPackage = new TestPackage(new System.Uri(assembly.CodeBase).LocalPath);
        SimpleTestRunner testRunner = new SimpleTestRunner();
        testRunner.Load(testPackage);

        TestResult testResult = testRunner.Run(new NullListener(), TestFilter.Empty, false, LoggingThreshold.All);

        var formatted = this.FormatResults(testResult, new List<TestResult> {});
        return new IntegrationTestsResults
            {
                status = (IntegrationTestResultState)testResult.ResultState,
                time = testResult.Time,
                items = formatted,
                passed =
                    formatted.SelectMany(t => t.Results.Cast<TestResult>())
                        .Count(r => r.ResultState == ResultState.Success || r.ResultState == ResultState.Ignored),
                total = formatted.SelectMany(t => t.Results.Cast<TestResult>()).Count(),
                isSuccess = testResult.ResultState == ResultState.Success
            };
    }

但是我如何才能 运行 仅在特定 class 中进行单元测试?

您对 TestFilter.Empty 的使用告诉 NUnit 运行 所有测试。相反,使用 class.

的全名创建并传递一个 SimpleNameFilter