How to create global initialization in NUnit 3.2?

I am trying to develop a test suit to run global initialization before all my trials. We can assume that there are tests in different classes and namespaces. In the NUnit documentation, I found only the OneTimeSetUp attribute, but it only works for tests in the same namespace.

So, I developed using inheritance. all of my test classes inherit the base test class, where its constructor performs global initialization (using a static variable to check if it was initialized or not initialized), and the same thing with a global break in the destructor.

Using it, I could create my own script. But when the test suit works, the base test class creates new objects because there are tests in different classes and namespaces. This causes an excess number in the system, and the following tests run slowly: the first test runs after 50 seconds, and the other (which does the same, but in a different namespace) runs after 120 seconds.

There is a better way to create global initialization and global shutdown without affecting test performance

+5
source share
1 answer

You are correct that OneTimeSetUp only works for tests in the same namespace, but since SetUpFixture documentation notes:

A SetUpFixture outside of any namespace provides SetUp and TearDown for the entire assembly.

So the class will look like this:

 using System; using NUnit.Framework; [SetUpFixture] public class TestInitializerInNoNamespace { [OneTimeSetUp] public void Setup() { /* ... */ } [OneTimeTearDown] public void Teardown() { /* ... */ } } 
+10
source

Source: https://habr.com/ru/post/1245566/


All Articles