我知道如何从C#代码运行完整的NUnit程序集

TestPackage testPackage = new TestPackage(assemblyName);
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(testPackage);
TestResult testResult = remoteTestRunner.Run(new NullListener(), TestFilter.Empty, false, LoggingThreshold.Error);

但是,我该如何运行单个TestFixtures甚至单个测试?

最佳答案

感谢@Tony Hopkinson,我找到了解决方案。
我只需要创建一个从TestFilter继承的类并覆盖那里的Match函数。

public class SingleTestFilter : TestFilter
{
    private string testName;

    public SingleTestFilter(string TestName)
    {
        testName = TestName;
    }


    public override bool Match(ITest test)
    {
        return test.TestName.Name.Equals(testName);
    }
}

然后使用它作为参数调用remoteTestRunner.Run
remoteTestRunner.Run(new NullListener(), new SingleTestFilter("MyTest"), false, LoggingThreshold.Error);

10-08 07:02