zoukankan      html  css  js  c++  java
  • 多线程的单元测试工具

    写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的。JVM都终止了,在测试线程启动的其他线程自然也无法执行。JunitCore代码如下:

    1. /** 
    2.      * Run the tests contained in the classes named in the <code>args</code>. 
    3.      * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1. 
    4.      * Write feedback while tests are running and write 
    5.      * stack traces for all failed tests after the tests all complete. 
    6.      * @param args names of classes in which to find tests to run 
    7.      */ 
    8.     public static void main(String... args) { 
    9.         runMainAndExit(new RealSystem(), args); 
    10.     } 
    11.  
    12.     /** 
    13.      * Do not use. Testing purposes only. 
    14.      * @param system  
    15.      */ 
    16.     public static void runMainAndExit(JUnitSystem system, String... args) { 
    17.         Result result= new JUnitCore().runMain(system, args); 
    18.         system.exit(result.wasSuccessful() ? 0 : 1); 
    19.     } 

    RealSystem.java:

    1. public void exit(int code) { 
    2.  
    3.         System.exit(code); 
    4.  
    5.     } 
    所以要想编写多线程Junit测试用例,就必须让主线程等待所有子线程执行完成后再退出。想到的办法自然是Thread中的join方法。话又说回来,这样一个简单而又典型的需求,难道会没有第三方的包支持么?通过google,笔者很快就找到了GroboUtils这个Junit多线程测试的开源的第三方的工具包。
     
     GroboUtils官网如下:
     
     
    下载页面:
     
    Maven依赖方式:
     
    1. <dependency> 
    2.       <groupId>net.sourceforge.groboutils</groupId> 
    3.       <artifactId>groboutils-core</artifactId> 
    4.       <version>5</version> 
    5.     </dependency> 
    注:需要第三方库支持:
    Repository Opensymphony Releases
    Repository url https://oss.sonatype.org/content/repositories/opensymphony-releases
    依赖好Jar包后就可以编写多线程测试用例了。上手很简单:
    1. /** 
    2.      * 多线程测试用例 
    3.      *  
    4.      * @author lihzh(One Coder) 
    5.      * @date 2012-6-12 下午9:18:11 
    6.      * @blog http://www.coderli.com 
    7.      */ 
    8.     @Test 
    9.     public void MultiRequestsTest() { 
    10.                 // 构造一个Runner 
    11.         TestRunnable runner = new TestRunnable() { 
    12.             @Override 
    13.             public void runTest() throws Throwable { 
    14.                 // 测试内容 
    15.             } 
    16.         }; 
    17.         int runnerCount = 100; 
    18.                 //Rnner数组,想当于并发多少个。 
    19.         TestRunnable[] trs = new TestRunnable[runnerCount]; 
    20.         for (int i = 0; i < runnerCount; i++) { 
    21.             trs[i] = runner; 
    22.         } 
    23.                 // 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入 
    24.         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); 
    25.         try { 
    26.                         // 开发并发执行数组里定义的内容 
    27.             mttr.runTestRunnables(); 
    28.         } catch (Throwable e) { 
    29.             e.printStackTrace(); 
    30.         } 
    31.     } 

    执行一下,看看效果。怎么样,你的Junit也可以执行多线程测试用例了吧:)。

    本文出自One Coder博客,出处: http://www.coderli.com/archives/multi-thread-junit-grobountils/

  • 相关阅读:
    scrum
    control.begininvoke
    ChangeBrowsePosition Method
    常见linux命令(表格分类)
    Python 之优先级排序
    Python 之分辨双胞胎:copy(浅拷贝)与 deepcopy(深拷贝)
    字符编码学习总结
    Python 多继承方式及顺序
    AttributeError: module 'datetime' has no attribute 'now' ------解决方法之一
    Python 模块定义、导入、优化详解
  • 原文地址:https://www.cnblogs.com/xujanus/p/5530444.html
Copyright © 2011-2022 走看看