dataProvider数据获取存储数据库 - 编程语言
实体报告类
CREATE TABLE `testngreport_two` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`uuid` varchar(100) NOT NULL COMMENT '编号',
`passed` varchar(255) DEFAULT NULL COMMENT '通过',
`failed` varchar(255) DEFAULT NULL COMMENT '失败',
`skipped` varchar(255) DEFAULT NULL COMMENT '跳过',
`duration` varchar(255) DEFAULT NULL COMMENT '使用时间',
`test_user` varchar(255) DEFAULT NULL COMMENT '测试用户人员',
`systemInfo` varchar(255) DEFAULT NULL COMMENT '系统信息',
`test_time` datetime DEFAULT NULL COMMENT '测试时间',
`description` varchar(255) DEFAULT NULL COMMENT '用例描述',
`starttime` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '开始时间',
`endtime` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '结束时间',
`Interfacename` varchar(255) DEFAULT NULL COMMENT '接口名字',
`type` varchar(255) DEFAULT NULL COMMENT '类型',
`failuredetails` varchar(500) DEFAULT NULL COMMENT '失败详情',
`testPackageNamePath` varchar(255) DEFAULT NULL COMMENT '包名方类名方法名',
`requestUrl` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '请求url',
`requestBody` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '请求body',
`requestParameter` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '请求参数',
`responseBody` longtext CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT '响应信息',
PRIMARY KEY (`id`,`uuid`)
) ENGINE=InnoDB AUTO_INCREMENT=380 DEFAULT CHARSET=utf8;
实体请求类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.log4j.Log4j;
import java.util.Map;
/**
* @author liwen406
* @Title: MyHttpEntity
* @Description: 请求实体类
* @date 2019/3/24 / 17:54
*/
@Log4j
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MyHttpEntity implements Entity {
String url;
String ip;
String body;
String info;
Map<String, ?> parameter;
Map<String, String> header;
}
数据配置链接
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @author liwen406
* @Title: JDBCUtils
* @Description: 1. 声明静态数据源成员变量
* 2. 创建连接池对象
* 3. 定义公有的得到数据源的方法
* 4. 定义得到连接对象的方法
* 5. 定义关闭资源的方法
* @date 2019/3/20 / 13:34
*/
public class JDBCUtils {
/**1. 声明静态数据源成员变量*/
private static DataSource ds;
/**2. 创建连接池对象*/
static {
/**加载配置文件中的数据*/
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("application.yml");
Properties pp = new Properties();
try {
pp.load(is);
/** 创建连接池,使用配置文件中的参数*/
ds = DruidDataSourceFactory.createDataSource(pp);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 3. 定义公有的得到数据源的方法
* @return
*/
public static DataSource getDataSource() {
return ds;
}
/**
* 4. 定义得到连接对象的方法
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* 5.定义关闭资源的方法
* @param conn
* @param stmt
* @param rs
*/
public static void close(Connection conn, Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
/***
* 6.重载关闭方法
* @param conn
* @param stmt
*/
public static void close(Connection conn, Statement stmt) {
close(conn, stmt, null);
}
}
数据库sql写法
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
public int savereportTwo1(TestngReport testngReport) {
testngReport.setUuid(Tool.getUUID());
testngReport.setTesttime(new Date());
int id = 0;
try {
//1.定义sql
String sql = "INSERT INTO testngreport_two(uuid,duration,test_user," +
"test_time,starttime,endtime," +
"requestUrl,requestBody,requestParameter,responseBody) VALUES (?,?,?,?,?,?,?,?,?,?)";
//2.执行sql
id = template.update(sql, testngReport.getUuid(),
testngReport.getDuration(),
testngReport.getTestuser(),
testngReport.getTesttime(),
testngReport.getStarttime(),
testngReport.getEndtime(),
testngReport.getRequestUrl(),
testngReport.getRequestBody(),
testngReport.getRequestParameter(),
testngReport.getResponseBody()
);
log.info("插入成功:" + id);
} catch (DataAccessException e) {
log.info("插入失败");
e.printStackTrace();
}
return id;
}
test类
@DataProvider
public Object[][] getparam() {
int k = 0;
String[] param = {"2367952", "2367141", "2366552", "2364334"};
Object[][] result = new Object[param.length][];
for (int i = 0; i < param.length; i++) {
MyHttpEntity httpEntity = new MyHttpEntity();
httpEntity.setUrl("https://blog.51cto.com/357712148/" + param[i]);
result[k++] = new Object[]{httpEntity};
}
return result;
}
@Test(dataProvider = "getparam", dataProviderClass = Dataprovidert.class)
public void mydatep(MyHttpEntity httpEntity) {
TestngReportRaoimpl reportRaoimpl = new TestngReportRaoimpl();
Long starttime = System.currentTimeMillis();
HttpResponse response = HttpRequest.get(httpEntity.url).execute();
Assert.assertTrue(response.body().contains("success"));
Long endttime = System.currentTimeMillis();
TestngReport testngReport = new TestngReport();
testngReport.setRequestUrl(httpEntity.getUrl());
testngReport.setStarttime(starttime + "");
testngReport.setEndtime(endttime + "");
testngReport.setTestuser("liwen");
testngReport.setRequestParameter(httpEntity.getParameter() + "");
testngReport.setResponseBody(response.body());
testngReport.setRequestBody(httpEntity.getBody());
testngReport.setDuration((endttime - starttime) + "毫秒");
reportRaoimpl.savereportTwo1(testngReport);
}
结果:
The End
发布于:2023-01-18,除非注明,否则均为
原创文章,转载请注明出处。
发表评论