title: Seed test data for every test in ASP.NET Core / EF Core / xUnit.NET integration tests
body: <p>I've been following the strategies for setting up tests for an ASP.NET Core 2.2 API using the Microsoft documentation at <a href="https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.2" rel="noreferrer">Integration tests in ASP.NET Core</a>.</p>
  
  <p>To summarize, we extend and customize <code>WebApplicationFactory</code> and use a <code>IWebHostBuilder</code> to setup and configure various services to provide us with a database context using an in-memory database for testing like below (copied and pasted from the article):</p>
  
  <pre><code>public class CustomWebApplicationFactory&lt;TStartup&gt; 
      : WebApplicationFactory&lt;TStartup&gt; where TStartup: class
  {
      protected override void ConfigureWebHost(IWebHostBuilder builder)
      {
          builder.ConfigureServices(services =&gt;
          {
              // Create a new service provider.
              var serviceProvider = new ServiceCollection()
                  .AddEntityFrameworkInMemoryDatabase()
                  .BuildServiceProvider();
  
              // Add a database context (ApplicationDbContext) using an in-memory 
              // database for testing.
              services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt; 
              {
                  options.UseInMemoryDatabase("InMemoryDbForTesting");
                  options.UseInternalServiceProvider(serviceProvider);
              });
  
              // Build the service provider.
              var sp = services.BuildServiceProvider();
  
              // Create a scope to obtain a reference to the database
              // context (ApplicationDbContext).
              using (var scope = sp.CreateScope())
              {
                  var scopedServices = scope.ServiceProvider;
                  var db = scopedServices.GetRequiredService&lt;ApplicationDbContext&gt;();
                  var logger = scopedServices
                      .GetRequiredService&lt;ILogger&lt;CustomWebApplicationFactory&lt;TStartup&gt;&gt;&gt;();
  
                  // Ensure the database is created.
                  db.Database.EnsureCreated();
  
                  try
                  {
                      // Seed the database with test data.
                      Utilities.InitializeDbForTests(db);
                  }
                  catch (Exception ex)
                  {
                      logger.LogError(ex, $"An error occurred seeding the " +
                          "database with test messages. Error: {ex.Message}");
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>In the tests we can use the factory and create a client like so:</p>
  
  <pre><code>public class IndexPageTests : 
      IClassFixture&lt;CustomWebApplicationFactory&lt;RazorPagesProject.Startup&gt;&gt;
  {
      private readonly HttpClient _client;
      private readonly CustomWebApplicationFactory&lt;RazorPagesProject.Startup&gt; 
          _factory;
  
      public IndexPageTests(
          CustomWebApplicationFactory&lt;RazorPagesProject.Startup&gt; factory)
      {
          _factory = factory;
          _client = factory.CreateClient(new WebApplicationFactoryClientOptions
              {
                  AllowAutoRedirect = false
              });
      }
  
      [Fact]
      public async Task Test1()
      {
          var response = await _client.GetAsync("/api/someendpoint");
      }
  }
  </code></pre>
  
  <p>This works fine, but note the call to <code>InitializeDbForTests</code> which sets up some test data for all tests when the services are configured.</p>
  
  <p>I'd like a reasonable strategy for starting every API test with a clean slate, so that tests don't become dependent on each other. I've been looking for various ways to get a hold of <code>ApplicationDbContext</code> in my test methods to no avail.</p>
  
  <p>Would it be reasonable to do integration tests in complete isolation from each other, and how could I approach it using ASP.NET Core / EF Core / xUnit.NET?</p>