基於Fitnesse的介面自動化測試-關鍵字設計-樣例-獲取時間
阿新 • • 發佈:2020-08-11
需求
介面測試中,有時候需要使用到日期格式的欄位值,不僅是當前時間,還需要帶偏移量的時間。
實現
1.編寫建構函式和成員變數
private String timeZone;
private TimeUtil timeUtil;
public TimeFixture(String format) {
timeUtil = new TimeUtil(format);
}
2.實現方法(關鍵字)
public String getCurrentTime() {//獲取當前時間,格式是yyyyMMddHHmmss return timeUtil.getTime(Calendar.SECOND, "0", this.timeZone); } public String getDay(String count) {//獲取當前或偏移天數 String time = null; try { time = timeUtil.getTime(Calendar.DAY_OF_MONTH, count, this.timeZone); } catch (NumberFormatException e) { time = "NumberFormatException"; logger.debug("NumberFormatException:", e); } logger.info("time: {}", time); return time; }
public class TimeUtil { private String format = "yyyyMMddHHmmss"; public TimeUtil() { } public TimeUtil(String format) { this.format = format; } public String getTime(int calendar, String count ,String timeZone) throws NumberFormatException { int i_count = Integer.parseInt(count); SimpleDateFormat format = new SimpleDateFormat(this.format); if(timeZone!=null){ format.setTimeZone(TimeZone.getTimeZone(timeZone)); } String time = format.format(getDate(calendar, i_count)); return time; } private Date getDate(int calendar, int count) { Calendar calendarNew = Calendar.getInstance(); calendarNew.add(calendar, count); return calendarNew.getTime(); } }
使用
1.引入類對應package
|import |
|own.slim.time|
2.編寫指令碼
|script|time fixture|yyyyMMddHHmmss|
|show |getCurrentTime |
|show |getDay |0 |
|show |getDay |3 |
|show |getDay |-3 |
3.測試
總結
上面例子中,只給出了按天偏移的方法。同理還可以編寫按照秒、月、年的方法。