はじめに
本エントリは「TDD Advent Calendar 2013」の参加エントリです。
8日目は@biacさんの「TDD 最初の一歩 (C#編) #tddadventjp: TDD.NET」でした。
テストケースの分類
TDD限らず自動化テストを行っていると、どんどん増えていくテストケースをいかに整理、管理していくかが課題の一つとなってきます。整理できていないと、テストコードの可読性低下、不要なテストが残ることによるテストの実行速度低下、思いがけないレッド(=テスト失敗)などにつながります。
本エントリではC#およびMSTestを使い、「xxがxxの場合、xxがxxならばxxとなる」を表すテストケースの分類方法をまとめてみます。
テストケース名での分類
一番シンプルなのが、テストケース名で分類する方法です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TddAdventJp2013.Test | |
{ | |
[TestClass] | |
public class テストケース名 | |
{ | |
[TestMethod] | |
public void Aがa1の場合・Bがb1ならばc1となる() | |
{ | |
} | |
[TestMethod] | |
public void Aがa1の場合・Bがb2ならばc2となる() | |
{ | |
} | |
[TestMethod] | |
public void Aがa2の場合・Bがb1ならばc3となる() | |
{ | |
} | |
} | |
} |
メリット
- なんといってもシンプル
デメリット
カテゴリーを使った分類
カテゴリーをつけるための属性を使用する方法です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TddAdventJp2013.Test | |
{ | |
[TestClass] | |
public class カテゴリー | |
{ | |
[TestCategory("Aがa1の場合")] | |
[TestMethod] | |
public void Bがb1ならばc1となる() | |
{ | |
} | |
[TestCategory("Aがa1の場合")] | |
[TestMethod] | |
public void Bがb2ならばc2となる() | |
{ | |
} | |
[TestCategory("Aがa2の場合")] | |
[TestMethod] | |
public void Bがb1ならばc3となる() | |
{ | |
} | |
} | |
} |
メリット
デメリット
- 同じカテゴリーを何度も指定しないといけない
- カテゴリーごとに表示しても階層は2階層止まり
内部クラスを使った分類
テストケースを内部クラスとして定義する方法です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TddAdventJp2013.Test | |
{ | |
public class 内部クラス | |
{ | |
[TestClass] | |
public class Aがa1の場合 | |
{ | |
[TestMethod] | |
public void Bがb1ならばc1となる() | |
{ | |
} | |
[TestMethod] | |
public void Bがb2ならばc2となる() | |
{ | |
} | |
} | |
[TestClass] | |
public class Aがa2の場合 | |
{ | |
[TestMethod] | |
public void Bがb1ならばc3となる() | |
{ | |
} | |
} | |
} | |
} |
メリット
デメリット
- 少々面倒
- 内部クラス名、テストケース名がいわゆる半角数字で始められないなど、C#の言語仕様に制限される
番外:partialクラスの活用
C#はpatialクラスを使うことで、一つのクラスを複数ファイルに分割することができます。これを利用することで、同一テストクラスでも意味のあるまとまりごとにテストケースを分けて記載することが可能になります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TddAdventJp2013.Test | |
{ | |
[TestClass] | |
public partial class partialクラス | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TddAdventJp2013.Test | |
{ | |
public partial class partialクラス | |
{ | |
[TestCategory("Aがa1の場合")] | |
[TestMethod] | |
public void Bがb1ならばc1となる() | |
{ | |
} | |
[TestCategory("Aがa1の場合")] | |
[TestMethod] | |
public void Bがb2ならばc2となる() | |
{ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TddAdventJp2013.Test | |
{ | |
public partial class partialクラス | |
{ | |
[TestCategory("Aがa2の場合")] | |
[TestMethod] | |
public void Bがb1ならばc3となる() | |
{ | |
} | |
} | |
} |
最後に
テストケースを整理するために上にあげた方法は排他的なものではないので、それぞれ組み合わせることももちろん可能です。他にももっと良い分類方法などあれば、ぜひ教えてください。
なお、今回のサンプルコードは、GitHubにあげてあります。
https://github.com/masaru-b-cl/TDDAdventJp2013
次は
10日目はまだ空いているようです。そこのあなた!ぜひ書いてみてはいかがでしょう!
参加登録は「TDD Advent Calendar 2013の参加状況確認・参加登録 – Qiita [キータ]」から!
[2013/12/11追記]
10日目がアップされました! > @Lewuatheさん++
ピンバック: 2013年振り返り by @masaru_b_cl | be free