docs/articles/nunit/writing-tests/attributes/setupfixture.md
This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures in a given namespace including nested namespaces below, within an assembly.
The class may contain at most one method marked with the OneTimeSetUpAttribute and one method marked with the OneTimeTearDownAttribute.
There are a few restrictions on a class that is used as a setup fixture.
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. In the examples below, the method RunBeforeAnyTests() is called before any tests or setup methods in the NUnit.Tests namespace. The method RunAfterAnyTests() is called after all the tests in the namespace as well as their individual or fixture teardowns have completed execution.
Multiple SetUpFixtures may be created in a given namespace. The order of execution of such fixtures is indeterminate.
using System;
using NUnit.Framework;
namespace NUnit.Tests
{
[SetUpFixture]
public class MySetUpClass
{
[OneTimeSetUp]
public void RunBeforeAnyTests()
{
// ...
}
[OneTimeTearDown]
public void RunAfterAnyTests()
{
// ...
}
}
}
With respect to the order of execution of setup (also one-time setup) it's deterministic between namespaces (including nested namespaces) but non-deterministic if you have two setups at the same level, e.g. two methods in the same class marked [SetUp]. That's, however, a usage that should only come up in limited situations and is easy to avoid.
The defined order is as follows...
[Items 6 and 7 rarely come into play but the features are available for situations like code generation, where it may be more convenient to have multiple setup fixtures and/or methods.]
About filtering on SetUpFixtures:
A SetUpFixture is normally not used for filtering tests. However, if that is done, one should be aware that a
SetUpFixture encapsulates all tests to which it belongs.
If placed in a namespace it will encapsulate all tests in that namespace and contained namespaces.
If placed on the assembly level, it will encapsulate all tests in the assembly.
Prior to NUnit 3.0, SetUpFixture used the SetUp and TearDown attributes rather than OneTimeSetUp and
OneTimeTearDown. The older attributes are no longer supported in SetUpFixtures in NUnit 3.0 and later.