开发者

Simple task library question

I'm new to task library. I want to run开发者_开发知识库 some of my unit tests in parallel and test the concurrency issues using task library in .NET 4.0.

As you know TestMethods are parameter-less and return nothing (void) so I need just run one of my tests by N threads concurrently.

[TestMethod()]
void MyTest()
{
    // Do Something
}

It can be done by traditional threads, but wanna use a more robust and managed technique, so:

How to run N number of MyTest() simultaneously using TaskLibrary?

I'm wondering why there's not a built-in attribute for concurrent unit-testing.


[TestMethod()]
void MyTest()
{
    // Do Something
}

[TestMethod()]
void MyTest_4_Times()
{
    Parallel.Invoke(MyTest, MyTest, MyTest, MyTest);
}

or if you want to change the number of concurrent tests by a parameter.

[TestMethod()]
void MyTest_4_Times()
{
    var n = 4;
    Task.WaitAll(Enumerable.Range(0, n).Select(_ => Task.Factory.StartNew(MyTest)).ToArray());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜