qileilove

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

          Junit使用GroboUtils進行多線程測試

           寫過Junit單元測試的同學應該會有感覺,Junit本身是不支持普通的多線程測試的,這是因為Junit的底層實現上,是用System.exit退出用例執行的。JVM都終止了,在測試線程啟動的其他線程自然也無法執行。JunitCore代碼如下:
          /**
          * Run the tests contained in the classes named in the <code>args</code>.
          * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1.
          * Write feedback while tests are running and write
          * stack traces for all failed tests after the tests all complete.
          * @param args names of classes in which to find tests to run
          */
          public static void main(String... args) {
          runMainAndExit(new RealSystem(), args);
          }
          /**
          * Do not use. Testing purposes only.
          * @param system
          */
          public static void runMainAndExit(JUnitSystem system, String... args) {
          Result result= new JUnitCore().runMain(system, args);
          system.exit(result.wasSuccessful() ? 0 : 1);
          }
          RealSystem.java:
          public void exit(int code) {
          System.exit(code);
          }
            所以要想編寫多線程Junit測試用例,就必須讓主線程等待所有子線程執行完成后再退出。想到的辦法自然是Thread中的join方法。話又說回來,這樣一個簡單而又典型的需求,難道會沒有第三方的包支持么?通過google,很快就找到了GroboUtils這個Junit多線程測試的開源的第三方的工具包。
            GroboUtils官網下載解壓后 使用 GroboUtils-5\lib\core\GroboTestingJUnit-1.2.1-core.jar 這個即可
            GroboUtils是一個工具集合,里面包含各種測試工具,這里使用的是該工具集中的jUnit擴展.
            依賴好Jar包后就可以編寫多線程測試用例了。上手很簡單:
          package com.junittest.threadtest;
          import java.util.ArrayList;
          import java.util.HashSet;
          import java.util.Hashtable;
          import java.util.List;
          import java.util.Map;
          import java.util.Set;
          import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
          import net.sourceforge.groboutils.junit.v1.TestRunnable;
          import org.junit.Test;
          public class MutiThreadTest {
          static String[] path = new String[] { "" };
          static Map<String, String> countMap = new Hashtable<String, String>();
          static Map<String, String> countMap2 = new Hashtable<String, String>();
          static Set<String> countSet = new HashSet<String>();
          static List<String> list = new ArrayList<String>();
          @Test
          public void testThreadJunit() throws Throwable {
          //Runner數組,想當于并發多少個。
          TestRunnable[] trs = new TestRunnable [10];
          for(int i=0;i<10;i++){
          trs[i]=new ThreadA();
          }
          // 用于執行多線程測試用例的Runner,將前面定義的單個Runner組成的數組傳入
          MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
          // 開發并發執行數組里定義的內容
          mttr.runTestRunnables();
          }
          private class ThreadA extends TestRunnable {
          @Override
          public void runTest() throws Throwable {
          // 測試內容
          myCommMethod2();
          }
          }
          public void myCommMethod2() throws Exception {
          System.out.println("===" + Thread.currentThread().getId() + "begin to execute myCommMethod2");
          for (int i = 0; i <10; i++) {
          int a  = i*5;
          System.out.println(a);
          }
          System.out.println("===" + Thread.currentThread().getId() + "end to execute myCommMethod2");
          }
          }
           運行時需依賴log4j的jar文件,GroboUtils的jar包。
            主要關注3個類:TestRunnable,TestMonitorRunnable,MultiThreadedTestRunner,全部來自包:net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner.
            (1) TestRunnable 抽象類,表示一個測試線程,實例需要實現該類的runTest()方法,在該方法中寫自己用的測試代碼.該類繼承了jUnit的junit.framework.Assert類,所以可以在TestRunnable中使用各種Assert方法
            (可惜因為GroboUtils使用的jUnit版本較老,且久未更新,新版本的jUnit中已經不推薦使用這個類的方法了).該類實現了Runnable,在run方法中調用抽象方法runTest().
            (2)   MultiThreadedTestRunner
            這個類相當與一個ExecuteService,可以用來執行 TestRunnable,構造函數需要傳入TestRunnable數組,表示需要測試的線程.調用MultiThreadedTestRunner.runTestRunnables() 方法啟動測試線程,開始執行測試.
            這個方法默認讓測試線程TestRunnable的run方法最多運行1天,也可以調用MultiThreadedTestRunner.runTestRunnables(long maxTime) 這個方法,然測試線程TestRunnable最多執行 maxTime 毫秒.如果超過maxTime毫秒之后,TestRunnable還沒有執行完畢,則TestRunnable會被中斷,并且MultiThreadedTestRunner 會拋出異常,導致測試失敗fail("Threads did not finish within " + maxTime + " milliseconds.").每個TestRunnable中runTest需要能夠控制自己在什么時間自己結束自己,精確控制測試時間,不要利用上面的maxTime.
            (3) TestMonitorRunnable
            表示監控線程,可以讓每一個TestRunnable對應一個TestMonitorRunnable,在TestMonitorRunnable中監控TestRunnable.TestMonitorRunnable是TestRunnable的子類,提供了一些方便使用的方法.

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

          <2014年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 綦江县| 朝阳县| 二连浩特市| 罗江县| 太保市| 百色市| 全南县| 苏尼特左旗| 吉木乃县| 梧州市| 治多县| 出国| 项城市| 清涧县| 东丽区| 图木舒克市| 甘南县| 沽源县| 定兴县| 哈密市| 娄烦县| 城市| 平安县| 巧家县| 溆浦县| 柘荣县| 石河子市| 织金县| 三门县| 台北市| 武平县| 塔城市| 青岛市| 吉木萨尔县| 石门县| 庆阳市| 卓资县| 石城县| 东兰县| 平南县| 苗栗县|