后端测试,主要以测试接口为主。需要代码支撑,近期便找了个天气接口捣鼓了。

使用到的工具是: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 = newURL(url);12 connection =(HttpURLConnection) postUrl.openConnection();13 //设置通用的请求属性

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 //建立实际的连接

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 //断开连接

37 connection.disconnect();38 } catch(Exception e) {39 e.printStackTrace();40 }41 returnhttpResults;42 }43 }

View Code

测试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="会同";27 cityCode="101251206";28 resultCheck(cityCode, expect_city);29 }30

31 @Test(priority=2)32 public void getChangsha() throwsIOException{33 expect_city="长沙";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("

请求地址: "+weather.geturl()+"

");64 Reporter.log("【返回结果】: "+httpResult);65 weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");66 city=Common.getJsonValue(weatherinfo, "city");67 Reporter.log("

【用例结果】: resultCode=>expected: " + expect_city + " ,actual: "+ city+"

");68 Assert.assertEquals(city,expect_city);69 Reporter.log("

"+"------------------------------------------------------------------------------"+"

");71 }72 }

View Code

报表文件示例

报表html文件位置在如下所示:

代码实现如上就完成,需要配置pom.xml文件和testng.xml文件,可参照如下:

pom.xml

pom.xml文件是下载依赖包的,特别方便

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数据,所以必须导入json依赖包:

1

2 org.json

3 json

4 20171018

5

testng.xml

testng.xml文件是用于运行的,运行程序直接运行该文件即可:

1 <?xml version="1.0" encoding="UTF-8"?>

2

3

4

5

6

7

8

9

10

11

12

13

View Code

问题总结

自己在完成过程中,过程中遇到如下问题:

1、接口返回的数据是乱码

如下所示:

解决办法:

在CityWeather类中代码下加上如下代码即可:

public staticString formatString(String s) {if (s != null) {

s= s.replaceAll("\ufeff", "");

}returns;

}

2、Common类中导包错误

Common类中代码导入如下包,运行程序报错

importorg.json.JSONException;import org.json.JSONObject;

报错为:

解决办法为:

重新导入JSON包即可,如下:

importnet.sf.json.JSONException;import net.sf.json.JSONObject;

3、注意:测试报告美化是依赖ReportNG包的,切莫忘记

本文仅代表作者观点,系作者@温一壶清酒发表。

欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

文章出处:http://www.cnblogs.com/hong-fithing/

java post接口测试_接口测试——Java + TestNG 国家气象局接口(json解析)实例相关推荐

  1. java jsoup解析开彩网api接口json数据实例

    https://www.cnblogs.com/zdz8207/p/7288830.html 开彩网apiJava技术学习 https://www.itkc8.com json-lib-2.4-jdk ...

  2. java正则表达式 匹配()_学习Java正则表达式(匹配、替换、查找)

    import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; public c ...

  3. java核心教程_核心Java教程

    java核心教程 Welcome to Core Java Tutorial. I have written a lot on Core Java and Java EE frameworks. Th ...

  4. java 包命名_【Java】包的命名规则

    命名规范 包:所有单词的字母小写,之后每个单词用-隔开,如 org.nemo.demo 常量:所有单词的字母大写,之后每个单词用_隔开,如 FLAG 类:所有单词的首字母大写,如 TestJava p ...

  5. java 图片效果_使用Java进行图片底片化效果处理

    使用java代码读取图片,并进行底片化处理 util importjava.awt.image.BufferedImage;importjava.io.File;importjava.util.Arr ...

  6. java array缓存_有java数组

    [JAVA零基础入门系列]Day10 Java中的数组 [JAVA零基础入门系列](已完结)导航目录 Day1 开发环境搭建 Day2 Java集成开发环境IDEA Day3 Java基本数据类型 D ...

  7. java检测工具_常用Java代码质量检测评估工具

    常用Java代码质量检测评估工具 1. PMD from http://pmd.sourceforge.net/ PMD能够扫描Java 源代码,查找类似以下的潜在问题: 可能的bug--try/ca ...

  8. java 分割一个_分割java

    [java]分割字符串工具类,霸气 jdk自带的 java 分割字符串,分割string,可以根据多个条件去分割.比如逗号,分号,逗号或者分号. 比如一个字符串:"abc,def;gh,ij ...

  9. java 字符串包_包java字符串

    Java核心技术卷I基础知识3.6.3 不可变字符串 3.6.3 不可变字符串 String类没有提供用于修改字符串的方法.如果希望将greeting的内容修改为"Help!",不 ...

  10. java path类_基于java Files类和Paths类的用法(详解)

    Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream; import java.nio.file.FileSystem ...

最新文章

  1. *2 echo、printf、mkdir命令的应用
  2. BZOJ4122 : [Baltic2015]File paths
  3. FL2440移植linux内核常用命令(command)
  4. 真空压力变送器怎么样零点标定_恒压供水设备中液位变送器分类及工作原理
  5. c# 简单的科学计算
  6. Linux开启服务器问题(李蕾问题)
  7. 用计算机画出方格表,方格造型图_怎么做这种颜色相间的方格图(有图)_彩妆阁...
  8. 用R3写卡成PRL302,解决新电信卡PRL301只能1X不能上3G问题
  9. 2万字总结《MybatisPlus—为简化开发而生》
  10. C++ STL 数据结构与算法 —— 排序
  11. 从linux使用sz命令下载大于4g的文件到windows
  12. 自由空间模型损耗计算详细说明
  13. 全国计算机信息处理技术员报名官网入口,信息处理技术员考试,就是这么简单!...
  14. VS2015安装教程(带图解+下载地址+超详细)
  15. GoFW|网页加速器
  16. python和按键精灵自动化测试_IOS开发入门之iOS自动化测试需求实现(iOS按键精灵类似)...
  17. 《三国演义》中的火烧赤壁居然也是假的!!
  18. [论文阅读]Road Mapping and Localization using Sparse Semantic Visual Features
  19. QT tableview内置控件
  20. matlab绘四叶玫瑰线,玫瑰线 - calculus的日志 - 网易博客

热门文章

  1. MindArmour差分隐私
  2. CEVA引入新的可配置传感器集线器DSP架
  3. C/C++语言编程的隐患!
  4. 客快物流大数据项目(十六):使用脚本创建镜像
  5. 打开word出现“您正试图运行的函数包含有宏或需要宏语言支持的内容。”的解决方法
  6. HarmonyOS ScrollView 不滑动的问题
  7. C++ new 的使用
  8. Android TextView 在strings 里面 实现换行
  9. android 在设备上安装apk包
  10. CoordinatorLayout 的jar包位置