jgauffin
jgauffin

Reputation: 101130

Execute tests using the same static object in sequential order

Some of my tests uses a static class which makes it impossible to run them in full isolation.

Therefore I'm wondering if there are a way to tell xunit that some of the tests (in different test classes) in sequential order?

NCrunch has the [ExclusivelyUses("TheStaticClass")] attribute and I'm looking for something similar (so that the build server do not fail on them).

Upvotes: 2

Views: 1153

Answers (1)

m4ttsson
m4ttsson

Reputation: 203

You can probably use the xunit Collection attribute. I have used it when I had a singleton class that could not be used concurrently. Tests in the same collection do not run in parallel. See section "Custom Test Collection" here https://xunit.github.io/docs/running-tests-in-parallel.html.

[Collection("Our Test Collection #1")]
public class TestClass1
{
    [Fact]
    public void Test1()
    {
        Thread.Sleep(3000);
    }
}

[Collection("Our Test Collection #1")]
public class TestClass2
{
    [Fact]
    public void Test2()
    {
        Thread.Sleep(5000);
    }
}

It might also be a good idea to use a custom collection fixture for custom cleanup after all the tests in the collection has been run.

Upvotes: 1

Related Questions