David Huemer
David Huemer

Reputation: 85

NUnit global setup with different namespaces

Is it possible to have a NUnit OneTimeSetUp for the entire assembly?

In my project I have a BaseTest class which is in the namespace MyProject.Tests.Basics and I have a setup class which contains the [OneTimeSetUp] method. The SetUp class is in the namespace MyProject.Tests

When I run a test that derives from the BaseTest class and is for example in the namespace MyProject.Tests.Data, then the OneTimeSetUp method is not called. But when I change the namespace of my SetUp class to the namespace of the test class, then the method is called just fine, but only for this particular test class of course.

So my question is, how can I implement a SetUp Class that works for the entire assembly?

Here are my CodeSamples:

SetUp class:

namespace MyProject.Tests
{
    [SetUpFixture]
    public class SetUp
    {
        [OneTimeSetUp]
        public void Setup()
        {
            ...
        }
    }
}

BaseTest:

namespace MyProject.Tests.Basics
{
    [TestFixture]
    public abstract class BaseTest
    {
        public BaseTest()
        {
            ...
        }
    }
}

Testclass:

namespace MyProject.Tests.Data.IO.Files.DataFiles
{
    public class ExampleTest: BaseTest
    {
        [Test]
        public void Example()
        {
            ...
        }
    }
}

Upvotes: 2

Views: 976

Answers (1)

Chris
Chris

Reputation: 6062

To do this, have your SetUpFixture class in the global namespace, i.e. don't put it in any namespace.

This is documented in the NUnit docs as below:

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

https://docs.nunit.org/articles/nunit/writing-tests/attributes/setupfixture.html

Upvotes: 2

Related Questions