如何在NUnit中通过c#代码运行特定的测试类

时间:2016-02-02 10:38:00

标签: c# nunit

我需要从特定的类中运行单元测试并显示结果。我正在使用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
            };
    }

但是我怎样才能在特定的类中运行单元测试?

1 个答案:

答案 0 :(得分:1)

您对TestFilter.Empty的使用告诉NUnit运行所有测试。相反,使用类的FullName创建并传递SimpleNameFilter。