Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispose not called when running tests from a number of classes in VS

I have a number of xUnit tests across multiple classes. A lot of these classes rely on a setup method being called before each test and a clean up method being called after each test.

In each test class I have a constructor which calls the setup method and a dispose method like the following to clean up:

public override void Dispose()
{
    Cleanup();
}

Each test class extends a base class, which extends a class which inherits IDisposable.

When I run the tests in an individual test class all the tests run fine and the dispose method is called correctly.

When I run all the tests in my solution using the test explorer in Visual Studio, a lot of the tests are failing because the clean up method isn't being called. When I debug the tests I'm not seeing dispose being called.

I installed the xunit.runner.visualstudio NuGet package to get the tests to run in Visual Studio 2019. I'm using xUnit 2.4.1.

Does anybody know why dispose is not being called when I run all the tests at once?

like image 817
richarth Avatar asked Dec 03 '25 00:12

richarth


1 Answers

it does create some shared state which needs to be initialised before each test and cleaned up afterwards

xUnit executes tests of different classes in parallel by default. Because you are using shared state between tests you need to execute all tests sequentially.

For executing tests sequentially, group all tests which using shared state into one "Test Collection".

From docs

When to use: when you want to create a single test context and share it among tests in several test classes, and have it cleaned up after all the tests in the test classes have finished.

https://xunit.net/docs/shared-context

like image 133
Fabio Avatar answered Dec 04 '25 15:12

Fabio