timdeschryver / HowToTestYourCsharpWebApi

54 stars 15 forks source link

Run all tests fail #1

Closed Luigie closed 3 years ago

Luigie commented 3 years ago

Hello Tim, Thanks for the good example for E2E testing.

I have this this issue that running all my tests are failing. individually they are all okay.

image

image

public class ApiWebApplicationFactory : WebApplicationFactory { public IConfiguration Configuration { get; private set; }

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {

        builder.ConfigureAppConfiguration(config =>
        {
            Configuration = new ConfigurationBuilder()
                .AddJsonFile("settings.json")
                .Build();

            config.AddConfiguration(Configuration);
        });

        // will be called after the `ConfigureServices` from the Startup
        builder.ConfigureTestServices(services =>
        {
            services.AddTransient<IDbContextFactory, DbContextFactoryStub>();

        });

        builder.ConfigureTestServices(services =>
        {
            MvcServiceCollectionExtensions.AddControllers(services, options => options.Filters.Add(new AllowAnonymousFilter()));
        });
    }

}

public abstract class IntegrationTestBase : IClassFixture { public readonly Checkpoint Checkpoint = new Checkpoint { SchemasToInclude = new[] { "BLD" }, WithReseed = true, };

    protected readonly ApiWebApplicationFactory factory;
    protected readonly IDbContextFactory dbContextFactory;
    protected readonly HttpClient client;

    public IntegrationTestBase( ApiWebApplicationFactory fixture)
    {
        factory = fixture;
        client = factory.CreateClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetToken());
        dbContextFactory = (DbContextFactoryStub)factory.Services.GetService(typeof(IDbContextFactory));
        Checkpoint.Reset(factory.Configuration.GetSection("ConnectionStrings:SQL").Value).GetAwaiter().GetResult();
    }

}

timdeschryver commented 3 years ago

Hi @Luigie, sadly I can't provide an answer based on the information. Have you tried to debug the second test?

timdeschryver commented 3 years ago

Also, make sure that you don't run your tests in parallel

Luigie commented 3 years ago

Hello Tim,

okay the problem was that the xunit.runner.json file was not existing.

Thx