C# 实体框架 XUnit 测试总是失败
我目前正在尝试设置一些测试来检查我的代码是否正常工作。我已经设法使用 .NET 6 最小 API 进行设置,并使用测试数据库上下文,但是,当我进行多个测试时,所有测试都会失败。
我相信这样做的原因是因为它们是并行运行的,这显然会导致与数据库上下文发生冲突。
为了解决这个问题,我尝试了一个内存数据库,并根据测试前解决的 guid 创建一个新数据库,但是,我读到内存数据库并不是最适合测试的,因为它不是真正的数据库反射。所以我选择使用测试 SQL Server。我计划在每次测试后拆除它 - 我知道这可能比平常花费更长的时间,但我很高兴接受它,因为它准确地反映了与数据库交互的代码。
然而,为此我认为我需要单独运行测试而不是并行运行。
我的测试非常简单,只测试 200 和计数。单独运行它们效果很好。
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);
}
}
我的应用程序工厂很简单:
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 XUnit 进行测试时,您可以通过将测试放入“集合”中来防止测试的并行执行。您可以通过使用
Collection
属性“装饰”类声明来声明哪些测试属于集合。https://xunit.net/docs/running-tests-in-对此进行了解释并行
例如:
在此示例中,任何一个类中的测试都不会并行运行。
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:
In this example, none of the tests in either of the classes will run in parallel.