根据上一篇博文实现了junit+springboottest+parameterized达到数据驱动的效果,可以看出很繁琐,要自己写解析文件代码,今天介绍一个插件easytest,本文主要源于(https://github.com/EaseTech/easytest-core/wiki/Understanding-@DataLoader-annotation)这篇文档,仅以自己角度理解,如有不适合的欢迎评论。(注意:这种方式不支持@Transactional事务回滚属性)
easytest为JAVA+junit提供数据驱动工具,通过注解引入对应文件,只要按照规则该工具就可以解析文件,适配参数。
1、引入maven包
首先,pom.xml文件中引入maven包,这里面要注意把对应冲突的包exclusion一下,之前我就是因为里面javaee的问题导致被测程序启动报错,找了很久原因。
<dependency>
<groupId>org.easetech</groupId>
<artifactId>easytest-core</artifactId>
<version>1.4.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
2、整理测试数据
csv文件内容如下面规则:
simplTestMethod,name,age
,Ravi,32
,Christiaan,29
,Anuj,31
其中simplTestMethod是测试方法,即被@Test注解的测试这批数据的方法,这个务必不要乱写,之前我以为不是一一对应关系,就报空指针错误,一脸懵逼。name,age即为测试数据标题,第二行就是测试数据了,第一列要为空
3、测试方法
package easytech.demo;
import org.easetech.easytest.annotation.DataLoader;
import org.easetech.easytest.annotation.Param;
import org.easetech.easytest.runner.DataDrivenTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths={"testData.csv"},loaderType=LoaderType.CSV,writeData=false)
public class DataLoadderFilePathsDemo{
@Test
public void simplTestMethod(@Param(name="name")String name , @Param(name="age")int age){
}
}
注解 @DataLoader(filePaths = "testData.csv", loaderType = LoaderType.CSV) 标识是用CSV,XML还是EXCEL
然后@Param(name = "a")中的NAME就是对应EXCEL或CSV文件中的列的标题了。然后就可以使用啦,其他功能可以参看原文