timdeschryver/HowToTestYourCsharpWebApi

Run all tests fail

Luigie opened this issue · 3 comments

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();
    }

}

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

Hello Tim,

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

Thx