Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit global initialization - bad idea?

We need some global one time setup code in our test suite. We can do it more than once but it takes quite some time.

  • It's required by all fixtures so [TestFixtureSetUp] does not work. It has to run before all [TestFixtureSetUp] code.

  • Put it in Main() since we keep test assemblies as executables. However Main doesn't get executed under GUI client.

  • Creating a separate class with a static constructor for initialization only works when you reference the class which we do not favor doing in each and every class.

  • Inheriting all test fixtures from a base class and adding a static constructor to it causes multiple calls to the init code.

Now given the circumstances, I have two questions:

1) Is "global setup" a very bad idea that it's not supported by NUnit?

2) What's the least painful, most common way to achieve this?

like image 438
Sedat Kapanoglu Avatar asked Sep 01 '10 15:09

Sedat Kapanoglu


2 Answers

[SetUpFixture]

This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace.

The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution.

Assembly wide initialization. If you don't put the class in any namespace, it will apply to all tests in the assembly.

eg.

// using statements  [SetUpFixture] public class GlobalSetup {   [SetUp]   public void ShowSomeTrace() {     Trace.WriteLine("It works..."); // won't actually trace   } } 

http://www.nunit.org/index.php?p=setupFixture&r=2.4

like image 192
6opuc Avatar answered Oct 02 '22 11:10

6opuc


Starting from NUnit 3.0, the Setup attribute is no longer supported inside classes marked with the SetUpFixture attribute. The current valid syntax is:

  [SetUpFixture]   public class MySetUpClass   {     [OneTimeSetUp]     public void RunBeforeAnyTests()     {       // ...     }      [OneTimeTearDown]     public void RunAfterAnyTests()     {       // ...     }   } 

The OneTimeSetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The OneTimeTearDown method is executed once after all the fixtures have completed execution.

Current SetUpFixture documentation page

like image 40
Leaky Avatar answered Oct 02 '22 11:10

Leaky