0

JUnit を学習していて、メソッドを複数回テストする必要があります。2 つのパラメーター (環境とケース番号) に基づいてメソッドをテストする必要があります。同じ Case# が異なる環境間で同じ結果をもたらすかどうかを確認する必要がある 2 つの環境で作業しています。これはテストケースです:

public class AppStartTest
{

/**
 * Test method for {@link archive.AppStart#beginOper(java.lang.String)}.
 */

List<String> actualSections = new ArrayList<String>();
List<String> environments = new ArrayList<String>();
List<String> cases = new ArrayList<String>();

@Before
public void prepareTest()
{
    environments.add("env1");
    environments.add("env2");
    cases.add("case1");
    //cases.add("case2");
    cases.add("case3");
    cases.add("case32");
    cases.add("case4");
    cases.add("emp3");

}

@Test
public void testBeginOper()
{
    for (String caseStr : cases)
    {
        @SuppressWarnings("unchecked")
        Map<String, Integer> mapList[] = new HashMap[2];
        int i = 0;
        for (String env : environments)
        {
            System.out.println("Starting " + env + "-" + caseStr);
            AppStart a = new AppStart();
            mapList[i] = a.beginOper(env + "-" + caseStr, true);
            printMap(mapList[i++]);             
        }
                    //Using assert in this method
        compareResults(mapList[0], mapList[1], caseStr);
    }
}   
}

結果は単一のテストケースとして得られますが、結果を次のように要求します。

testBeginOper[0]
testBeginOper[1]
testBeginOper[2]
.....

パラメーター化されたテスト ケースを使用しようとしましたが、テスト メソッドは独立して実行されます。2 つの環境間で結果を比較する必要があり、@Test を使用してメソッドが何らかの値を返し (メソッドは void である必要があります)、アサートする必要があります。お知らせ下さい。

4

1 に答える 1

0

私が考えることができる最も簡単な解決策は、まったくtestBeginOper()注釈を付けず@Testに、標準の、非テストの、値を返す、パラメーターを受け入れる関数にすることです。次に@Test、さまざまなバージョンの を実行testBeginOper()し、結果を収集して比較する 1 つの注釈付き関数があります。次のようになります。

private Map<String, Integer> testBeginOper(String environment, String case) {
    // do your actual tests using the given parameters "environment" and "case"
}

@Test
public void runTests() {
    List<Map<String, Integer>> results = new ArrayList<>();
    for(String environment : environments) {
        for(String case : cases) {
            results.add(testBeginOper(environment, case));
        }
    }
    // now compare your results
}

もちろん、独自の TestRunner を作成するという別の方法もありますが、これがおそらく最も簡単な解決策です。

于 2014-06-26T21:21:37.280 に答える