在xUnit测试项目(.NET Core)中完成集成测试后如何关闭Resharper测试运行器?

问题描述 投票:0回答:2

我是集成测试的新手。我的解决方案中有一个 xUnit 项目,其中仅包含一个测试。 这是我的测试的定义:

[Fact]
public async Task ShouldCreateUser()
{
    // Arrange
    var createUserRequest = new CreateUserRequest
    {
        Login = "testowyLogin",
        Password = "testoweHaslo",
        FirstName = "testoweImie",
        LastName = "testoweNazwisko",
        MailAddress = "[email protected]"
    };
    var serializedCreateUserRequest = SerializeObject(createUserRequest);
    
    // Act
    var response = await HttpClient.PostAsync(ApiRoutes.CreateUserAsyncRoute,
        serializedCreateUserRequest);
    
    // Assert
    response
        .StatusCode
        .Should()
        .Be(HttpStatusCode.OK);
}

以及 BaseIntegrationTest 类定义:

public abstract class BaseIntegrationTest
{
    private const string TestDatabaseName = "TestDatabase";
    
    protected BaseIntegrationTest()
    {
        var appFactory = new WebApplicationFactory<Startup>()
            .WithWebHostBuilder(builder =>
            {
                builder.ConfigureServices(services =>
                {
                    RemoveDatabaseContextFromServicesCollectionIfFound<EventStoreContext>(services);
                    RemoveDatabaseContextFromServicesCollectionIfFound<GrantContext>(services);
                    
                    services
                        .AddDbContext<EventStoreContext>(options =>
                            options.UseInMemoryDatabase(TestDatabaseName))
                        .AddDbContext<GrantContext>(options =>
                            options.UseInMemoryDatabase(TestDatabaseName));
                });
            });
        
        HttpClient = appFactory.CreateClient();
    }

    protected HttpClient HttpClient { get; }
    
    protected static StringContent SerializeObject(object @object) =>
        new StringContent(
            JsonConvert.SerializeObject(@object),
            Encoding.UTF8,
            "application/json");

    private static void RemoveDatabaseContextFromServicesCollectionIfFound<T>(IServiceCollection services)
        where T : DbContext
    {
        var descriptor = services.SingleOrDefault(service =>
            service.ServiceType == typeof(DbContextOptions<T>));

        if (!(descriptor is null))
        {
            services
                .Remove(descriptor);
        }
    }
}

当我运行测试时,需要几秒钟,测试成功结束。问题是 Resharper Test Runner 仍然运行,尽管我已经收集了结果。我在这里做错了什么?执行所有测试后,我是否必须以某种方式处置 HttpClient?如果是这样,如何实现?谢谢你的帮助。

c# asp.net-core resharper xunit rider
2个回答
0
投票

看起来您实际上是在测试中启动应用程序,而不是使用测试主机(https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1

public class BasicTests 
    : IClassFixture<WebApplicationFactory<RazorPagesProject.Startup>>
{
    private readonly WebApplicationFactory<RazorPagesProject.Startup> _factory;

    public BasicTests(WebApplicationFactory<RazorPagesProject.Startup> factory)
    {
        _factory = factory;
    }

    [Theory]
    [InlineData("/")]
    [InlineData("/Index")]
    [InlineData("/About")]
    [InlineData("/Privacy")]
    [InlineData("/Contact")]
    public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
    {
        // Arrange
        var client = _factory.CreateClient();

        // Act
        var response = await client.GetAsync(url);

        // Assert
        response.EnsureSuccessStatusCode(); // Status Code 200-299
        Assert.Equal("text/html; charset=utf-8", 
            response.Content.Headers.ContentType.ToString());
    }
}

注意 IClassFixture 的东西。


0
投票

您需要处置您的WebApplicationFactory appFactory。如果您不处理一次性物品,测试运行程序不会停止 这是您的代码的更新版本:

public abstract class BaseIntegrationTest : IAsyncDisposable
{
    private const string TestDatabaseName = "TestDatabase";
    // Keep thins as it is
    
     public async ValueTask DisposeAsync()
    {
        await _factory.DisposeAsync();
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.