2011-06-04 89 views
3

想,我有一個junit測試類:如何編寫動態測試案例

 
class MyComponentTest { 

    private void test(File file) {...} 

    @Test public void test1() {test("test1.txt")} 
    @Test public void test2() {test("test2.txt")} 
    @Test public void test3() {test("test3.txt")} 
} 

test方法從file讀取輸入數據,並與輸入數據測試組件。

如果我更改MyComponentTest會怎麼樣?

class MyComponentTest { 

    private void test(File file) {...} 

    @Test public void testAll() { 
    for (String name: dir.list()) 
     test(new File(name)) 
    } 
}

現在我只有一個測試(testAll),而不是三個測試(test1test2,並test3),因爲它是以前。例如,Runner不會識別三個分開的測試,就像它在以前版本的類中所做的那樣。

我的問題是:如何讓每個test方法調用從junit的角度單獨測試一下?

+1

[JUnit test with dynamic number of tests]的可能重複(http://stackoverflow.com/questions/358802/junit-test-with-dynamic-number-of-tests) – McDowell 2011-06-04 14:08:35

回答