RequiresThread
RequiresThreadAttribute is used to indicate that a test method, class, or assembly should always run on a separate thread. Optionally, the desired apartment state for the thread may be specified.
Constructors
RequiresThreadAttribute()
RequiresThreadAttribute(ApartmentState apartment)
| Parameter | Type | Description |
|---|---|---|
apartment |
ApartmentState |
The apartment state for the thread. Must be STA or MTA (not Unknown). |
Applies To
| Test Methods | Test Fixtures (Classes) | Assembly |
|---|---|---|
| ✅ | ✅ | ✅ |
Examples
Fixture Level
[TestFixture]
[RequiresThread]
public class FixtureOnSeparateThread
{
[Test]
public void TestRunsOnSeparateThread()
{
// All tests in this fixture run on a dedicated thread
Assert.That(Thread.CurrentThread.IsThreadPoolThread, Is.False);
}
}
Method Level with Apartment State
[TestFixture]
public class MixedThreadTests
{
[Test]
[RequiresThread]
public void TestOnSeparateThread()
{
// This test runs on a separate thread
Assert.Pass();
}
[Test]
[RequiresThread(ApartmentState.STA)]
public void TestOnSTAThread()
{
// This test runs on a separate STA thread
Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.STA));
}
[Test]
[RequiresThread(ApartmentState.MTA)]
public void TestOnMTAThread()
{
// This test runs on a separate MTA thread
Assert.That(Thread.CurrentThread.GetApartmentState(), Is.EqualTo(ApartmentState.MTA));
}
}
Assembly Level
// The whole assembly runs on a separate thread
[assembly: RequiresThread]
Assembly-level use is rarely needed. Runners already execute each test assembly on its own thread, so the attribute adds little at that level. It was more useful in earlier versions of NUnit and is kept for compatibility.
When to Use It
Most test projects never need this attribute. Its intended use is on a test method, or occasionally a fixture, whose code intentionally corrupts the state of the thread it runs on: for example by changing thread-local storage, the thread's culture or principal without restoring it, or by leaving the thread in a state that later tests must not inherit. Running such a test on a dedicated thread, which is discarded afterwards, protects the worker threads that NUnit reuses for other tests.
If you only need a particular apartment state, use the Apartment attribute instead. It
creates a new thread only when the current thread does not already have the requested state.
Notes
- This attribute always creates a new thread, regardless of the current thread's apartment state.
- To create a thread only when the current apartment state is not appropriate, use the
[Apartment]attribute instead. - When used without an
ApartmentStateargument, the thread uses the default apartment state (MTA). - This attribute is useful for tests that modify thread-local state or require isolation from other tests.
- Use on an assembly is rarely useful, since each test assembly already runs on its own thread.