Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a specific NUnit test from code using NUnit 3 (C#)

I am trying to run tests a second time programmatically, that exist in the same project.

Because if a SetUp of tests was failed then tests and SetUp will run again.

I use Selenium Webdriver to run tests and a lot of then has SetUp pre-actions. For example: a pre-action is to buy a ticket and then a set of tests are run to check if all is OK.

Often my SetUp pre-action fails (error occurred) and then no test are run. I would like to give them a second change, that is, to run them again.

A great solution that might work for me is this SO question, but it requires NUnit 3.

There is no NUnit.Core package in NUnit 3 so the solution shows a lot of errors. I'm also not sure that I have to use it if my tests methods are placed in the same project (so maybe I don't need to load an assembly).

Any ideas of how to run a test method from code if this method is placed in the same project?

like image 782
Denis Koreyba Avatar asked Jan 06 '23 14:01

Denis Koreyba


1 Answers

The missing methods and assemblies are part of the internal implementation of NUnit 2.6.4 and were never intended for public use. The solution you point to is only "great" while it works, but because it uses internal classes and methods, there are no guarantees.

For NUnit 3.0, you are in luck because there is now a public API for use in running tests. By using a public / published API, you gain a guarantee that we will keep it working in the future.

The documentation for the API is now located at Test-Engine-API. Here is a simple example of its use...

ITestEngine engine = TestEngineActivator.CreateInstance();
TestPackage package = new TestPackage("path/to/assembly");
ITestRunner runner = engine.GetRunner(package);
XmlNode result = runner.Run(this, TestFilter.Empty);

You should reference the nunit.engine.api assembly (not nunit.engine) in order to use this. Your class making these calls is assumed to implement ITestEventListener.

like image 96
Charlie Avatar answered Jan 09 '23 05:01

Charlie