从C#代码运行单个TestFixtures

时间:2013-01-30 16:11:21

标签: c# nunit

我知道如何从C#Code

运行完整的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甚至单次测试?

1 个答案:

答案 0 :(得分:1)

感谢@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);