Java自动化测试(参数化 19)

Java自动化测试(参数化 19)

测试数据修改

使用占位符来替代参数,可以在代码中动态修改它的值。

修改前测试参数:

1
{"mobile_phone":"13212332111","pwd":"12345678"}

测试参数

1
{"mobile_phone":"${register_md}","pwd":"${register_pwd}"}

测试SQL

1
["select count(*) from member a where a.mobile_phone = '${register_mb}';"]

Maven导入测试数据生成

1
2
3
4
5
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>java-testdata-generator</artifactId>
<version>1.1.2</version>
</dependency>

随机测试参数生成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.zhongxin.utils;

import cn.binarywang.tools.generator.ChineseMobileNumberGenerator;

import java.util.HashMap;
import java.util.Map;

public class UserData {
// 存储接口响应对象
public static Map<String, Object> VARS = new HashMap<>();
// 存储默认请求头
public static Map<String, String> DEFAULT_HEADERS = new HashMap<>();

static {
// 静态代码:类在加载时会自动加载一次此代码
DEFAULT_HEADERS.put("X-Lemonban-Media-Type", "lemonban.v2");
DEFAULT_HEADERS.put("Content-Type", "application/json");

// 把需要参数化的数据存储到VARS
// 随机手机号码
VARS.put("${register_mb}", ChineseMobileNumberGenerator.getInstance().generate());
VARS.put("${register_pwd}", "12345678");
VARS.put("${amount}", "5000");
}
}

测试前的参数替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void paramsReplace(CaseInfo caseInfo) {
Set<String> keySet = UserData.VARS.keySet();
String params = caseInfo.getParams();
String sql = caseInfo.getSql();
String expectedResult = caseInfo.getExpectedResult();
String url = caseInfo.getUrl();
for (String placeHolder : keySet) {
String value = UserData.VARS.get(placeHolder).toString();
if (StringUtils.isNotBlank(params)) {
params = params.replace(placeHolder, value);
}
if (StringUtils.isNotBlank(sql)) {
sql = sql.replace(placeHolder, value);
}
if (StringUtils.isNotBlank(expectedResult)) {
expectedResult = expectedResult.replace(placeHolder, value);
}
if (StringUtils.isNotBlank(url)) {
url = url.replace(placeHolder, value);
}
}
caseInfo.setParams(params);
caseInfo.setSql(sql);
caseInfo.setExpectedResult(expectedResult);
caseInfo.setUrl(url);
}

Maven中导入异常排除

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.0.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
</exclusions>
</dependency>

使用常量替代

final修饰变量,变量成为常量,常量只能赋值一次。

1
2
3
4
5
6
7
8
9
10
11
12
package com.zhongxin.utils;

public class Constants {

// 响应数据回写列
public static final int PESPONSE_CELL_NUM = 8;
// 断言数据回写列
public static final int ASSERT_CELL_NUM = 10;
// 用例文件路径
public static final String EXCEL_PATH = "src/test/resources/cases_v3.xlsx";

}

部分可能会变更的数据写入到Constants类中定义为一个常量

日志记录

引入Maven

1
2
3
4
5
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

定义输出

src/test/resources/log4j.properties

1
2
3
4
5
6
7
8
9
10
11
12
log4j.rootLogger = INFO,console,file

log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File = log/api_auto.log
log4j.appender.file.Append = false
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

定义logger

src/test/java/com/zhongxin/utils/HttpUtils.java

1
2
3
import org.apache.log4j.Logger;

private static Logger logger = Logger.getLogger(HttpUtils.class);

使用

1
logger.info(statusCode);

测试日志打印

打印

 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!