LoginSignup
4
3

More than 5 years have passed since last update.

xUnit.net の使い方(並列実行編)

Last updated at Posted at 2016-09-01
  • 複数のスレッドを利用して、テストクラスがどのように実行されるかを設定する。

テストの並列実行

  • 同じテストクラス内のテストメソッドは、互いに同時には実行されない。
  • 逆に、異なるテストクラス同士は、互いにどういう風に実行されるか分からない。
    • 同時に実行されるかもしれないし、違うかもしれない。

同時に実行しないテストの設定

  • Collection 属性をつけることによって、テストクラス間の同時実行を管理する。
  • 同時に実行されたくないクラスには、Collection 属性をつけ同じ文字列を引数に与える。
[Collection("Our Test Collection #1")]
public class TestClass1
{
    [Fact]
    public void Test1()
    {
        Thread.Sleep(3000);
    }
}

[Collection("Our Test Collection #1")]
public class TestClass2
{
    [Fact]
    public void Test2()
    {
        Thread.Sleep(5000);
    }
}
  • TestClass1TestClass2 は同時には実行されない。

並列実行の初期設定

  • テストプロジェクトの AssemblyInfo.cs に設定をする。
// 
[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]


// テストの同時実行で利用するスレッド数の最大値
[assembly: CollectionBehavior(MaxParallelThreads = n)]


// このアセンブリ内のテストを並列で処理する
// デフォルト : false
[assembly: CollectionBehavior(DisableTestParallelization = true)]

参考資料

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3