MSTest 同时执行我的所有测试会破坏测试 - 该怎么办 [英] MSTest executing all my tests simultaneously breaks tests - what to do

查看:29
本文介绍了MSTest 同时执行我的所有测试会破坏测试 - 该怎么办的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这很烦人.

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.

有人有什么想法吗?

推荐答案

您可能想尝试使用 Monitor 并进入 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 同时执行我的所有测试会破坏测试 - 该怎么办的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆