Daily Usage Issues

Isolated attribute not always respected

Started by GreenMoose on 4,521 views

[v3.7.0.4]
I use IsolatedAttribute on fixture level to ensure my tests are all running inside same process (I use a login routine once during setup, which I don't want to redo more than once for all tests), but it seems every other run it runs the 2 tests in 2 different projects?

Repro:

[TestFixture]
[Isolated]
internal class FooFixture
{
private static int _foo;
private static readonly object _fooLock=new object();

[SetUp]
public void FooFixtureSetUp()
{
lock (_fooLock)
{
if (_foo == 0)
_foo = 1;
else
Assert.Fail("Foo was 1.");
}
}
[Test]
public void Test1()
{
}[Test]
public void Test2()
{
}
}


Pin Test1 and Test2, hide all other tests, select "run all tests visible here" command. For me every other time there is 1 red and 1 green test (expected), and every other run 2 green (not expected). I notice that when I get the unexpected result NCrunch starts 2 TestHost test runners simultaneously.

Bonus question: Can I somehow keep this "isolated" test runner active, so I can reuse it if running tests with "run covering tests in existing project" ?

Thanks.

*Edit: This was probably wrong approach overall, I changed this to use a local cookies cache instead so it doesn't matter which test runner is used.

Edited

Hi, thanks for posting.

The Isolated attribute basically marks a test or fixture as being run in isolation. When this is applied to a fixture, tests within the fixture are considered to be 'Isolated by fixture', which means that they CAN be run in the same process, but there is no code that explicitly requires them to do so. This means that if you have two tests isolated under the same fixture, you can be sure that they will always be run in isolation, but they may still run in different processes.

I think what you're after here is an 'Atomic' fixture rather than an isolated one. 'Atomic' fixtures are flagged so that all tests within the fixture must be run together and cannot be separated. This is quite an important concept internally for NCrunch, as certain situations (such as execution of MSpec tests) do require it. There have always been plans for NCrunch.Framework.AtomicAttribute so that users would have the capability to mark these out where they're needed. So far, I just haven't quite gotten around to implementing it yet. I think it's a useful concept and it would cleanly handle situations like the one you've just described.

Post a reply

Log in to reply.