0

I'm currently trying to setup some tests to check that my code is working. I've managed to set it up with the .NET 6 minimal API and use a test db context instead, however, when I have multiple tests all the tests are failing.

I believe the reason for this is because they are running parallel which is obviously causing a conflict with the database context.

To counteract this, I've tried an in memory database and create a new database based on a guid which is resolved before the test, however, I've read that in memory databases aren't the best for testing as it's not a true reflection. So I've opted to use a test SQL Server. Which I plan on tearing down after each test - I know this may take a lot longer than usual but I'm happy to take that given it's an accurate reflection of the code interacting with the database.

However, for that I think I need to run the tests individually rather than in parallel.

My test is really simple, it just tests a 200 and a count. Running them individually works perfectly.

public class GetUserEndpointTest()
{
  [Fact]
  public async Task OnSuccess_ReturnStatusCodeOk()
  {
    await using var application = new Application();
    var client = application.CreateClient();

    var response = await client.GetAsync("/v1/users");
    response.EnsureSuccessStatusCode();
  }

  [Fact]
  public async Task OnSuccess_ReturnsTheCorrectUserCount()
  {
    await using var application = new Application();
    var client = application.CreateClient();

    var users = await client.GetFromJsonAsync<List<User>>("/v1/users");
    users.Count.Shound().Be(1);
  }  
}

My application factory is just simple:

class Application : WebApplicationFactory<Program>
{
  protected override IHost CreateHost(IHostBuilder builder)
  {
    builder.ConfigureServices(services =>
    {
      services.RemoveAll(typeof(DbContextOptions<ApplicationContext>));
      services.AddDbContext<ApplicationContext>(options =>
        {
          options.UseSqlServer(
            "Server=localhost;Database=testing_database;Trusted_Connection=True;"
          );
        });
     });

     return base.CreateHost(builder);
  }
}
9
  • And... What is your question? Commented Mar 22, 2022 at 19:13
  • I suppose there’s multiple. How do I get the tests so they don’t run in parallel? Is using a database the best approach here? Commented Mar 22, 2022 at 19:15
  • In our company we are starting docker container with DB and running tests on it. To run tests sequentially there is a config in VS, or -m:1 parameter when running dotnet test in CI. Commented Mar 22, 2022 at 19:20
  • Chech this for fast db reset between tests github.com/jbogard/Respawn Commented Mar 22, 2022 at 19:22
  • Also check this for tests settings learn.microsoft.com/en-us/visualstudio/test/… Commented Mar 22, 2022 at 19:24

1 Answer 1

1

When testing with XUnit, you can prevent parallel execution of the tests by putting them in a "collection". You declare which tests belong to the collection by "decorating" the class declaration with a Collection attribute.

This is explained at https://xunit.net/docs/running-tests-in-parallel

For example:

    [Collection("NonParallelTestCollection")]
    public class MyUnitTests1
    {
    // your tests here ...
    }

    [Collection("NonParallelTestCollection")]
    public class MyUnitTests2
    {
    // your tests here ...
    }

In this example, none of the tests in either of the classes will run in parallel.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.