Entity Framework Core InMemory database tests break when run in parallel

拈花ヽ惹草 提交于 2019-12-07 08:17:04

问题


When running all the test, I'm eventually getting errors saying:

"An item with the same key has already been added. Key: 125"

This doesn't happen when each test is run separately.

The funny thing is that each test works with a different DbName, to avoid any conflict:

[TestMethod]
public void Test1() 
{
    using (var context = CreateTestingContext()) 
    {
        ...
    }
}

[TestMethod]
public void Test2() 
{
    using (var context = CreateTestingContext()) 
    {
        ...
    }
}

protected static SGDTPContext CreateTestingContext([CallerMemberName] string dbName = "TestingDb")
{
    var builder = new DbContextOptionsBuilder<MyDbContext>().UseInMemoryDatabase(dbName);
    return new MyDbContext(builder.Options);
}

It's really strange, because when I run the tests individually, they are Green! and when I run them all at the same time, some eventually fail.

NOTE: I'm using the integrated MSTest from Visual Studio 2017.

来源:https://stackoverflow.com/questions/47335920/entity-framework-core-inmemory-database-tests-break-when-run-in-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!