Nunit-C#: run specific Tests through coding

后端 未结 2 1984
刺人心
刺人心 2021-01-03 02:16

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

CoreExtensions.Host.InitializeService();
TestPackage testPack         


        
相关标签:
2条回答
  • 2021-01-03 02:38

    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.

    0 讨论(0)
  • 2021-01-03 02:41

    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);
    
    0 讨论(0)
提交回复
热议问题