Oskar Emil
Oskar Emil

Reputation: 356

The next xUnit test starts to run before previous cleanup in IClassFixture

Using "xunit": "2.2.0-beta4-build3444" with ASP.NET Core I have two integration tests that interact with the same database table and seed some data using IClassFixture:

// FooTestData inserts two rows in TableA
// In FooTestData.Dispose() the rows are deleted
public class FooTest : IClassFixture<FooTestData>
{
    [Fact]
    public void Test()
    {
        var result = GetAllRowsFromTableA()
        // Assert that result.Count == 2
    }

}

// BarTestData inserts one row in TableA
// In BarTestData.Dispose() the row is deleted
public class BarTest: IClassFixture<BarTestData>
{
    [Fact]
    public void Test()
    {
        // Do something
    }
}

FooTest.Test fails because it starts before BarTestData.Dispose() is called. The amount of rows in the database is 3 if I run all tests at once (not in parallell). How do I fix that ?

UPDATE It seems like the tests are run in parallell even if I tell them not to (Not selecting "Run in parallell" in Visual Studio)

Upvotes: 0

Views: 300

Answers (2)

Matus
Matus

Reputation: 437

The link in accepted answer is not working, so I am adding my solution of the problem.

Using xUnit 2.8.1 and visual studio runner.

When I tried to disable parallelization in xunit.runner.json, it did not work. Neither with just

"parallelizeTestCollections": false

nor when adding

"maxParallelThreads": 1

my solution was to use the assembly attribute

[assembly: CollectionBehavior(MaxParallelThreads = 1, DisableTestParallelization = true)]

Upvotes: 0

Oskar Emil
Oskar Emil

Reputation: 356

https://xunit.github.io/docs/running-tests-in-parallel.html

By default xunit > 2.0 runs two tests in paralell regardless of what I tell Visual Studio to do.

Upvotes: 1

Related Questions