为什么使用 HostType(“Moles”) 的单元测试的断言在单独运行时会通过,但在与一组测试一起运行时会失败?

发布于 2024-09-27 07:06:14 字数 927 浏览 0 评论 0原文

我最近登上了 Pex & 号。鼹鼠的潮流是为了用许多静态、非虚拟、密封等元素来测试一些逻辑。最近,我开始从一些测试中看到我无法解释的行为。

我存根的接口的几个方法返回 void,因此我将存根设置为更新布尔变量以指示它们已被调用的委托。这就是我正在做的事情:

[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);
    }

无论出于何种原因,如果我单独运行此测试,上述断言就会成功。但是,如果我将测试与程序集中的所有其他测试一起运行(使用 Visual Studio 的“运行解决方案中的所有测试”功能),则第一个断言将失败。

我想知道为什么会发生这种情况,以及我需要进行哪些更改才能解决该问题。

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.

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. 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.

I'd like to know why this occurs, and what I need to change to resolve the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

打小就很酷 2024-10-04 07:06:14

这可能只是使用多个线程执行测试的“运行所有测试”的副作用吗?那么,在 Assert 触发时 Dispose() 尚未运行?

尝试使用 ManualResetEvent 阻止测试方法直到 Dispose() 运行?像这样的东西;

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);
}

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?

Try using a ManualResetEvent to block the test method until Dispose() has run? 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文