码迷,mamicode.com
首页 > 其他好文 > 详细

用代码触发testng实现并发测试

时间:2018-06-26 00:48:53      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:rgs   man   code   classes   testng依赖   mint   lis   group   public   

有时候希望测试用例能用代码触发,发现testng支持这种操作,于是记录一下:

首先添加testng依赖:

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.9.10</version>
    </dependency>

然后实现要被调用的测试用例:

/**
 * testng 注解实现并发测试
 */
public class ConcurrentTest {

    @Test(threadPoolSize = 10, invocationCount = 10, timeOut = 1000)
    public static void display() {
        System.out.println(UUID.randomUUID().toString());
    }
}

再用代码触发执行该测试用例:

/**
 * 代码触发testng测试
 */
public class TestNgDemo {

    public static void main(String[] args) {
        /**初始化testNG对象*/
        TestNG testNG = new TestNG();
        TestListenerAdapter listener = new TestListenerAdapter();

        testNG.addListener(listener);

        /**添加需要执行的测试类数组*/
        testNG.setTestClasses(new Class[]{ConcurrentTest.class});

        /**执行测试用例*/
        testNG.run();

        /**汇总分析*/
        List<ITestResult> pass = listener.getPassedTests();
        List<ITestResult> failed = listener.getFailedTests();

        List<Long> interval = pass.stream().map(x -> (x.getEndMillis() - x.getStartMillis())).collect(Collectors.toList());

        /**最大耗时*/
        long maxtime = interval.stream().max(Comparator.naturalOrder()).get();

        /**最小耗时*/
        long mintime = interval.stream().min(Comparator.naturalOrder()).get();

        /**平均耗时*/
        double avgtime = interval.stream().mapToDouble(i -> i).average().getAsDouble();

        System.out.println(String.format("test result: [success: %s],[failure: %s]",pass.size(),failed.size()));

        System.out.println(String.format("performance analysis: [maxtime: %s(ms)],[mintime: %s(ms),[avgtime: %s(ms)]]",
                maxtime, mintime, avgtime));
    }
}

最后执行结果如下:

[TestNG] Running:
  Command line suite

894eecc7-6d39-4b8d-bc6f-36c94296ff13
f7bdeb99-2baf-4894-a0a3-40fd0792432a
ce27be2e-402b-46bb-8b71-c983f2d3b36c
4b042268-ede8-4c75-9d9a-e5ea7cf63d93
a2dd8b48-56e9-4187-b2f8-004a99164b3c
b4d188e2-1ba4-4635-a546-7131faa2dede
cbffc694-f0f5-48e3-80ec-e9cebdb054b9
904eaab1-3ef7-4627-bc6c-0e890510b119
3db7eb16-f369-449c-be2c-cb89e9647ad0
c4baf3ce-a162-40e0-aa27-e538cc09fb99

===============================================
Command line suite
Total tests run: 10, Failures: 0, Skips: 0
===============================================

test result: [success: 10],[failure: 0]
performance analysis: [maxtime: 8(ms)],[mintime: 0(ms),[avgtime: 2.8(ms)]]

Process finished with exit code 0

 

用代码触发testng实现并发测试

标签:rgs   man   code   classes   testng依赖   mint   lis   group   public   

原文地址:https://www.cnblogs.com/cord/p/9226672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!