How to Run NUnit Tests Programmatically Using NUnit.Engine Library with Reflection
Tags: testing, dotnet, csharp
Published: 2023-04-17 11:43:41

In this technical blog, we will explore how to use the `NUnit.Engine` library with reflection to run `NUnit` tests programmatically.

`NUnit.Engine` is a library that provides an API for running `NUnit` tests programmatically. It allows you to create an instance of the `TestEngine`, build filters using `TestFilterBuilder`, and execute tests using `TestRunner`. It provides a lot of flexibility and allows you to run tests in different ways, such as by test name, category, or filter.

Let's look at an example of how to use NUnit.Engine with reflection to run NUnit tests programmatically.

```csharp
using nunit.tests;

var engine = TestEngineActivator.CreateInstance();
var package = new TestPackage($"{TestAssemblyReference.AssemblyName}.dll");
var filterBuilder = engine.Services.GetService(typeof(TestFilterService)) as ITestFilterService;
var builder = filterBuilder?.GetTestFilterBuilder();

foreach (var testName in TestAssemblyReference.TestNames)
{
  builder?.AddTest(testName);
}

/*
 * manually set test name and/or filter but category to run by test name(s) or category
 * builder?.AddTest("nunit.tests.ChromeTests.AnotherGoogleTest");
 * builder?.SelectWhere("cat == ui");
 * builder?.SelectWhere("test =~ AmazonTest || test=~ GoogleTest");
 */

var runner = engine.GetRunner(package);
var filters = builder?.GetFilter();

var testResult = runner.Run(listener: null, new TestFilter(filters?.Text));

var testCount = runner.CountTestCases(TestFilter.Empty);
Console.WriteLine($"Total test count: {testCount}");
var explore = runner.Explore(new TestFilter(filters?.Text));
Console.WriteLine("=====================================>");
Console.WriteLine(filters?.Text);
Console.WriteLine(testResult.OuterXml);

Console.WriteLine("=====================================>");
Console.WriteLine("=====================================>");

Console.WriteLine(explore.OuterXml);
runner.StopRun(true);
engine.Dispose();
```
In the above code, we first create an instance of the TestEngine using the TestEngineActivator.CreateInstance() method. We then create a TestPackage object that specifies the name of the assembly containing the tests we want to run. In this example, we use the TestAssemblyReference.AssemblyName property to get the name of the assembly. We then use reflection to get the names of the tests in the assembly using the TestAssemblyReference.TestNames property.

Next, we create an instance of TestFilterBuilder and add each test name to the filter using the AddTest() method. This creates a filter that includes all the tests we want to run. We then create a TestRunner object using the GetRunner() method of the TestEngine object and pass in the TestPackage object and the filter we just created.

We then execute the tests using the Run() method of the TestRunner object. This method returns a TestResult object that contains the results of the test run. We also use the CountTestCases() method to get the total number of test cases and print it to the console.

Finally, we use the Explore() method of the TestRunner object to get detailed information about the tests and print it to the console.

In summary, the above code demonstrates how to use the NUnit.Engine library with reflection to run NUnit tests programmatically. It provides a lot of flexibility and allows you to run tests in different ways, such as by test name, category, or filter. This approach can be particularly useful in scenarios where you need to run tests automatically as part of a build process or other automated pipeline.

Feel free to take a look at the sample code in GitHub: [nunit-test-runner](https://github.com/mhossen/nunit-test-runner)