这里写目录标题
在testng中还有一种获取数据的方法。是使用上下文的方式即ITestContext
自我理解的上下文(小白一枚。不知道是否正确,仅说下自己的理解):可以看做是一个容器。将从A中读取的数据通过setAttribute放到上下文中。B需要获取从A得到的数据,则可以从上下文中用getAttribute获取期值。如果有理解错误偏差。望指正。
需求:访问以下网站
查询专业名称为“物联网”返回web返回的数据和数据库查询的数据是否一致
1.请求web页进行查询请求操作。查看返回的数据
下面展示 ReadITestContextWeb .java。
public class ReadITestContextWeb {
public static void main(String[] args) {
//先调用登录接口,保证为已经登录状态。不被拦截
logintestng.login("http://127.0.0.1:8080/StudentsCourse/LoginController/tologin", "admin", "123456");
contextweb("http://127.0.0.1:8080/StudentsCourse/MangerController/queryCoreQueryInfoForClass.do","2");
}
public static Map<String, Object> contextweb(String url,String professionid) {
//将传入的参数放入到json串中
JSONObject json=JSONUtil.createObj()
.put("professionid",professionid);
//带着传入的参数发送请求
HttpResponse response=HttpRequest.post(url).body(json.toString()).execute();
String str=response.body();
//将返回的格式转成json对象
JSONObject jsobj=JSONUtil.parseObj(str);
//再将json对象转成jsonarray的格式
JSONArray arrayweb=jsobj.getJSONArray("list");
System.out.println(arrayweb);
Map<String, Object> webmap=new HashMap<String, Object>();
for (int i = 0; i < arrayweb.size(); i++) {
System.out.println("++++++++"+arrayweb.get(1));
//遍历每条jsonarry
JSONObject arr=(JSONObject)arrayweb.get(i);
//已calssid(不重复的数据)作为key 每条遍历的数据为value作放到webmap中
webmap.put(arr.getStr("classid"), arr);
}
//关闭请求流
response.close();
System.out.println("!!!!!!!!!!"+webmap);
return webmap;
}
}
以上为web查询返回来的结果
2.查询数据库返回的值:
下面展示: ReadITestContextDb.java。
public class ReadITestContextDb {
public static void main(String[] args) throws SQLException {
jsonDB();
}
public static Map<String, Object> jsonDB() throws SQLException{
//查询数据库。按照web页面查询的结果进行查询
List<Entity> listDB=Db.use().query("select cl.classid as classid, cl.classname as classname, cl.grade as grade,"
+ " p.professionid as professionid, p.professionname as professionname, cl.intime as intime, cl.modtime as modtime, "
+ "p.collegeid as collegeid, c.collegename as collegename, "
+ "u.name as name from classinfo cl "
+ "left join professioninfo p on cl.professionid = p.professionid "
+ "left join collegeinfo c on p.collegeid = c.collegeid "
+ "left join userinfo u on cl.moduserid = u.userid where p.professionid = '3'");
// System.out.println(listDB);
Map<String, Object> dbmap=new HashMap<String, Object>();
for (Entity entity : listDB) {
JSONObject jsonDb=new JSONObject();
Set<String> entitys=entity.getFieldNames();
for (String key : entitys) {
jsonDb.put(key, entity.get(key));
}
dbmap.put(jsonDb.getStr("classid"),jsonDb);
}
// for (int i = 0; i < array.length; i++) {
return dbmap;
}
}
3.编写testng的代码进行两个数据的对比
下面展示: ReadITestContextTestng.java。
public class ReadITestContextTestng {
ReadITestContextWeb web;
ReadITestContextDb db;
@Test(priority= 0)
//将web查询出来的数据放入到context中
public void selectWEB(ITestContext context) {
logintestng.login("http://127.0.0.1:8080/StudentsCourse/LoginController/tologin", "admin", "123456");
Map<String, JSONObject> seweb= web.contextweb("http://127.0.0.1:8080/StudentsCourse/MangerController/queryCoreQueryInfoForClass.do","3");
//用setAttribute放入到上下文中。用key value的形式。key可以随意命名
context.setAttribute("webs",seweb );
}
@Test(priority= 1)
//将数据库查询出来的数据用setAttribute放入到上下文中。key可以随意命名
public void selectDb(ITestContext context) throws SQLException {
Map<String, Object> seDb=db.jsonDB();
context.setAttribute("Dbs",seDb );
}
@Test(priority= 2)
public void equ(ITestContext context) throws SQLException {
boolean flag=true;
Map<String, JSONObject> equweb=(Map<String, JSONObject>) context.getAttribute("webs");
Map<String, JSONObject> equDb=(Map<String, JSONObject>) context.getAttribute("Dbs");
System.out.println(equweb);
System.out.println(equDb);
//判断两个查询出来的数据的长度是否一样。不一样则直接返回false
if(equweb.size()!=equDb.size()) {
flag=false;
}
for (String key : equweb.keySet()) {
JSONObject w=(JSONObject) equweb.get(key);
System.out.println("+++++++++++++++++"+w);
JSONObject d=(JSONObject) equDb.get(key);
//判断根据key获取的value的值是否为空。为空则返回false
if(w==null||d==null) {
flag=false;
}
for (String kk : w.keySet()) {
//判断获取的具体的值是否一样。不一样返回false
if(!w.get(kk).equals(d.get(kk))) {
flag=false;
}
else {
flag=true;
}
}
}
assertEquals(flag, true);
}
}
在编写ReadITestContextTestng.java 时。在标签@test后没有加(priority= )则执行中报空指针异常。由于没有约定test们的优先级。则他们的执行顺序按照字典序来执行的,即执行顺序为:equ>selectDb>selectWEB 先执行equ 方法,由于无数据则报异常。如果加上(priority= ) , 值越小优先级越高,则问题解决。
这篇博客介绍了如何在TestNG中利用ITestContext进行数据的传递和对比。作者首先解释了ITestContext作为数据容器的角色,然后详细阐述了在测试过程中,先从网页查询数据,再从数据库获取数据,最后通过TestNG代码比较两者的一致性。文章提到了在编写测试时未设置优先级导致的空指针异常问题,并指出通过设置priority属性可以解决这个问题。

1134

被折叠的 条评论
为什么被折叠?



