-
java 接口测试
学习java 时间也不算太长,大学有点基础,学起来也顺手多了,其实我感觉更是因为学习完python之后,在学习别的语言的时候就简单多了,几乎所有的语言学起来都差不多。前几篇写过的都是python接口测试,开源的接口测试代码,https://github.com/liwanlei/jiekou-python3 ,好评如潮。
啥都不说,学会点java我就想实践,毕竟是实践出真知。
其实这个目录结构也是很简单的,根据文件的名字就能猜测到这里是干什么呢,那么接下来,我们来看看这里的庐山真面目。本次才用了是testng测试框架,利用ztest做测试报告,才用测试数据驱动的方式来编写测试用例,
Urlconnetc.java /** * */ package com.testapi.until; import java.io.DataOutputStream; import java.net.HttpURLConnection; import java.net.URL; /** * @author lileilei *Urlconnetc.java */ public class Urlconnetc { /* * 对http请求的简单的封装 */ public static HttpURLConnection getConnection(String url,String meth,String param){ HttpURLConnection connection = null; try { URL postUrl = new URL(url); DataOutputStream out=null; connection = (HttpURLConnection) postUrl.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod(meth); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type","application/json"); connection.setRequestProperty("Accept","application/json"); connection.setInstanceFollowRedirects(true); } catch (Exception e) { e.printStackTrace(); } return connection; } }
ParseJson,java /** * */ package com.testapi.until; import com.google.gson.Gson; /** * @author lileilei * */ public class ParseJson { /* *解析返回的json ֵ */ public static String Json(String jsStrings) { String result; if(jsStrings==null) { result=null; return result ; }try { Gson gson=new Gson(); Json json=gson.fromJson(jsStrings, Json.class); result=json.code; return result ; } catch (Exception e) { // TODO: handle exception result=null; return result ; } } }
Makejson.java package com.testapi.until; import java.util.HashMap; import com.google.gson.Gson; public class Makejson { /* * 传入字符串,转成json格式 */ public static String makejson(String map) { Gson gson=new Gson(); String param1=gson.toJson(map); return param1; } }
Json.java package com.testapi.until; public class Json { public String code; public String text; }
getcode.java /** * */ package com.testapi.until; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.HashMap; import javax.swing.SortingFocusTraversalPolicy; import com.google.gson.Gson; import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List; /** * @author lileilei * *Getcode.java */ public class Getcode { private String url=""; public String geturl() { return url; } public String getHttpResponse(String parm,String baseurl,String path,String meth) { String line=""; String resulst=""; url=baseurl+path; try { HttpURLConnection connection=Urlconnetc.getConnection(url, meth,parm); DataOutputStream out=null; connection.connect(); out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(parm.toString()); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line = reader.readLine()) != null) { resulst = resulst + line.toString(); } reader.close(); connection.disconnect(); } catch (Exception e) { // TODO: handle exception } return resulst; } }
测试用例文件 package com.testapi.cas; import static org.testng.Assert.assertEquals; import java.util.HashMap; import org.testng.annotations.*; import com.google.gson.Gson; import com.testapi.until.Getcode; import com.testapi.until.Makejson; import com.testapi.until.ParseJson; import com.testapi.until.ZTestReport; @Listeners({ZTestReport.class}) public class NewTest { @DataProvider(name="DP1") public Object[][] createData(){ return new Object[][] { { "{'key':'你好!'}","http://www.tuling123.com", "/openapi/api", "POST","text" ,"40007"}, { "{'key':'你好!'}","http://www.tuling123.com", "/openapi/api", "POST" ,"text","40007"}, { "{'key':'你好!'}","http://www.tuling123.com", "/openapi/api", "POST" ,"text","40007"}, { "{'key':'你好!'}","http://www.tuling123.com", "/openapi/api", "POST","text","40007"} }; } @Test(dataProvider="DP1") public void f(String map,String url,String path,String meth,String jsonid,String qiwang) { String param1 = Makejson.makejson(map); Getcode getcode=new Getcode(); String resulst=getcode.getHttpResponse(param1, url, path,meth.toUpperCase()); String bnei=ParseJson.Json(resulst); assertEquals(bnei,qiwang); } @BeforeTest public void beforeTest() { } @AfterTest public void afterTest() { } }
然后利用testng run来测试
最后的测试报告的html
测试报告来源于飞总的开源的测试报告: https://github.com/zhangfei19841004/ztest
感谢大神开源这么牛逼的测试报告,python版本的测试报告:来源于mock哥https://github.com/TesterlifeRaymond/BeautifulReport
感谢大神的开源,
开源代码地址:https://github.com/liwanlei/java_jiekou
作者寄语:
前进的道路我们充满着迷茫,
前进的每一步我们都会有收获。
路在脚下,我们决定不了我们的出身,但是我们可以努力改变我们未来。
告别昨天失败的自己,努力拼搏今天,成就美好明天
出 处:https://www.cnblogs.com/leiziv5/p/8032769.html