LoginSignup
0
0

More than 3 years have passed since last update.

テストの組み合わせを洗い出す

Posted at

テストの組み合わせを洗い出すプログラムを書いてみました。
テストケースを削らせてもらえないストイックな現場にいる人に役立つかもしれません。

テストの組み合わせ

例えば、性別「男・女」、職業「学生・会社員・その他」の場合、組み合わせは 2 * 3 = 6 通りになります。下表の組み合わせです。

性別 職業
学生
会社員
その他
学生
会社員
その他

これくらいだとすぐに書き出せるのですが、パラメータが増えて組み合わせが100通り、200通りにもなった場合、書き出すのが困難になってきます(面倒だし、頭が混乱してくる)
面倒なので、テストケースを削りたくなってきますが、削る基準を考えるのも面倒ですし、ストイックな現場だと100、200程度なら全部やれと言われるでしょう。

プログラム

そういうときにテストケースを洗い出すプログラムがあれば便利です。
以下、例として、プリンタの詳細設定の組み合わせを出力するプログラムを書いてみます。
combination関数が組み合わせを出力するプログラムになっています。
HTMLファイルとして保存して、ブラウザで開くと、実行できます。

combination.html
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Combination</title>
  <script>
    var p1 = ['Letter', 'A4'];
    var p2 = ['1', '2', '4', '6', '9', '16'];
    var p3 = ['既定', 'なし', '最小', 'カスタム'];
    var p4 = ['既定', 'カスタム'];
    var p5 = ['ヘッダーとフッター', '背景のグラフィック'];

    var resultBuffer = '';

    combination(p1, p2, p3, p4, p5);

    function combination() {
      combinationInternal(arguments, 0, arguments[0], '');
      document.write('<pre>' + resultBuffer + '</pre>')
    }

    function combinationInternal(allParams, idx, params, result) {
      for (var i = 0; i < params.length; i++) {
        if (allParams.length - 1 === idx) {
          resultBuffer += (result + params[i] + '\r\n');
        } else {
          combinationInternal(allParams, idx + 1, allParams[idx + 1], result + (params[i] + '\t'));
        }
      }
    }
  </script>
</head>
<body>
</body>
</html>

実行結果

実行結果は以下になります。
長いので、途中から省略していますが、2 * 6 * 4 * 2 * 2 = 192パターン出力されます。

実行結果
Letter  1   既定  既定  ヘッダーとフッター
Letter  1   既定  既定  背景のグラフィック
Letter  1   既定  カスタム    ヘッダーとフッター
Letter  1   既定  カスタム    背景のグラフィック
Letter  1   なし  既定  ヘッダーとフッター
Letter  1   なし  既定  背景のグラフィック
Letter  1   なし  カスタム    ヘッダーとフッター
Letter  1   なし  カスタム    背景のグラフィック
Letter  1   最小  既定  ヘッダーとフッター
・・・・・・・・・・(省略)・・・・・・・・・・
0
0
2

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
0