經常,在應用程序的業務邏輯中存在大量的這樣的接口:他們接受不同的輸入,然后進行或驗證,或處理,進而完成相同的流程。比如網站的登錄入口,用戶名和密碼都有長度的限制,同時也具有是否允許特殊字符的限制等,所以在我們進行其單元測試的過程中,根據不同長度的用戶名和密碼,以及不同的字符組合,只需要提供相同的測試代碼結構,就能完成測試,不同的僅僅測試數據與期望值,但是因為每一個測試方法中的輸入參數不同,我們必須為每一個輸入組編寫單獨的測試用例,從而產生大量冗余代碼,十分不便于維護。
基于以上場景,JUnit 4 提供了參數化的特性,從而能夠實現不同數據輸入對相同測試代碼的測試,如清單 1 所示:
清單 1. JUnit 4 參數化測試代碼示例
package sample.test; import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import sample.code.UserAccess; /* * JUnit - Parameter test sample */ @RunWith(Parameterized.class) public class JunitSample { private String user; private String pw; private boolean expected; @Parameters public static Collection dataGenerater() { return Arrays.asList(new Object[][] { { "user01", "123456", true }, { "helloworld", "123456", false }, { "david", "re*ads", false }, { "goodone", "onegood", true } }); } public JunitSample(String user, String pw, boolean expected) { this.user = user; this.pw = pw; this.expected = expected; } @Test public void testAccessCheck() { assertEquals(expected, UserAccess.accessCheck(user, pw)); } } |
通過以上示例代碼可以看出,JUnit 4 通過使用一個標記 @Parameters 注釋的返回類型為 Collection 的靜態方法產生數據,測試數據通過變量傳遞給測試方法,從而完成多數據輸入的測試。但是隨著業務的需要,測試人員需要經常增加測試數據與修改現有測試數據,JUnit 4 提供的硬編碼方式已經愈顯笨重和不便,數據與代碼分離顯得尤為重要。
幸好,本文所述的 Feed4JUnit 良好的解決了數據與代碼分離的問題,Feed4JUnit 是 JUnit 測試框架的擴展,它通過操作來自于文件以及不同的數據源的測試數據,使您的單元測試變得更容易編寫與維護。
本文將通過示例向您展示 Feed4JUnit 的安裝以及測試代碼與數據分離的實現,請注意本文的示例代碼全部基于針對如下一個十分簡單用戶登錄檢驗的類,并且假定您正在使用 Eclipse 作為您的 IDE,請看清單 2 類代碼:
清單 2. 實例類
package sample.code; public class UserAccess { // simple validation for user name and password public static boolean accessCheck(String userName, String password) { if (userName.length() <= 4 || userName.length() > 8) return false; if (password.length() <= 4 || password.length() > 8) return false; if (userName.contains("@")) return false; if (password.contains("*")) return false; return true; } } |
回頁首
Feed4JUnit 的下載及安裝
1. Feed4JUnit 是開源的測試組件,您可以從如下鏈接下載最新版本:
http://sourceforge.net/projects/feed4junit/files/
2. 解壓下載的 zip 包,復制整個 lib 文件夾到您的 Java 項目的根目錄,如圖 1:
圖 1. 復制 lib 到項目根目錄
3. 選定項目,右鍵選擇項目的屬性,然后通過 Add JARs 將步驟 2 中 lib 文件夾下的所有 Jar 添加到項目的 Build Path 下,如圖 2
圖 2. 添加 Jar 到 Build Path
通過以上三步,您已經準備好您的 Feed4JUnit 環境并可以開始使用它,當然,開發測試代碼之前,您必需要將 Feed4JUnit 相應的包 Import 進您的類。
回頁首
使用 Feed4JUnit 實現數據與代碼分離的測試
Feed4JUnit 的數據源可以包括以下幾種類型 - 文件 (CSV 或者 Excel )、數據庫、自定義數據源。
Feed4JUnit 使用一個特殊的運行類 Feeder.class,用來支持與標識參數化測試,如果您想要編寫數據與代碼分離的測試腳本,必須在您的測試類上增加注釋 @RunWith(Feeder.class) 。同時,您需要使用 @Test 來標示您實現測試的方法,并且使用 @Source 來聲明和接收數據源的數據,基本的代碼結構如清單 3 所示:
清單 3. 基本代碼結構
package sample.test; import static org.junit.Assert.assertEquals; import org.databene.feed4junit.Feeder; import org.databene.benerator.anno.Source; import org.junit.Test; import org.junit.runner.RunWith; /* * Feed4JUnit - @RunWith, @Test and @Source */ @RunWith(Feeder.class) //Specify the class will be ran as Feeder class public class Feed4JSample { @Test //Specify the method as a test method @Source()//Specify the input data source public void testAccessCheck() { assertEquals(true, true); } } |
原文轉自:http://www.anti-gravitydesign.com