0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

xUnitで特定のテストケースだけを実行する

Posted at

概要

xUnitで[Trait]属性とfilterオプションを使い、特定のテストケースだけを実行する。

手順

1. ソリューションを作成する

2. xUnitで単体テストを書く

UnitTest1.cs

// [Trait]属性でテストにキーが"Category"で値が"Quick"を付与
[Trait("Category", "Quick")]
[Fact]
public void TestFact()
{
  var actual = app.Calc(5, 6);
  Assert.Equal(11, actual);
}

// [Trait]属性でテストにキーが"Category"で値が"Full"と、キーが"Priority"で値が"High"を付与
[Trait("Category", "Full"),Trait("Priority", "High")]
[Theory]
[InlineData(5, 6, 11)]
[InlineData(-1, 3, 2)]
[InlineData(-5, -8, -13)]
public void TestTheory(int x, int y, int expected)
{
  var actual = app.Calc(x, y);
  Assert.Equal(expected, actual);
}

[Trait]属性

  • [Fact]属性や[Theory]属性で使用する。
  • [Trait]を記述したテストに属性を付与する。
    • 第1引数:キー、第2引数:値を指定する

3. 単体テストを実行する

dotnet test --filter "Category=Quick"
  • dotnet testコマンドでテスト実行する
  • filterオプションを使うことで、特定のテストだけを実行できる。
    • 例: キーが"Category"で値が"Quick"のテストだけを実施する。

フィルタ式の他の記法

#AND条件
dotnet test --filter "Category=Full&Priority=High"

#OR条件
dotnet test --filter "Category=Quick|Category=Full"

#否定
dotnet test --filter "Category!=Quick"

参考資料

選択した単体テストの実行

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?