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 TestMethod
s 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());
}
精彩评论