Nunit-C#:通过编码运行特定的测试 [英] Nunit-C#: run specific Tests through coding

查看:40
本文介绍了Nunit-C#:通过编码运行特定的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Selenium与C#用于自动化,并且我想通过以下代码调用NUnit:

I am using Selenium with C# for Automation, and I want to invoke NUnit through code as follows:

CoreExtensions.Host.InitializeService();
TestPackage testPackage = new TestPackage(@"D:\Automation\bin\Debug\Test.dll");
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(testPackage);
//TestFilter filter = new NameFilter(new TestName() { Name = "Test1" });
TestResult testResult = remoteTestRunner.Run(
    new NullListener(),
    TestFilter.Empty,
    false,
    LoggingThreshold.Off
); 

我可以使用以下类别过滤器来运行测试

I am able to run tests using category filter as below

remoteTestRunner.Run(
    new NullListener(),
    new CategoryFilter("MyCat"),
    false,
    LoggingThreshold.Off
);

但是我想执行特定的测试.如何设置套件过滤器?我已经尝试了以下方法,但是它不起作用:

But I want to execute specific tests. How do I set the suite filter? I have tried the following, but it does not work:

TestFilter filter = new NameFilter(new TestName() { Name = "Test1" }); 
TestResult testResult = remoteTestRunner.Run(
    new NullListener(),
    filter,
    false,
    LoggingThreshold.Off
);

如何运行特定的测试以及如何通过代码传递参数?

How do I run specific tests and how do I pass arguments through code?

推荐答案

此示例代码应使您了解开始循环测试并选择要运行的测试所必须执行的操作.我使用了几个数组索引0,但应该在其中循环遍历.

This sample code should give you an idea of what you have to do to start cycling through your tests and selecting which one you want to run. I have used several array indexes of 0 where you should loop through instead.

最重要的是,您必须先实际加载测试,然后才能开始尝试单独运行它们,因为测试需要具有唯一的TestId,该ID只有在加载后才设置.以下代码可以运行并在您的测试框架中的第一个测试夹具中运行第一个测试.它还会扫描所有测试名称,以便您根据某些条件显示或运行

The Jist of it is that you have to actually load the tests before you can start trying to run them individually as the tests need to have a unique TestId which only gets set once they have been loaded. The following code works and runs the first test in the first testfixture in your testing framework. It also scans all the test names for you to either display or run based on some criteria

CoreExtensions.Host.InitializeService();
TestSuiteBuilder builder = new TestSuiteBuilder();
TestPackage testPackage = new TestPackage(@"path.to.dll");
RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
remoteTestRunner.Load(testPackage);
TestSuite suite = builder.Build(testPackage);
TestSuite test = suite.Tests[0] as TestSuite;
var numberOfTests = ((TestFixture)test.Tests[0]).TestCount;

foreach (TestMethod t in ((TestFixture)test.Tests[0]).Tests)
{
    Console.WriteLine(t.TestName.Name);
}

TestName testName = ((TestMethod)((TestFixture)test.Tests[0]).Tests[0]).TestName;
TestFilter filter = new NameFilter(testName);
TestResult result = test.Run(new NullListener(), filter);
ResultSummarizer summ = new ResultSummarizer(result);
Assert.AreEqual(1, summ.ResultCount);

这篇关于Nunit-C#:通过编码运行特定的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆