后端测试,主要以测试接口为主。需要代码支撑,近期便找了个天气接口捣鼓了。
使用到的工具是:Eclipse + TestNG + Maven + ReportNG,全国城市编码:http://www.cnblogs.com/oucbl/p/6138963.html,接口地址:http://www.weather.com.cn/data/cityinfo/城市编码.html
先看一下代码架构,如下所示:
建的是maven工程,个人觉得这样下载依赖包较方便。工程科分为四部分:功能代码,测试case,报表文件和配置文件。网络上也有很多这样的实例,我只是列举些我在做的过程中所遇到的问题吧,也当一个记录。maven不会配置,可参见我之前写的随笔。
功能代码
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 //打开和URL之间的连接
11 URL postUrl &#