5

我正在使用 Selenium 和 C# 进行自动化,我想通过代码调用 NUnit,如下所示:

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
); 

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

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

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

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

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

4

2 回答 2

6

此示例代码应该让您了解开始循环测试并选择要运行的测试所需执行的操作。我已经使用了几个 0 的数组索引,您应该在其中循环。

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

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);
于 2013-05-10T09:58:14.540 回答
0

这是我的工作代码....

    SimpleNameFilter filter = new SimpleNameFilter()

    foreach (DataRow DR in DT.Rows)
    {
    string Test = "FullNameOftheTest";
    filter.Add(Test); 
    }

  CoreExtensions.Host.InitializeService();
  TestPackage testPackage = new TestPackage(@"D:\Test\Test.dll");
  RemoteTestRunner remoteTestRunner = new RemoteTestRunner();
  remoteTestRunner.Load(testPackage);
  TestResult result = remoteTestRunner.Run(new NullListener(), filter, true,          LoggingThreshold.All);
  ResultSummarizer summaryResults = new ResultSummarizer(result);

感谢大家的支持,基肖尔。

于 2013-05-20T12:41:39.887 回答