Nunit-C#: run specific Tests through coding

大兔子大兔子 提交于 2019-12-19 03:15:54

问题


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?


回答1:


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.

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



回答2:


Here is my working code....

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

Thanks to all for your support, kishore.



来源:https://stackoverflow.com/questions/16216011/nunit-c-run-specific-tests-through-coding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!