标签:eth resource 结果 inter alt case org false junit
1、自动化测试:一个自动化测试脚本有成千上万条用例,每次执行的时间在小时级别,leader说为了能先于用户知道线上的问题,能否加快脚本的执行速度
2、功能测试:不使用jmeter等工具,使用junit5实现来测试接口是否存在线程安全以及分布式线程安全问题,是否有做幂等性处理
junit5的并行测试
自身的测试用例独立,用例之间没有依赖关系,容错性好
#是否允许并行执行true/false junit.jupiter.execution.parallel.enabled=true #是否支持方法级别多线程same_thread/concurrent junit.jupiter.execution.parallel.mode.default=concurrent #是否支持类级别多线程same_thread/concurrent junit.jupiter.execution.parallel.mode.classes.default=concurrent # the maximum pool size can be configured using a ParallelExecutionConfigurationStrategy junit.jupiter.execution.parallel.config.strategy=fixed junit.jupiter.execution.parallel.config.fixed.parallelism=5
package com.wechat.testcase; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.parallel.Execution; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT; /** * 线程测试 */ //@Execution(CONCURRENT) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class Demo_07_1 { private static final Logger logger = LoggerFactory.getLogger(Demo_07_1.class); @DisplayName("线程测试01") @RepeatedTest(10) @Execution(CONCURRENT) void threadTest01() throws InterruptedException { Thread.sleep(3000); long id = Thread.currentThread().getId(); logger.info("线程号" + id + "==>装入坚果01 " + "\n"); } @DisplayName("线程测试02") @Execution(CONCURRENT) @RepeatedTest(10) void threadTest02() throws InterruptedException { Thread.sleep(3000); long id = Thread.currentThread().getId(); logger.info("线程号" + id + "==>装入坚果02 " + "\n"); } }
测试结果:
package com.wechat.testcase; import com.wechat.apiobject.DepartmentApiObject; import com.wechat.apiobject.TokenHelper; import io.restassured.response.Response; import org.junit.jupiter.api.*; import org.junit.jupiter.api.parallel.Execution; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT; /** * 线程测试 */ //@Execution(CONCURRENT) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class Demo_07_2 { private static final Logger logger = LoggerFactory.getLogger(Demo_07_2.class); static String accessToken; @BeforeAll public static void getAccessToken() throws Exception { accessToken = TokenHelper.getAccessToken(); logger.info(accessToken); } @DisplayName("创建部门") @RepeatedTest(20) @Execution(CONCURRENT) void createDepartment() { String creatName = "name1234567"; String creatEnName = "en_name1234567"; Response creatResponse = DepartmentApiObject.createDepartment(creatName, creatEnName, accessToken); assertEquals("0", creatResponse.path("errcode").toString()); } }
测试结果:
备注:说明企业微信的部门创建接口是做了线程安全和幂等性处理,5个线程同时执行20条相同的部门创建请求,只有1条是成功,19条是失败
我们需要扎实的掌握junit5,理论结合实际,才能让我们的产品不出重大bug
标签:eth resource 结果 inter alt case org false junit
原文地址:https://www.cnblogs.com/Durant0420/p/14994464.html