C#中的并行代码单元测试 - 是否有框架/工具/ nunit扩展 [英] parallel code unit tests in C# - is there a framework / tools / nunit extension

查看:94
本文介绍了C#中的并行代码单元测试 - 是否有框架/工具/ nunit扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在执行并行操作时测试组件的行为。我还没有找到帮助我这样做的库/工具箱/单元测试扩展。



我想在并行代码片段中生成精确的执行序列,所以我可以手动执行此操作。但这非常耗费时间和烦人。

I have to test the behavior of components when executing parallel operations. I haven't found a library / toolbox / unit test extension helping me doing this.

I want to produce exact execution sequences in the parallel code fragments, so I could do this with events manually. But this is very time consuming and annoying.

推荐答案

你试试这个吗?

PNUnit [ ^ ]
Do you try this?
PNUnit[^]


事实证明,没有符合我需求的解决方案,我使用了一年中剩下的几天写一个并发测试助手。这是来自单元测试,它显示了使用测试助手类执行,同步和分析的三个并行代码块:



As it turned out, that no solution exists which matches my needs, I used the remaining days of the year to write a concurrency test helper. This is from the unit tests and it shows three parallel code blocks that are executed, synchronized and analyzed using the test helper class:

[Test]
public void SetWaitMultipleEventsTest()
{
    CTestHelper.Run(
        c =>
            {
                Thread.Sleep(1000);
                CTestHelper.AddSequenceStep("s1", "Provide1");
                CTestHelper.SetEvent("e1");
            },
        c =>
            {
                Thread.Sleep(1000);
                CTestHelper.AddSequenceStep("s2", "Provide2");
                CTestHelper.SetEvent("e2");
            },
        c =>
            {
                CTestHelper.WaitAllEvents(new[] { "e1", "e2" });
                CTestHelper.AddSequenceStep("s1", "Consume1");
                CTestHelper.AddSequenceStep("s2", "Consume2");
            },
        TimeSpan.FromSeconds(10));

    Expect(CTestHelper.GetSequence("s1"), Is.EquivalentTo(new[] { "Provide1", "Consume1" }));
    Expect(CTestHelper.GetSequence("s2"), Is.EquivalentTo(new[] { "Provide2", "Consume2" }));
}





CTestHelper.Run()方法并行运行提供的代码块(Actions)并等待它们已完成或已超时。同时提供了同步机制......样品很快就会推出。



资源(到目前为止):

Crawler-Lib并发测试助手主页

Crawler-Lib并发测试助手NuGet包


这篇关于C#中的并行代码单元测试 - 是否有框架/工具/ nunit扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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