簡體   English   中英

如何在Junit 4中重寫數據驅動的JUnit 3測試套件?

[英]How to rewrite data-driven test suites of JUnit 3 in Junit 4?

我正在使用基於Rainsberger的JUnit Recipes運行JUnit 3的數據驅動測試套件。 這些測試的目的是檢查與一組輸入/輸出對相關的特定功能是否正確實現。

這是測試套件的定義:

public static Test suite() throws Exception {
    TestSuite suite = new TestSuite();
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(2009, 8, 05, 13, 23); // 2009. 09. 05. 13:23
    java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
    suite.addTest(new DateFormatTestToString(date, JtDateFormat.FormatType.YYYY_MON_DD, "2009-SEP-05"));
    suite.addTest(new DateFormatTestToString(date, JtDateFormat.FormatType.DD_MON_YYYY, "05/SEP/2009"));
    return suite;
}   

以及測試類的定義:

public class DateFormatTestToString extends TestCase {

    private java.sql.Date date;
    private JtDateFormat.FormatType dateFormat;
    private String expectedStringFormat;

    public DateFormatTestToString(java.sql.Date date, JtDateFormat.FormatType dateFormat, String expectedStringFormat) {
        super("testGetString");
        this.date = date;
        this.dateFormat = dateFormat;
        this.expectedStringFormat = expectedStringFormat;
    }

    public void testGetString() {
        String result = JtDateFormat.getString(date, dateFormat);
        assertTrue( expectedStringFormat.equalsIgnoreCase(result));
    }
}

使用JUnit 4如何測試一個方法的幾個輸入輸出參數?

這個問題和答案向我解釋了在這方面JUnit 3和4之間的區別。 該問題和答案描述了為一組類創建測試套件的方法,但沒有為具有一組不同參數的方法創建測試套件的方法。

解決方案

根據drscroogemcduck的回答,這正是幫助的確切頁面

真正簡單的方法:

您總可以有一種方法:

checkGetString(date, dateFormat, expectedValue)

然后有一個方法

@Test
testGetString:

  checkGetString(date1, '...', '...');
  checkGetString(date2, '...', '...');

更好的方法:

http://junit.sourceforge.net/javadoc_40/org/junit/runners/Parameterized.html

或更好的junit理論:

http://isagoksu.com/2009/development/agile-development/test-driven-development/using-junit-datapoints-and-theories/

暫無
暫無

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

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