国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Parameterized unit tests with JUnit 4

2019-11-09 16:10:36
字體:
來源:轉載
供稿:網友

前面已經分析過junit單元測試的用法,這篇詳細學習junit4的參數化測試

格式

在測試類上面添加 @RunWith(Parameterized.class) 提供數據集合使用 @Parameterized.Parameters(),提供的數據集合必須返回 一個數組類型的集合

@Parameterized.Parameters() public static Iterable

原理

測試運行器被調用時,將執行數據生成方法,和它將返回一組數組,每個數組是一組測試數據。測試運行器將實例化類和第一組測試數據傳遞給構造函數。構造函數將存儲數據的字段。然后將執行每個測試方法,每個測試方法獲得,第一組測試數據。每個測試方法執行后,對象將被實例化,這一次使用集合中的第二個元素的數組,等等。

代碼分析

該測試用例,用來測試時間格式化。

import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import java.util.Arrays;/** * Created by weichyang on 2017/2/7. * 參數序列測試 */@RunWith(Parameterized.class)public class FrommatUtilTest { PRivate boolean mExpected = true; private Long[] mArgs; @Parameterized.Parameters() public static Iterable<Object[]> data() { return Arrays.asList(new Object[][]{ {false, new Long[]{1483490840155l, 1483499648767l}}, {false, new Long[]{0l, 1483499648767l}}, {false, new Long[]{112120l, 0l}}, {false, new Long[]{1483413248767l, 1483499648767l}}, {false, new Long[]{1480648448767l, 1483499648767l}}, {false, new Long[]{1480648448767l - (10 * 86400000), 1483499648767l}} }); } public FrommatUtilTest(boolean expected, Long... args) { mExpected = expected; mArgs = args; } @Test public void showTimeState() throws Exception { Assert.assertEquals(mExpected, FrommatUtil.showTimeState(mArgs[0], mArgs[1])); }}

被測試方法

/** * @param sellOutMillisecond 售罄時間 * @param curTimeMillisecond 當前時間 * @return */ public static String showTimeState(long sellOutMillisecond, long curTimeMillisecond) { long calculateTime = curTimeMillisecond - sellOutMillisecond; if (calculateTime <= 0 || sellOutMillisecond <= 0) { return "剛剛"; }// LogUtil.d("sellOutMillisecond =" + sellOutMillisecond// + " curTimeMillisecond =" + curTimeMillisecond +// " calculateTime=" + calculateTime); long days = calculateTime / (1000 * 60 * 60 * 24); long hours = (calculateTime - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); String returnValue = ""; if (days > 0 || hours > 23) { //顯示日期 DateFormat formatter = new SimpleDateFormat("M月d日"); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(sellOutMillisecond); returnValue = formatter.format(calendar.getTime()); } else if (days <= 0 && hours >= 1 && hours <= 23) { //顯示小時數 returnValue = hours + "小時前"; } else if (days == 0 && hours == 0) { returnValue = "剛剛"; } return returnValue; }

輸出結果:會給出計算的值和期望的值對比,這里是測試期望的值無意義。

這里寫圖片描述

優點

參數化測試能夠最大限度的復用測試代碼

引用: Parameterized unit tests with JUnit 4 https://blogs.Oracle.com/jacobc/entry/parameterized_unit_tests_with_junit


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 民勤县| 乐东| 三亚市| 陈巴尔虎旗| 麻栗坡县| 南充市| 克什克腾旗| 邻水| 津南区| 溧水县| 宜都市| 镇坪县| 土默特左旗| 大同市| 澳门| 汉川市| 东光县| 凤山市| 苗栗市| 基隆市| 哈巴河县| 镇远县| 当涂县| 麻阳| 新田县| 扶沟县| 吉木萨尔县| 阳西县| 米林县| 商丘市| 金昌市| 嘉峪关市| 万载县| 中江县| 临潭县| 宁陕县| 福清市| 综艺| 雷波县| 会昌县| 博湖县|