简体   繁体   English

为什么使用HostType(“ Moles”)进行的单元测试中的断言在单独运行时可以通过,而在一组测试中运行时却失败?

[英]Why would an assert on a unit test with HostType(“Moles”) pass when run individually, but fail when run with a group of tests?

I recently got aboard the Pex & Moles bandwagon in order to test some logic with many elements that are static, non-virtual, sealed, etc. Recently, I've begun to see behavior I can't explain from a few of the tests. 我最近上了Pex&Moles潮流,用一些静态,非虚拟,密封等元素来测试某些逻辑。最近,我开始看到一些测试无法解释的行为。

A couple of the methods for an interface I stubbed out return void, so I set the stubs to delegates that update boolean variables to indicate that they've been called. 我断的接口的几种方法都返回void,因此我将存根设置为委托,这些委托会更新布尔变量以指示已被调用。 Here's what I'm doing: 这是我在做什么:

[TestMethod]
[HostType("Moles")]
    public void UnitTestX()
    {
        bool disposeCalled = false;
        bool getCalled = false;

        ClassX classX = new ClassX();
        var data = new SIClassXData
                       {
                           Dispose = () => disposeCalled = true,
                           Get = () => getCalled = true,
                           ClassXGet = () => classX
                       };

        MDataLayerFactory.CreateDataLayerObject(() => (IClassXData)data);

        Assert.IsTrue(disposeCalled);
        Assert.IsTrue(getCalled);
    }

For whatever reason, the asserts above succeed if I run this test alone. 无论出于何种原因,如果我单独运行此测试,上述断言都会成功。 But if I run the test along with every other test in the assembly (using Visual Studio's "Run All Tests in Solution" capability), the first assert fails. 但是,如果我将测试与程序集中的其他所有测试一起运行(使用Visual Studio的“在解决方案中运行所有测试”功能),则第一个断言将失败。

I'd like to know why this occurs, and what I need to change to resolve the issue. 我想知道为什么会发生这种情况,以及我需要更改以解决此问题的原因。

Could it just be a side-effect of the 'run all tests' using multiple threads to execute the tests? 难道这只是使用多个线程执行测试的“运行所有测试”的副作用? So, that Dispose() has not run by the time the Assert fires? 那么,在Assert触发时Dispose()尚未运行?

Try using a ManualResetEvent to block the test method until Dispose() has run? 尝试使用ManualResetEvent阻止测试方法,直到Dispose()运行为止? Something like; 就像是;

public void UnitTestX()
{
    // use this to block the test thread until Dispose() is run
    ManualResetEvent mre = new ManualResetEvent(false);

    bool disposeCalled = false;
    bool getCalled = false;

    ClassX classX = new ClassX();
    var data = new SIClassXData
                   {
                       Dispose = () => { 
                          disposeCalled = true; 
                          mre.Set(); // release the test thread now
                       },
                       Get = () => getCalled = true,
                       ClassXGet = () => classX
                   };

    MDataLayerFactory.CreateDataLayerObject(() => (IClassXData)data);

    Assert.IsTrue(mre.WaitOne(1000)); // give it a second to fire
    Assert.IsTrue(disposeCalled);
    Assert.IsTrue(getCalled);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试在一起运行时失败,单独传递 - Unit Tests fail when run together, pass individually 为什么我的测试一起运行时失败,但单独通过? - Why do my tests fail when run together, but pass individually? Flurl&HttpTest:单元测试在全部运行时失败,但在单独运行时通过 - Flurl & HttpTest: Unit tests fail when Run All, but pass when run individually 单元测试在Visual Studio中“全部运行”但单独传递时失败 - Unit Tests Fail when “Run All” in Visual Studio but passes individually 单元测试在“全部运行”时失败,但在单次运行时通过 - Unit tests fail on Run All but pass when are run single 一起运行时单元测试超时,单独运行时成功吗? - Unit Tests timeout when run together, succeed when run individually? MSTest单元测试自行通过,在运行其他测试时失败 - MSTest unit test passes by itself, fails when other tests are run 为什么单元测试在第一次运行时会失败? - Why does unit test fail on first run? 一些 Nunit 测试在组中失败,但在其他环境中单独运行时通过 - Some Nunit tests are failing in groups but pass when run individually in other env 运行所有测试时单元测试失败,但在调试时通过 - Unit Tests failing when I Run All Tests but pass when I Debug
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM