问题:集成测试套件中case 执行出现Threadpool 异常但本地单case 执行结果pass ?
代码结构:
/**
* 基础test 类,处理基础请求体封装
*/
public class BaseTest extends AbstractTestNGSpringContextTests {
//省略内容
}
/**
* 继承基础test类,处理业务相关的部分特性业务
*/
public class XMBaseTest extends BaseTest {
//省略内容
}
public class TestAddRelation extends XMBaseTest {
//普通测试类
@Test
public void testAddRelation() {
//数据封装
//请求
//结果断言
}
}
集成配置 testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="xm-autotest-live" parallel="true" thread-count="20" data-provider-thread-count="5" >
<listeners>
<listener class-name="com.demo.RetryListener" />
<listener class-name="com.demo.TestListener" />
<listener class-name="com.demo.ExtentTestNGIReporterListener"/>
</listeners>
<test name="demo" preserve-order="true" verbose="3">
<packages>
<package name="com.demo.cases"></package>
</packages>
</test>
</suite>
说明:因为我们有近1000个case,执行时间已经超预期的长,为了降低case 执行时间,开启了并行数 parallel ,并且配置了线程总数为 20 , 并行数据最大提供5线程池。
源码说明:
<!ATTLIST suite
name CDATA #REQUIRED
junit (true | false) "false"
verbose CDATA #IMPLIED
parallel (false | true | none | methods | tests | classes | instances) "none"
parent-module CDATA #IMPLIED
guice-stage (DEVELOPMENT | PRODUCTION | TOOL) "DEVELOPMENT"
configfailurepolicy (skip | continue) "skip"
thread-count CDATA "5"
annotations CDATA #IMPLIED
time-out CDATA #IMPLIED
skipfailedinvocationcounts (true | false) "false"
data-provider-thread-count CDATA "10"
object-factory CDATA #IMPLIED
group-by-instances (true | false) "false"
preserve-order (true | false) "true"
allow-return-values (true | false) "false"
>
- parallel (false | true | none | methods | tests | classes | instances) "none"
- 注释: Whether TestNG should use different threads to run your tests (might speed up the process) Do not use "true" and "false" values, they are now deprecated.
- 翻译:TestNG是否应该使用不同的线程来运行测试(可能会加快进程)不要使用“true”和“false”值,它们现在已经被弃用了。
- 说明:也就是说parallel 是同来控制是否使用多线程执行测试,默认是不开启,可以指定到 方法,文件等,“true”和“false”值 已经不提倡使用了。
- thread-count: An integer giving the size of the thread pool to use if you set parallel.
- 翻译:如果设置parallel,则给出要使用的线程池大小的整数。
- 说明:集成测试设置的线程池大小
- data-provider-thread-count: An integer giving the size of the thread pool to use for parallel data providers.
- 翻译:如果使用并行模式,则给出要使用的线程池大小的整数。覆盖套件级别值。
- 说明:该字段多用于具体测试包指定线程数大小
大概分析:
我们会出现Threadpool 异常,多半是配置脚本在并行执行时出现资源对象抢占混乱导致。
解决办法:
将原来并行的脚本执行改为串行问题解决
<!--<suite name="xm-autotest-live" parallel="true" thread-count="20" data-provider-thread-count="5" >-->
<!--可能存在线程不安全情况让sutie case 按顺序执行 parallel="false" -->
<suite name="xm-autotest-live" parallel="none" >
深入分析:
- 实际使用中往往一个suite 中会配置多个子套件或者多个测试包文件,一个包文件往往有几十到几百个case
- 而基于我们的工程结构,所有的case 都是集成于基础test 类,基础test 类会帮我初始化很多对象系信息。
- 当我们在配置suite 可以进行多线程执行case 时,就容易引起初始化对象错乱,一些case 的初始化配置异常,导致case 抛出thrreadpool 异常。
以上结论不是很肯定,但从结果分析,每次失败的case 是随机的,但异常都是一样的,然而本地单个case 执行时却是通过的,另外通过改变suite 配置取消并行执行后,不再出现ThreadPool 异常
因此断定是上述原因导致。