簡體   English   中英

類似的jUnit測試用例是否應該駐留在同一測試中?

[英]Should similar jUnit test cases reside within the same test?

好的,所以我有一個剛剛完成的課程作業問題單,我知道它工作正常,但是我想像一些測試實踐一樣為此編寫一些jUnit測試。 這引發了一些問題。 我編寫了一個名為count的函數,該函數接受一個int n並將所有從1 to n值求和,然后將其返回。 因此,我為此編寫了4個測試用例,並假定它們都應屬於同一測試方法之內:

@Test
    public void testCount() {
        assertEquals(0, SimmonsPS5.count(0)); // 1 obj in table with name => 0 permutations
        assertEquals(1, SimmonsPS5.count(1)); // 2 obj in table with name => 1 permutation
        assertEquals(3, SimmonsPS5.count(2)); // 2 obj in table with name => 3 permutations (3c2)
        assertEquals(5050, SimmonsPS5.count(100)); // 100 obj in table with name => 100c2 permutations (100(100+1)/2)
    }

但是,如果一個測試未通過(這可能意味着它們全部都通過了測試),那么錯誤報告程序只會說一個測試失敗了,這是不正確的。

接下來,我在類中的main方法實例化該類,運行一些函數,然后打印到stdout main方法通過首先輸入一個參數(文件名),然后讀取文件並對文件進行一些操作來執行此操作。 因此,我想測試它是否為每個文件輸出正確的數據-這些測試用例中的每一個都應該位於相同的測試方法中,還是位於單獨的測試方法中? 我選擇了單獨的方法,但是僅出於唯一的原因,我覺得這些方法比以前的方法更容易失敗。 因此,如果這是測試它的正確方法,那么應該調用什么測試方法? testMain1testMain2等? 什么是“可接受的”術語?

我的main測試用例:

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

@Before
public void setUpStreams() {
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));
}

@After
public void cleanUpStreams() {
    System.setOut(null);
    System.setErr(null);
}

@Test
public void testMain4in() {
    SimmonsPS5.main(new String[]{"4.in"});
    assertEquals(47485, Integer.parseInt(outContent.toString().trim()));
}

@Test
public void testMain5in() {
    SimmonsPS5.main(new String[]{"5.in"});
    assertEquals(1667, Integer.parseInt(outContent.toString().trim()));
}

@Test
public void testMain6in() {
    SimmonsPS5.main(new String[]{"6.in"});
    assertEquals(45871, Integer.parseInt(outContent.toString().trim()));
}

@Test
public void testMain7in() {
    SimmonsPS5.main(new String[]{"7.in"});
    assertEquals(324, Integer.parseInt(outContent.toString().trim()));
}

@Test
public void testMain8in() {
    SimmonsPS5.main(new String[]{"8.in"});
    assertEquals(1057527, Integer.parseInt(outContent.toString().trim()));
}

請注意, stdout是一個整數。

您希望每個測試都測試特定功能。 所執行的所有測試都應能夠共同表明該功能是否正常運行。 因此,例如,在您的第一個示例中:

@Test
public void testCount() {
    assertEquals(0, SimmonsPS5.count(0)); // 1 obj in table with name => 0 permutations
    assertEquals(1, SimmonsPS5.count(1)); // 2 obj in table with name => 1 permutation
    assertEquals(3, SimmonsPS5.count(2)); // 2 obj in table with name => 3 permutations (3c2)
    assertEquals(5050, SimmonsPS5.count(100)); // 100 obj in table with name => 100c2 permutations (100(100+1)/2)
}

這將是適當的。 您正在測試以查看count()的行為是否符合設計要求。 如果其中任何一個失敗,則count()的行為不符合設計要求。

不要緊, 多少測試失敗-如果所有的人都失敗,將是一樣的,如果他們中的一個出現故障時采取行動,你采取行動:調試count()

您的第二種情況:

main方法通過首先輸入一個參數(文件名),然后讀取文件並對文件進行一些操作來執行此操作。 因此,我想測試它是否為每個文件輸出正確的數據-這些測試用例中的每一個都應該位於相同的測試方法中,還是位於單獨的測試方法中?

從概念上講,是否將這些組合到一個測試中取決於您要測試的內容。 您是否正在測試以查看main()有效? 或者您是否正在測試以查看它是否可以在單個特定文件上運行? 前者是一種從概念上來說更正確的測試,因為您實際上並不關心它可以在特定文件上運行,您真的在乎它可以為任何文件設計,並且您會想到一組測試文件。准確表示程序將在其上運行的文件類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM