问题描述
好的,这很烦人。
MSTest同时执行我的所有测试,导致其中的一些失败。这不是因为我的测试是脆弱的和容易构建顺序,而是因为这是一个演示项目,其中我使用Db4o对象数据库运行从文件。
MSTest executes all of my tests simultaneously which causes some of them to fail. No this is not because my tests are fragile and susceptible to build order rather it is because this is a demo project in which I use a Db4o object database running from a file.
所以我有几个DataAccess测试检查我的存储库工作正常和繁荣,MSTest爆炸。因为它试图同时运行所有的测试,当一个测试尝试访问数据库文件而其他测试正在使用它时会收到一个错误。
So I have a couple of DataAccess tests checking that my repositories work correctly and boom, MSTest blows up. Since it tries to run all its tests at the same time it gets an error when a test tries to access the database file while other tests are using it.
一个快速的方法呢?我不想沟渠MSTest(确定我做了,但另一个故事),我肯定,因为他不想运行一个成熟的数据库服务,所以我会采取任何方式强制MSTest不同时运行或技巧
Can anyone think of a quick way around this? I don't want to ditch MSTest (ok I do but another story) and I sure as heck don't want to run a full-blown database service so I'll take any way to force MSTest not to run simultaneously or tricks with opening files.
任何人都有任何想法?
推荐答案
想尝试使用并输入TestInitialize并在TestCleanup上退出。如果你的测试类都依赖于外部文件,你需要为它们使用一个锁对象。
You might want to try using a Monitor and entering in TestInitialize and exiting on TestCleanup. If your test classes all depend on the external file, you'll need to use a single lock object for all of them.
public static class LockClass
{
public static object LockObject = new object();
}
...
[TestInitialize]
public void TestSetup()
{
Monitor.Enter(LockClass.LockObject);
}
[TestCleanup]
public void TestCleanup()
{
Monitor.Exit(LockClass.LockObject);
}
这将强制所有的测试连续运行,你的测试通过/失败,他们应该运行。如果其中任何一个抛出意外的异常,所有其余的将挂起,因为退出代码将不会运行测试爆炸。
This should force all of your tests to run serially and as long as all of your tests pass/fail they should run. If any of them throws an unexpected exception, though, all the rest will hang since the Exit code won't be run for the test that blows up.
这篇关于MSTest同时执行我的所有测试打破测试 - 该怎么办的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!