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?
What Is NUnit? NUnit is a unit-testing framework for all .Net languages. Initially ported from JUnit, the current production release, version 3, has been completely rewritten with many new features and support for a wide range of .NET platforms.
Because NUnit is designed to unit test managed code, it does not lend itself to test unmanaged C++ code as easily as for C#, until now! There are several ways to test native (unmanaged) C++ code using NUnit. Because NUnit is designed to unit test managed code, it does not lend itself to test unmanaged C++ code as easily as for C#. Until now.
NUnit Engine covers the NUnit Engine, the central component all test runners are built around. NUnit VS Test Generator covers the Visual Studio extension for generating tests in both NUnit V2 and V3.
NUnit Engine covers the NUnit Engine, the central component all test runners are built around. NUnit VS Test Generator covers the Visual Studio extension for generating tests in both NUnit V2 and V3. NUnit Xamarin Runners covers the NUnit test runners for Xamarin and mobile devices. NUnit Analyzers covers the NUnit Analyzers for NUnit V3.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With