qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          JUnit中按照順序執行測試方式

          很多情況下,寫了一堆的test case,希望某一些test case必須在某個test case之后執行。比如,測試某一個Dao代碼,希望添加的case在最前面,然后是修改或者查詢,最后才是刪除,以前的做法把所有的方法都集中到某一個方法去執行,一個個羅列好,比較麻煩。比較幸福的事情就是JUnit4.11之后提供了MethodSorters,可以有三種方式對test執行順序進行指定,如下:
          /**
          * 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--------
          */
          換成按字母排序
          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.NAME_ASCENDING)
          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--------");
          }
          }
          /*
          2014-03-31 16:10:25,360 0    [main] INFO  - ------1--------
          2014-03-31 16:10:25,361 1    [main] INFO  - ------2--------
          2014-03-31 16:10:25,362 2    [main] INFO  - ------3--------
          */

          posted on 2014-10-15 09:14 順其自然EVO 閱讀(249) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 周口市| 青冈县| 滨海县| 华阴市| 隆安县| 五大连池市| 肥乡县| 三穗县| 南乐县| 梓潼县| 汶川县| 尼木县| 新营市| 清新县| 高尔夫| 石泉县| 东乌| 苏尼特左旗| 通山县| 连平县| 义马市| 龙口市| 高阳县| 武功县| 宜阳县| 宁安市| 门源| 三江| 平原县| 潍坊市| 黔东| 沛县| 泸西县| 灌南县| 越西县| 凉山| 盐城市| 临泉县| 上犹县| 大名县| 报价|