SingleThreadedAttribute ignored on OneTimeTearDown with ParallelScope set to All or FixturesΒ #2610
Open
Description
NUnit 3.9.0
OneTimeTearDown
from TestFixture with [SingleThreaded]
and [Parallelizable(ParallelScope.Fixtures)]
(or ParallelScope.All
) attributes is not always run on the same thread as previous test methods (OneTimeSetup
, SetUp
, Test
, TearDown
). Thread differs, but the NUnit.Framework.TestContext.CurrentContext.WorkerId
stays the same.
I would expect for SingleThreaded
attribute to force usage of the same thread on whole fixture.
The same code works as expected on NUnit 3.7.1 and stops working on NUnit 3.8.0.
Tested with R# and nunit3-console 3.7.0 test runners.
Sample code:
using System.Threading;
using NUnit.Framework;
namespace Tests.nunittest
{
[TestFixture, SingleThreaded]
[Parallelizable(ParallelScope.Fixtures)] // or ParallelScope.All
internal class TestClass
{
private Thread _setupThread;
[OneTimeSetUp]
public void OTSetup()
{
_setupThread = Thread.CurrentThread;
}
[SetUp]
public void Setup()
{
Assert.That(Thread.CurrentThread, Is.EqualTo(_setupThread));
}
[Test]
public void Test()
{
Assert.That(Thread.CurrentThread, Is.EqualTo(_setupThread));
}
[TearDown]
public void Teardown()
{
Assert.That(Thread.CurrentThread, Is.EqualTo(_setupThread));
}
[OneTimeTearDown]
public void OTTeardown()
{
Assert.That(Thread.CurrentThread, Is.EqualTo(_setupThread)); // failure, not always
}
}
}
AssemblyInfo.cs :
[assembly: LevelOfParallelism(4)]