LoginSignup
0
1

More than 5 years have passed since last update.

xUnitのテストを環境によってスキップする

Posted at

xUnitでは[Fact(Skip="ここはスキップするよ"]のように書けばテストを一時的にスキップできる。
しかし、例えば、ローカルの環境ではテストを実行したいけど、ビルドサーバーではやりたくないとか、OSなどの要因によってあるメソッドが未実装の時には失敗しては欲しくない時もある。

そんなときには、SkippableFactを使うとよい。
NuGetでインストールして、次のように書くだけで、指定したExceptionが発生したときにはスキップ扱いにしてくれる。

[SkippableFact(typeof(NotImplementedException))]
public void DoTest()
{
    // do some tests
}

複数ある場合はExceptionクラスを複数書けばよい。

[SkippableFact(typeof(NotImplementedException), typeof(NotSupportedException))]
public void DoTest()
{
    // do some tests
}

Theoryの場合は同じようにSkippableTheoryを使うだけ。

[SkippableTheory(typeof(NotImplementedException), typeof(NotSupportedException))]
public void DoTest()
{
    // do some tests
}

ただ、残念なことにDisplayNameなどと混在できないのが難点。。。まぁ、いっか。

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