2017-07-11 6 views
2

NUnitには、TestCase属性を持つテストに異なる値を指定できる次の機能があります。キャッチにも同様のことがありますか?C++ Catchには複数のパラメータ/入力オプションを持つNUnitのTestCaseのようなものがあります

[TestCase(12,3,4)] 
[TestCase(12,2,6)] 
[TestCase(12,4,3)] 
public void DivideTest(int n, int d, int q) 
{ 
    Assert.AreEqual(q, n/d); 
} 

同じ単位テストを異なるデータ値で実行する必要がありますが、それぞれ異なる単位テストにする必要があります。 TEST_CASE/SECTIONをコピー/ペーストして値を変更することはできますが、NUnitのようにそれを行うためのきれいな方法があります。

私は何を検索するか把握することは本当に難しいと思っています。 Catchは、NUnitがTestCaseを呼び出すのとはまったく異なる単体テストに対して、TEST_CASEを使用します。

答えて

2

私はかなりあなたが探しているものと同等のものを見つけることができませんでしたが、あなたがすべてですべてをコピー&ペーストする必要はありません。

#include "catch.hpp" 

// A test function which we are going to call in the test cases 
void testDivision(int n, int d, int q) 
{ 
    // we intend to run this multiple times and count the errors independently 
    // so we use CHECK rather than REQUIRE 
    CHECK(q == n/d); 
} 

TEST_CASE("Divisions with sections", "[divide]") { 
    // This is more cumbersome but it will work better 
    // if we need to use REQUIRE in our test function 
    SECTION("by three") { 
    testDivision(12, 3, 4); 
    } 
    SECTION("by four") { 
    testDivision(12, 4, 3); 
    } 
    SECTION("by two") { 
    testDivision(12, 2, 7); // wrong! 
    } 
    SECTION("by six") { 
    testDivision(12, 6, 2); 
    } 
} 

TEST_CASE("Division without Sections", "[divide]") { 
    testDivision(12, 3, 4); 
    testDivision(12, 4, 3); 
    testDivision(12, 2, 7); // oops... 
    testDivision(12, 6, 2); // this would not execute because 
          // of previous failing REQUIRE had we used that 
} 

TEST_CASE ("Division with loop", "[divide]") 
{ 
    struct { 
    int n; 
    int d; 
    int q; 
    } test_cases[] = {{12,3,4}, {12,4,3}, {12,2,7}, 
        {12,6,2}}; 
    for(auto &test_case : test_cases) { 
    testDivision(test_case.n, test_case.d, test_case.q); 
    } 
} 
+0

ありがとうございました。そこに何もないように見えます。私はそれが難しいとは思わない TEST_VARIABLES(int n、int d、int q) TEST_VALUES(n = 12、d = 3、q = 4) TEST_VALUES(n = 12、d = 2、q = 6) TEST_CASE ... フレームワーク内にあります。私はまだコピーとペーストの少しがあると思うが、それは私が今行くつもりです。 – Mochan

関連する問題