您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
JUnit中按照順序執(zhí)行測(cè)試方式
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2014/10/14 13:56:59 ] 推薦標(biāo)簽:開(kāi)源軟件測(cè)試 Junit 代碼

  很多情況下,寫了一堆的test case,希望某一些test case必須在某個(gè)test case之后執(zhí)行。比如,測(cè)試某一個(gè)Dao代碼,希望添加的case在前面,然后是修改或者查詢,后才是刪除,以前的做法把所有的方法都集中到某一個(gè)方法去執(zhí)行,一個(gè)個(gè)羅列好,比較麻煩。比較幸福的事情是JUnit4.11之后提供了MethodSorters,可以有三種方式對(duì)test執(zhí)行順序進(jìn)行指定,如下:
/**
* Sorts the test methods by the method name, in lexicographic order, with {@link Method#toString()} used as a tiebreaker
*/
NAME_ASCENDING(MethodSorter.NAME_ASCENDING),
/**
* Leaves the test methods in the order returned by the JVM. Note that the order from the JVM may vary from run to run
*/
JVM(null),
/**
* Sorts the test methods in a deterministic, but not predictable, order
*/
DEFAULT(MethodSorter.DEFAULT);
  可以小試牛刀一下:
  使用DEFAULT方式:
package com.netease.test.junit;
import org.apache.log4j.Logger;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
* User: hzwangxx
* Date: 14-3-31
* Time: 15:35
*/
@FixMethodOrder(MethodSorters.DEFAULT)
public class TestOrder {
private static final Logger LOG = Logger.getLogger(TestOrder.class);
@Test
public void testFirst() throws Exception {
LOG.info("------1--------");
}
@Test
public void testSecond() throws Exception {
LOG.info("------2--------");
}
@Test
public void testThird() throws Exception {
LOG.info("------3--------");
}
}
/*
output:
2014-03-31 16:04:15,984 0    [main] INFO  - ------1--------
2014-03-31 16:04:15,986 2    [main] INFO  - ------3--------
2014-03-31 16:04:15,987 3    [main] INFO  - ------2--------
*/

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd