上一篇介绍的框架大概轮廓及 秀了一下测试报告,这一节主要介绍框架代码的的详细解释
git地址: https://gitee.com/wancongpeng/HappyDragon.git
一、获取环境
1.pom配置参数默认环境sit,获取相关环境。
根据这个默认sit去寻找src/main/filters/filter_${env}.properties的文件 pom的配置指定profiles默认二、框架的程序的入口
创建服务,new一个http服务 这里类里是个重写的http服务主要服务是get/post(json、表单、消息头)知道连有什么就可以了不用太细研究。三、解析测试用例
测试用例 解析
import com.chuanglan.api.constant.BlueDBTemplate;
import com.chuanglan.api.constant.BlueSql;
import com.chuanglan.api.dataproducer.PhoneNumberDataGenerator;
import com.chuanglan.api.dataproducer.dataproducerHelp;
import com.chuanglan.api.services.HttpClientService;
import com.chuanglan.api.services.RPCServiceFactory;
import com.chuanglan.api.utils.DBUtils;
import com.chuanglan.api.utils.GetFileMess;
import com.chuanglan.api.utils.TimeHelper;
import io.qameta.allure.Feature;
import io.qameta.allure.Stories;
import io.qameta.allure.Story;
import io.restassured.RestAssured;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.*;
import static com.chuanglan.api.utils.WritePropertiesUtil.writePropertiesFile;
import static java.util.stream.Collectors.toList;
public class OtherDemo {
private static Loggerlogger = LoggerFactory.getLogger(OtherDemo.class);
private static JdbcTemplatejdbcTemplate;
private static JdbcTemplateoracleTemplate;
private static HttpClientServicehttpClientService;
private static PhoneNumberDataGeneratorphoneNumberDataGenerator;
private static Stringextno;
private static Stringuid;
private static dataproducerHelpdataproducerHelp;
private static DBUtilsdbUtils;
private static Stringdatabase ="DBhelp.properties";//通过filter配置数据库环境
private static Stringenv =new GetFileMess().getValue("enveromnet",database);
private static Stringdburl=new GetFileMess().getValue("send.url",database);
//找数据
private StringBatchSubmit =dburl +"/msg/BatchSubmit";
private StringHttpBatchSendSM =dburl+"/msg/HttpBatchSendSM";
private StringHttpVarSM =dburl+"/msg/HttpVarSM";
private Stringmt =dburl+"/msg/mt";
private Stringsend=dburl+"/msg/send";
private StringsendDelay =dburl+"/msg/sendDelay";
private Stringjson=dburl +"/msg/send/json";
private StringVjson=dburl+"/msg/variable/json";
private static StringpackagePath="";
@BeforeClass
public static void beforeClass() {
//链接公司数据库进行断言
oracleTemplate=BlueDBTemplate.getJdbcTemplate();
httpClientService = RPCServiceFactory.createHttpService();
phoneNumberDataGenerator =new PhoneNumberDataGenerator();
dataproducerHelp=new dataproducerHelp();
dbUtils=new DBUtils();
packagePath=dbUtils.getPath("测试用例.xlsx");
extno=String.valueOf(TimeHelper.notifyTime(new Date()));
logger.info("游戏运行开始");
}
@Feature("短信发送")
@Stories(value = {@Story(value ="短信发送1")})
@Test(priority =1)
public void BatchSubmit()throws Exception{
logger.info("*******短信发送接口***********");
Thread.sleep(2000);
String account=dataproducerHelp.readTestCase(packagePath,env+"_scene","1","1");
String password=dataproducerHelp.readTestCase(packagePath,env+"_scene","1","2");
String sign=dataproducerHelp.readTestCase(packagePath,env+"_scene","1","3");
String content=dataproducerHelp.readTestCase(packagePath,env+"_scene","1","4");
String phones=phoneNumberDataGenerator.generate();
String timestamp=String.valueOf(TimeHelper.gettime(new Date()));
String msgid=String.valueOf(TimeHelper.notifyTime(new Date()));
Map mappp =oracleTemplate.queryForMap(BlueSql.account_select,new Object[]{account});
String srcCode=String.valueOf(mappp.get("SRC_CODE"));
//数据准备
List list =new ArrayList();
Map mapp=new HashMap();
mapp.put("msgid",msgid);
mapp.put("phones",phones);
mapp.put("content",content);
mapp.put("sign",sign);
mapp.put("subcode","1");
mapp.put("sendtime",timestamp);
list.add(mapp);
JSONObject reqJson =new JSONObject();
reqJson.put("account", account);
reqJson.put("password", password);
reqJson.put("data", list);
logger.info("发送请求为:"+reqJson);
JSONObject res =httpClientService.sendHttpPost(BatchSubmit, reqJson,false);
logger.info("返回请求为:"+res);
}
@AfterClass
public static void afterClass()throws IOException{
logger.info("游戏运行结束");
Map data =new HashMap<>();
String database ="DBhelp.properties";
RestAssured.baseURI =new GetFileMess().getValue("send.url", database);
logger.info("********baseURI的值为:***********"+ RestAssured.baseURI);
data.put("ENVEROMNET",new GetFileMess().getValue("enveromnet", database));
data.put("DatabaseLoginName",new GetFileMess().getValue("oracle.user", database));
data.put("DatabaseLoginPass",new GetFileMess().getValue("oracle.password", database));
data.put("DatabaseLoginIP",new GetFileMess().getValue("oracle.dburl", database));
data.put("SendURL",new GetFileMess().getValue("send.url", database));
// data.put("baseURI", RestAssured.baseURI + ":" + RestAssured.port + "/" + RestAssured.basePath);
writePropertiesFile(data);
logger.info("********data的值为:***********"+data);
}
}
通过指定DBhelp.properties来获取环境,由于pom已经设置默认环境为sit,通过GetFileMess().getValue方法来获取参数值 DBhelp.properties通过这种变量${******}来获取filter_sit.properties的值,为什么是filter_sit.properties而不是filter_dev.properties的值 ,是因为pom的参数已经指定了传递的默认参数是sit,“<filter>src/main/filters/filter_${env}.properties</filter>”是否还有印象?四、获取执行的测试数据(Excel)
继续解析用例
五、随机生成测试
这一步就是创建测试数据的,身份证、手机号、订单、姓名这些数据
网友评论