51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1786|回复: 0
打印 上一主题 下一主题

Junit使用GroboUtils进行多线程测试

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-1-28 14:10:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
写过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.      * Do not use. Testing purposes only.
  13.      * @param system  
  14.      */
  15.     public static void runMainAndExit(JUnitSystem system, String... args) {
  16.         Result result= new JUnitCore().runMain(system, args);
  17.         system.exit(result.wasSuccessful() ? 0 : 1);
  18.     }
复制代码
RealSystem.java:
  1. public void exit(int code) {
  2.         System.exit(code);
  3.     }
复制代码
所以要想编写多线程Junit测试用例,就必须让主线程等待所有子线程执行完成后再退出。想到的办法自然是Thread中的join方法。话又说回来,这样一个简单而又典型的需求,难道会没有第三方的包支持么?通过google,很快就找到了GroboUtils这个Junit多线程测试的开源的第三方的工具包。

GroboUtils官网如下:http://groboutils.sourceforge.net/
下载页面:http://groboutils.sourceforge.net/downloads.html,解压后 使用 GroboUtils-5\lib\core\GroboTestingJUnit-1.2.1-core.jar 这个即可

GroboUtils是一个工具集合,里面包含各种测试工具,这里使用的是该工具集中的jUnit扩展.
依赖好Jar包后就可以编写多线程测试用例了。上手很简单:
  1. package com.junittest.threadtest;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashSet;  
  5. import java.util.Hashtable;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8. import java.util.Set;  
  9.   
  10. import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;  
  11. import net.sourceforge.groboutils.junit.v1.TestRunnable;  
  12.   
  13.   
  14. import org.junit.Test;  
  15.   
  16. public class MutiThreadTest {  
  17.   
  18.     static String[] path = new String[] { "" };  
  19.     static Map<String, String> countMap = new Hashtable<String, String>();  
  20.     static Map<String, String> countMap2 = new Hashtable<String, String>();  
  21.     static Set<String> countSet = new HashSet<String>();  
  22.     static List<String> list = new ArrayList<String>();  
  23.   
  24.     @Test  
  25.     public void testThreadJunit() throws Throwable {   
  26.         //Runner数组,想当于并发多少个。
  27.         TestRunnable[] trs = new TestRunnable [10];  
  28.         for(int i=0;i<10;i++){  
  29.             trs[i]=new ThreadA();  
  30.         }  

  31.         // 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入
  32.         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);  
  33.         
  34.         // 开发并发执行数组里定义的内容
  35.         mttr.runTestRunnables();  
  36.         
  37.         
  38.     }  
  39.   
  40.     private class ThreadA extends TestRunnable {  
  41.         @Override  
  42.         public void runTest() throws Throwable {  
  43.             // 测试内容
  44.             myCommMethod2();  
  45.         }  
  46.     }  
  47.   
  48.     public void myCommMethod2() throws Exception {  
  49.         System.out.println("===" + Thread.currentThread().getId() + "begin to execute myCommMethod2");  
  50.         for (int i = 0; i <10; i++) {  
  51.              int a  = i*5;  
  52.              System.out.println(a);  
  53.         }  
  54.         System.out.println("===" + Thread.currentThread().getId() + "end to execute myCommMethod2");  
  55.     }  
  56. }
复制代码
运行时需依赖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的子类,提供了一些方便使用的方法.

注:maven工程需要自己下载groboutils-core-5.jar,然后在私服上上传安装下,将dependency添加到自己的pom.xml中即可。





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-19 20:25 , Processed in 0.063095 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表