以为测试老鸟分享接口测试自动化的总结与思考(三)
TestNG 接口自动化实践 参数化测试示例以 DeviceStatusHSFService 为例,测试类如下:
public class DeviceStatusHSFServiceTest {
private DeviceStatusHSFService deviceStatusHSFService;
@BeforeTest(alwaysRun = true)
public void beforeTest() {
String envName = System.getProperty("maven.env");//运行环境可配置
SwitchENV switchEnv = new SwitchENV(envName); //运行环境可配置
deviceStatusHSFService = HsfRepository.getConsumer(DeviceStatusHSFService.class, switchEnv.getEnv(),
"HSF", switchEnv.getHsfVersion(), "aicloud-device-center", switchEnv.getTargetIp()).getTarget();
}
@Test(dataProvider = "updateDeviceStatus", dataProviderClass = DeviceStatusHSFServiceTestDataProvider.class)
public void updateDeviceStatusTest(Long userId, String uuid, DeviceStatus deviceStatus){
Result<Boolean> result = deviceStatusHSFService.updateDeviceStatus(userId, uuid, deviceStatus);
System.out.println("traceId:"+EagleEye.getTraceId()+result.toString());
Boolean res = result.getResult();
assertTrue(res);
}
}
其中通过 SwitchENV 类实现运行环境可配置:
/**
* 自定义环境配置
*/
public class SwitchENV {
/**
* 运行环境
*/
private Env env;
/**
* hsf环境
*/
private String hsfVersion;
/**
* 目标机器
*/
private String targetIp;
/**
* 环境名称
*/
private String envName;
public SwitchENV(String envName) {
Properties prop = new Properties();
// TODO: 本地自动化测试切换环境专用
if (envName == null) {
envName = "pre1";
}
switch (envName) {
case "online": {
InputStream in = SwitchENV.class.getClassLoader().getResourceAsStream(
"config/application-online.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
env = Env.ONLINE;
break;
}
case "pre1": {
InputStream in = SwitchENV.class.getClassLoader().getResourceAsStream(
"config/application-pre1.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
env = Env.PREPARE;
break;
}
case "pre2": {
InputStream in = SwitchENV.class.getClassLoader().getResourceAsStream(
"config/application-pre2.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
env = Env.PREPARE;
break;
}
case "pre3": {
InputStream in = SwitchENV.class.getClassLoader().getResourceAsStream(
"config/application-pre3.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
env = Env.PREPARE;
break;
}
default:
try {
throw new Exception("环境变量输入错误!");
} catch (Exception e) {
e.printStackTrace();
}
break;
}
hsfVersion = prop.getProperty("hsfVersion").trim();
targetIp= prop.getProperty("targetIp").trim();
this.envName = envName;
}
public Env getEnv() {
return env;
}
public String getHsfVersion() {
return hsfVersion;
}
public String getTargetIp() {
return targetIp;
}
public String getEnvName() {
return envName;
}
}
测试参数全部放在 DeviceStatusHSFServiceTestDataProvider 类中,实现具体的请求接口、参数、校验等数据做到与代码相隔离。
/**
* 自定义环境配置
*/
public class SwitchENV {
/**
* 运行环境
*/
private Env env;
/**
* hsf环境
*/
private String hsfVersion;
/**
* 目标机器
*/
private String targetIp;
/**
* 环境名称
*/
private String envName;
public SwitchENV(String envName) {
Properties prop = new Properties();
// TODO: 本地自动化测试切换环境专用
if (envName == null) {
envName = "pre1";
}
switch (envName) {
case "online": {
InputStream in = SwitchENV.class.getClassLoader().getResourceAsStream(
"config/application-online.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
env = Env.ONLINE;
break;
}
case "pre1": {
InputStream in = SwitchENV.class.getClassLoader().getResourceAsStream(
"config/application-pre1.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
env = Env.PREPARE;
break;
}
case "pre2": {
InputStream in = SwitchENV.class.getClassLoader().getResourceAsStream(
"config/application-pre2.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
env = Env.PREPARE;
break;
}
case "pre3": {
InputStream in = SwitchENV.class.getClassLoader().getResourceAsStream(
"config/application-pre3.properties");
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
env = Env.PREPARE;
break;
}
default:
try {
throw new Exception("环境变量输入错误!");
} catch (Exception e) {
e.printStackTrace();
}
break;
}
hsfVersion = prop.getProperty("hsfVersion").trim();
targetIp= prop.getProperty("targetIp").trim();
this.envName = envName;
}
public Env getEnv() {
return env;
}
public String getHsfVersion() {
return hsfVersion;
}
public String getTargetIp() {
return targetIp;
}
public String getEnvName() {
return envName;
}
}
页:
[1]