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

使用到的工具是:Eclipse + TestNG + Maven + ReportNG,全国城市编码:http://www.cnblogs.com/oucbl/p/6138963.html,接口地址:http://www.weather.com.cn/data/cityinfo/城市编码.html

先看一下代码架构,如下所示:

建的是maven工程,个人觉得这样下载依赖包较方便。工程科分为四部分:功能代码,测试case,报表文件和配置文件。网络上也有很多这样的实例,我只是列举些我在做的过程中所遇到的问题吧,也当一个记录。maven不会配置,可参见我之前写的随笔。

功能代码

Common

 package com.CityWether.CityInfo;

 import net.sf.json.JSONException;
import net.sf.json.JSONObject; public class Common {
public static String getJsonValue(String JsonString, String JsonId) {
String JsonValue = "";
//trim()去掉字符串首尾的空格
if (JsonString == null || JsonString.trim().length() < 1) {
return null;
}
try {
JSONObject obj1 = new JSONObject(JsonString);
JsonValue = obj1.getString(JsonId);
} catch (JSONException e) {
e.printStackTrace();
}
return JsonValue;
}
}

URLConnection

 package com.CityWether.CityInfo;

 import java.net.HttpURLConnection;
import java.net.URL; public class URLConnection {
public static HttpURLConnection getConnection(String url){
HttpURLConnection connection = null;
try {
// 打开和URL之间的连接
URL postUrl = new URL(url);
connection = (HttpURLConnection) postUrl.openConnection();
// 设置通用的请求属性
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Charset", "utf-8");
connection.setRequestProperty("Accept-Charset", "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}

CityWeather

 package com.CityWether.CityInfo;

 import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection; public class CityWeather {
private String url=""; public String geturl() {
return url;
} public static String formatString(String s) {
if (s != null) {
s = s.replaceAll("\ufeff", "");
}
return s;
} public String getHttpRespone(String cityCode) throws IOException {
String line = "";
String httpResults = "";
url=("http://www.weather.com.cn/data/cityinfo/" + cityCode + ".html");
try {
HttpURLConnection connection = URLConnection.getConnection(url);
// 建立实际的连接
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
httpResults = httpResults + line.toString();
}
reader.close();
// 断开连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return httpResults;
}
}

测试case

 package com.CityWether.CityInfo;

 import java.io.IOException;

 import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test; import com.CityWether.CityInfo.CityWeather;
import com.CityWether.CityInfo.Common; public class TestCase {
public String httpResult= null, weatherinfo= null, city=null,expect_city = null;
public static String cityCode="";
CityWeather weather=new CityWeather(); @Test(priority=0)
public void getHuaihua() throws IOException{
expect_city="怀化";
cityCode="101251201";
resultCheck(cityCode, expect_city);
} @Test(priority=1)
public void getHuitong() throws IOException{
expect_city="会同";
cityCode="101251206";
resultCheck(cityCode, expect_city);
} @Test(priority=2)
public void getChangsha() throws IOException{
expect_city="长沙";
cityCode="101250101";
resultCheck(cityCode, expect_city);
} @Test(priority=3)
public void getBaoshan() throws IOException{
expect_city="宝山";
cityCode="101020300";
resultCheck(cityCode, expect_city);
} @Test(priority=4)
public void getShanghai() throws IOException{
expect_city="上海";
cityCode="101020100";
resultCheck(cityCode, expect_city);
} @Test(priority=5)
public void Minhang() throws IOException{
expect_city="闵行";
cityCode="101020200";
resultCheck(cityCode, expect_city);
} public void resultCheck(String cityCode, String expect_city) throws IOException{
System.setProperty("org.uncommons.reportng.escape-output", "false");
Reporter.log("【正常用例】:获取"+expect_city+"天气成功!");
httpResult=weather.getHttpRespone(cityCode);
Reporter.log("<p><span style=\"color:#FF0000\">请求地址: "+weather.geturl()+"</span></p>");
Reporter.log("【返回结果】: "+httpResult);
weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
city=Common.getJsonValue(weatherinfo, "city");
Reporter.log("<p>【用例结果】: resultCode=>expected: " + expect_city + " ,actual: "+ city+"</p>");
Assert.assertEquals(city,expect_city);
Reporter.log("<p></p>");
Reporter.log("<p>"+"------------------------------------------------------------------------------"+"</p>");
}
}

报表文件示例

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

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

pom.xml

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

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com</groupId>
<artifactId>CityWether</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>CityWether</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
</dependency>
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity-dep</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>

这里需要注意的是,由于接口返回的是json数据,所以必须导入json依赖包:

 <dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>

testng.xml

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

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="suite1">
<test name="test1">
<classes>
<class name="com.CityWether.CityInfo.TestCase" />
</classes>
</test>
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</suite>

问题总结

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

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

如下所示:

经百度科普,是因为BOM报头报错,可参见该博文:http://blog.csdn.net/u012519664/article/details/41596857?%3E,里面有详细介绍。

解决办法:

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

  public static String formatString(String s) {
if (s != null) {
s = s.replaceAll("\ufeff", "");
}
return s;
}

2、Common类中导包错误

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

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

报错为:

解决办法为:

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

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

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

接口测试——Java + TestNG 国家气象局接口(json解析)实例的更多相关文章

  1. json-lib-2.4-jdk15.jar所需全部JAR包.rar java jsoup解析开彩网api接口json数据实例

    json-lib-2.4-jdk15.jar所需全部JAR包.rar  java jsoup解析开彩网api接口json数据实例 json-lib-2.4-jdk15.jar所需全部JAR包.rar  ...

  2. JSON解析实例——使用Json-lib

    JSON解析实例——使用Json-lib Json-lib下载及使用 本文介绍用一个类库进行JSON解析. 工具下载地址:http://sourceforge.net/projects/json-li ...

  3. Java进阶学习:JSON解析利器JackSon

    Java:JSON解析利器JackSon JackSon基础 1.Maven项目引入 <!-- https://mvnrepository.com/artifact/org.codehaus.j ...

  4. 接口测试02 - 无法绕过的json解析

    概述: 先瞧一下什么是json.JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式. 它基于ECMAScript(w3c定制的js规范)的一个子集 ...

  5. java中常见的json解析方法、库以及性能对比

    常见的json解析有原生的JSONObject和JSONArray方法,谷歌的GSON库,阿里的fastjson,还有jackson,json-lib. Gson(项目地址:https://githu ...

  6. Java 自定义注解与注解解析实例

    在学习Java之后会遇到很多的注解,有加载JavaBean的注解:@Component,@Service,@Controller:有获取配置文件中数值的注解@Value:有获取Http请求的数据的注解 ...

  7. 接口测试-Java代码实现接口请求并封装

    前言:在接口测试和Java开发中对接口请求方法进行封装都非常有必要,无论是在我们接口测试的时候还是在开发自测,以及调用某些第三方接口时,都能为我们调用和调试接口提供便捷: Java实现对http请求的 ...

  8. http后台json解析实例

    localhost:8080/hbinterface/orderInterface/sIReverseAccept.do?bizType=4&&bnetAccount=ESBTEST2 ...

  9. Java面向对象之接口interface 入门实例

    一.基础概念 (一)接口可以简单的理解为,是一个特殊的抽象类,该抽象类中的方法都是抽象的. 接口中的成员有两种:1.全局常量 2.抽象方法 定义接口用关键字interface,接口中的成员都用固定的修 ...

随机推荐

  1. spring boot https --restful接口篇

    我们写的接口默认都是http形式的,不过我们的接口很容易被人抓包,而且一抓全是明文的挺尴尬的 spring boot配置https生成证书大的方向有3种: 1.利用keytool自己生成证书 2.从免 ...

  2. Python中import机制

    Python语言中import的使用很简单,直接使用import module_name语句导入即可.这里我主要写一下"import"的本质. Python官方定义:Python ...

  3. 指针变量的*p,p以及&p的区别

    23/7/2017  决定夯实基础,回顾指针的知识点,该博文转载于CSDN博主百家晓东,小部分修改,外加编译图引证 正文如下: 以下面的情况说明: int a = 5; int* p = &a ...

  4. DeepLearning.ai学习笔记(三)结构化机器学习项目--week2机器学习策略(2)

    一.进行误差分析 很多时候我们发现训练出来的模型有误差后,就会一股脑的想着法子去减少误差.想法固然好,但是有点headlong~ 这节视频中吴大大介绍了一个比较科学的方法,具体的看下面的例子 还是以猫 ...

  5. 掌握numpy(二)

    目录 掌握numpy(一) 掌握numpy(二) 掌握numpy(三) 掌握numpy(四) 数组的reshape 顾名思义,就是对数组的形状进行改变,比如行变成列,一行变多行等. in place ...

  6. HNOI2013 BZOJ3142 数列

    尝试用Markdown写一篇博客 3142: [Hnoi2013]数列 Description 小T最近在学着买股票,他得到内部消息:F公司的股票将会疯涨.股票每天的价格已知是正整数,并且由于客观上的 ...

  7. LODOP打印控件示例

    一.lodop打印预览效果图 LODOP.PRINT_SETUP();打印维护效果图 LODOP.PREVIEW();打印预览图 二.写在前面 最近项目用到了LODOP的套打,主要用到两个地方,一是物 ...

  8. MySQL数据库学习02: SELECT语句

    声明:本篇文章大多数内容出自<MySQL必知必会>,仅供学习参考,勿作他用! 第4章 检索数据 4.1 SELECT语句 SELECT子句用于检索数据库中的表数据.它几乎是MySQL中最常 ...

  9. 使用C#开发数据库应用系统 习题

    错题积累 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:

  10. html5 storage事件

    HTML5 虽然很多年了,但是真的了解不不够不够.主题说的是 storage时间,说起对 storage 事件的了解还是从 QQ音乐 说起. QQ音乐的主页是 https://y.qq.com , 而 ...