java post接口测试_接口测试——Java + TestNG 国家气象局接口(json解析)实例
后端測(cè)試,主要以測(cè)試接口為主。需要代碼支撐,近期便找了個(gè)天氣接口搗鼓了。
使用到的工具是:Eclipse + TestNG + Maven + ReportNG,全國(guó)城市編碼:http://www.cnblogs.com/oucbl/p/6138963.html,接口地址:http://www.weather.com.cn/data/cityinfo/城市編碼.html
先看一下代碼架構(gòu),如下所示:
建的是maven工程,個(gè)人覺(jué)得這樣下載依賴(lài)包較方便。工程科分為四部分:功能代碼,測(cè)試case,報(bào)表文件和配置文件。網(wǎng)絡(luò)上也有很多這樣的實(shí)例,我只是列舉些我在做的過(guò)程中所遇到的問(wèn)題吧,也當(dāng)一個(gè)記錄。maven不會(huì)配置,可參見(jiàn)我之前寫(xiě)的隨筆。
功能代碼
Common
1 packagecom.CityWether.CityInfo;2
3 importnet.sf.json.JSONException;4 importnet.sf.json.JSONObject;5
6 public classCommon {7 public staticString getJsonValue(String JsonString, String JsonId) {8 String JsonValue = "";9 //trim()去掉字符串首尾的空格
10 if (JsonString == null || JsonString.trim().length() < 1) {11 return null;12 }13 try{14 JSONObject obj1 = newJSONObject(JsonString);15 JsonValue =obj1.getString(JsonId);16 } catch(JSONException e) {17 e.printStackTrace();18 }19 returnJsonValue;20 }21 }
View Code
URLConnection
1 packagecom.CityWether.CityInfo;2
3 importjava.net.HttpURLConnection;4 importjava.net.URL;5
6 public classURLConnection {7 public staticHttpURLConnection getConnection(String url){8 HttpURLConnection connection = null;9 try{10 //打開(kāi)和URL之間的連接
11 URL postUrl = newURL(url);12 connection =(HttpURLConnection) postUrl.openConnection();13 //設(shè)置通用的請(qǐng)求屬性
14 connection.setDoOutput(true);15 connection.setDoInput(true);16 connection.setRequestMethod("GET");17 connection.setUseCaches(false);18 connection.setInstanceFollowRedirects(true);19 connection.setRequestProperty("Content-Type", "application/json");20 connection.setRequestProperty("Charset", "utf-8");21 connection.setRequestProperty("Accept-Charset", "utf-8");22 } catch(Exception e) {23 e.printStackTrace();24 }25 returnconnection;26 }27 }
View Code
CityWeather
1 packagecom.CityWether.CityInfo;2
3 importjava.io.BufferedReader;4 importjava.io.DataOutputStream;5 importjava.io.IOException;6 importjava.io.InputStreamReader;7 importjava.net.HttpURLConnection;8
9 public classCityWeather {10 private String url="";11
12 publicString geturl() {13 returnurl;14 }15
16 public staticString formatString(String s) {17 if (s != null) {18 s = s.replaceAll("\ufeff", "");19 }20 returns;21 }22
23 public String getHttpRespone(String cityCode) throwsIOException {24 String line = "";25 String httpResults = "";26 url=("http://www.weather.com.cn/data/cityinfo/" + cityCode + ".html");27 try{28 HttpURLConnection connection =URLConnection.getConnection(url);29 //建立實(shí)際的連接
30 connection.connect();31 BufferedReader reader = new BufferedReader(newInputStreamReader(connection.getInputStream()));32 while ((line = reader.readLine()) != null) {33 httpResults = httpResults +line.toString();34 }35 reader.close();36 //斷開(kāi)連接
37 connection.disconnect();38 } catch(Exception e) {39 e.printStackTrace();40 }41 returnhttpResults;42 }43 }
View Code
測(cè)試case
1 packagecom.CityWether.CityInfo;2
3 importjava.io.IOException;4
5 importorg.testng.Assert;6 importorg.testng.Reporter;7 importorg.testng.annotations.Test;8
9 importcom.CityWether.CityInfo.CityWeather;10 importcom.CityWether.CityInfo.Common;11
12 public classTestCase {13 public String httpResult= null, weatherinfo= null, city=null,expect_city = null;14 public static String cityCode="";15 CityWeather weather=newCityWeather();16
17 @Test(priority=0)18 public void getHuaihua() throwsIOException{19 expect_city="懷化";20 cityCode="101251201";21 resultCheck(cityCode, expect_city);22 }23
24 @Test(priority=1)25 public void getHuitong() throwsIOException{26 expect_city="會(huì)同";27 cityCode="101251206";28 resultCheck(cityCode, expect_city);29 }30
31 @Test(priority=2)32 public void getChangsha() throwsIOException{33 expect_city="長(zhǎng)沙";34 cityCode="101250101";35 resultCheck(cityCode, expect_city);36 }37
38 @Test(priority=3)39 public void getBaoshan() throwsIOException{40 expect_city="寶山";41 cityCode="101020300";42 resultCheck(cityCode, expect_city);43 }44
45 @Test(priority=4)46 public void getShanghai() throwsIOException{47 expect_city="上海";48 cityCode="101020100";49 resultCheck(cityCode, expect_city);50 }51
52 @Test(priority=5)53 public void Minhang() throwsIOException{54 expect_city="閔行";55 cityCode="101020200";56 resultCheck(cityCode, expect_city);57 }58
59 public void resultCheck(String cityCode, String expect_city) throwsIOException{60 System.setProperty("org.uncommons.reportng.escape-output", "false");61 Reporter.log("【正常用例】:獲取"+expect_city+"天氣成功!");62 httpResult=weather.getHttpRespone(cityCode);63 Reporter.log("
請(qǐng)求地址: "+weather.geturl()+"
");64 Reporter.log("【返回結(jié)果】: "+httpResult);65 weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");66 city=Common.getJsonValue(weatherinfo, "city");67 Reporter.log("【用例結(jié)果】: resultCode=>expected: " + expect_city + " ,actual: "+ city+"
");68 Assert.assertEquals(city,expect_city);69 Reporter.log(""+"------------------------------------------------------------------------------"+"
");71 }72 }View Code
報(bào)表文件示例
報(bào)表html文件位置在如下所示:
代碼實(shí)現(xiàn)如上就完成,需要配置pom.xml文件和testng.xml文件,可參照如下:
pom.xml
pom.xml文件是下載依賴(lài)包的,特別方便
1
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 4.0.0
4
5 com
6 CityWether
7 0.0.1-SNAPSHOT
8 jar
9
10 CityWether
11 http://maven.apache.org
12
13
14 UTF-8
15
16
17
18
19 junit
20 junit
21 3.8.1
22 test
23
24
25 org.json
26 json
27 20171018
28
29
30 org.testng
31 testng
32 6.9.10
33
34
35 org.uncommons
36 reportng
37 1.1.4
38
39
40 com.google.inject
41 guice
42 4.0
43
44
45 velocity
46 velocity-dep
47 1.4
48
49
50
View Code
這里需要注意的是,由于接口返回的是json數(shù)據(jù),所以必須導(dǎo)入json依賴(lài)包:
1
2 org.json
3 json
4 20171018
5
testng.xml
testng.xml文件是用于運(yùn)行的,運(yùn)行程序直接運(yùn)行該文件即可:
1 <?xml version="1.0" encoding="UTF-8"?>
2
3
4
5
6
7
8
9
10
11
12
13
View Code
問(wèn)題總結(jié)
自己在完成過(guò)程中,過(guò)程中遇到如下問(wèn)題:
1、接口返回的數(shù)據(jù)是亂碼
如下所示:
解決辦法:
在CityWeather類(lèi)中代碼下加上如下代碼即可:
public staticString formatString(String s) {if (s != null) {
s= s.replaceAll("\ufeff", "");
}returns;
}
2、Common類(lèi)中導(dǎo)包錯(cuò)誤
Common類(lèi)中代碼導(dǎo)入如下包,運(yùn)行程序報(bào)錯(cuò)
importorg.json.JSONException;import org.json.JSONObject;
報(bào)錯(cuò)為:
解決辦法為:
重新導(dǎo)入JSON包即可,如下:
importnet.sf.json.JSONException;import net.sf.json.JSONObject;
3、注意:測(cè)試報(bào)告美化是依賴(lài)ReportNG包的,切莫忘記
本文僅代表作者觀點(diǎn),系作者@溫一壺清酒發(fā)表。
歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
文章出處:http://www.cnblogs.com/hong-fithing/
總結(jié)
以上是生活随笔為你收集整理的java post接口测试_接口测试——Java + TestNG 国家气象局接口(json解析)实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java强引用弱引用_Java 的强引用
- 下一篇: java servlet filter_