12、testng---并发测试

1、单接口测试

有些业务场景,我们需要对一个测试用例执行并发测试,或者说是一个接口的反复执行,我们知道pytest中有repeat这种装饰器。TestNG 中也提供了优雅的支持方式,在@Test标签中指定 threadPoolSize 和 invocationCount,threadPoolSize默认值是0,invocationCount默认值是1

其中 threadPoolSize 表明用于调用该方法的线程池容量,invocationCount 表示该方法总计需要被执行的次数。

下面是个例子,我设置线程池容量为1,执行次数为8。为了方便看,我把休眠时间设置为2s。

package org.example;

import org.testng.Assert;
import org.testng.annotations.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DemoTest {
    @BeforeClass
    public void setUp() {
        Date date = new Date();
        // 使用SimpleDateFormat,并指定匹配模板
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
    }

    @AfterClass
    public void tearDown() {
        Date date = new Date();
        // 使用SimpleDateFormat,并指定匹配模板
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
    }

    @Test(threadPoolSize = 1, invocationCount = 8)
    public void test01() throws InterruptedException {
        Thread.sleep(2000);
        Assert.assertTrue(true);
    }
}

执行时间为16s:

这里我再设置线程池容量为4,执行次数为8,为了方便看,我把休眠时间设置为2s。

@Test(threadPoolSize = 4, invocationCount = 8)
    public void test01() throws InterruptedException {
        Thread.sleep(2000);
        Assert.assertTrue(true);
    }

执行时间为5s ,这里相当于每个线程平均执行2个请求

这里我再设置线程池容量为6,执行次数为8,为了方便看,我把休眠时间设置为2s。

可以看到虽然设置了6个线程,但是每个线程最少执行一个请求,最多执行两个请求,所以总的执行时间是4s

2、xml配置实现并发

在testng.xml文件里面设置如下

  • 在当前测试规划的执行过程中,为每个测试方法的执行使用单独的线程,最多并发4个线程。
<suite name="My suite" parallel="methods" thread-count="4">
  • 在当前测试规划的执行过程中,为每个测试用例的执行使用单独的线程(该测试用例中的测试方法共享一个线程),最多并发4个线程。
<suite name="My suite" parallel="tests" thread-count="4">
  • 在当前测试规划的执行过程中,为每个测试类的执行使用单独的线程(该测试类中的测试方法共享一个线程),最多并发4个线程。
<suite name="My suite" parallel="classes" thread-count="4">
  • 在当前测试规划的执行过程中,为每个测试类的执行使用单独的线程(该测试类中的测试方法共享一个线程),最多并发4个线程。
<suite name="My suite" parallel="instances" thread-count="4">

这里的parallel默认值为"none",不太建议使用true或者false了

 

3、@DataProvider的并发

实际上大家都会使用 DataProvider 来管理一个用例的不同数据,而上述在 testng.xml 中修改 suite 标签的方法,不太适用于此, DataProvider中有自带注解参数parallel,我们在定义数据源的时候,可以加上parallel = true,如下例子,我每个case sleep 2秒。

package org.example;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DataTest {
    @BeforeClass
    public void setUp() {
        Date date = new Date();
        // 使用SimpleDateFormat,并指定匹配模板
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
    }

    @AfterClass
    public void tearDown() {
        Date date = new Date();
        // 使用SimpleDateFormat,并指定匹配模板
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
    }

    @DataProvider(name = "data", parallel = true)
    public static Object[][] data() {
        return new Object[][]{
                {1},
                {2},
                {3},
                {4},
                {5},
                {6}
        };
    }

    @Test(dataProvider = "data")
    public void test01(int num) throws InterruptedException {
        Thread.sleep(2000);
        System.out.println(num);
    }
}

结果如下,实际上DataProvider并行执行的线程池容量默认为 10

 

 如果要更改并发的数量,也可以在 suite tag 下指定参数 data-provider-thread-count

<suite name="All Test Suite" parallel="methods" thread-count="4" data-provider-thread-count="3">

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值