NUnitではTestCase属性で、テスト関数にわたす引数が設定出来ます。
TestCase | NUnit Docs のサンプルを例にすると、次です。
[TestCase(12, 3, ExpectedResult=4)] [TestCase(12, 2, ExpectedResult=6)] [TestCase(12, 4, ExpectedResult=3)] public int DivideTest(int n, int d) { return n / d; }
関数に渡す引数と、期待値を書いて上げます。 パラメーターと期待結果の組み合わせが一行でかけて便利です。 NUnitのテストケースを書いたのが10年ぶりぐらいです。 知らないうちに便利になっていました*1。
RSpecで書くとこんな感じでしょうか?
subject { n / d } context do let(:n) { 12 } let(:d) { 3 } it { is_expected.to 4 } end context do let(:n) { 12 } let(:d) { 2 } it { is_expected.to 6 } end context do let(:n) { 12 } let(:d) { 4 } it { is_expected.to 3 } end
あれ?あんまりかわいく書けないですね・・・。 最近は主にRSpecやMinitestでテストを書いていました。 それで、僕はパラメタライズドテストに馴染みがなかったようです。
GitHub - tomykaira/rspec-parameterized: RSpec::Parameterized supports simple parameterized test syntax in rspec. を使うと次のように書けるみたいです。
where(:a, :b, :answer) do [ [12, 3 , 4], [12, 2 , 6], [12, 4 , 3] ] end with_them do it "should do additions" do expect(a + b).to eq answer end end
参考
*1: 2009年リリースの2.5から使えるようになっていたようです。 https://docs.nunit.org/2.5.5/testCase.html