java 接口测试 例子_JAVA+Maven+TestNG搭建接口测试框架及实例

java 接口测试 例子_JAVA+Maven+TestNG搭建接口测试框架及实例一、Eclipse安装TestNG(两种安装方式,在线安装以及离线安装)在线安装name:TestNGLocation:http://beust.com/eclipse在线安装会比较慢,有的人可能还会链接不上这个地址,所以下面介绍一个离线下载的方法离线下载:TestNGEclipse插件下载地址http://testng.org/doc/download.htmla.下载离线安装包并解压…

一、Eclipse安装TestNG(两种安装方式,在线安装以及离线安装)

在线安装

11ff3a73cc572c7f2c89daa41aa14026.png

name:TestNG

Location:http://beust.com/eclipse

ec83fcbe91028111a96f322b9ed6df4b.png

a7bf8ad20ae0439637550094512c3d34.png

在线安装会比较慢,有的人可能还会链接不上这个地址,所以下面介绍一个离线下载的方法

离线下载:TestNG Eclipse 插件下载地址http://testng.org/doc/download.html

a.下载离线安装包并解压

b.将解压后的文件..\eclipse-testng离线包\features\目录下的文件夹org.testng.eclipse_6.8.6.20130607_0745放到eclipse–》features目录下;

c.将解压后的文件..\eclipse-testng离线包\org.testng.eclipse_6.8.6.20130607_0745文件夹放到eclipse–》plugins目录下;

d.重启eclipse.

查看testng是否安装成功

file — new  — other —  输入testNG看看是否存在

8d70a53bfe8e7724cba3cb43742f4399.png

测试框架的搭建

470eac09c0237b71041c44c9fc999632.png

904039be163f5cd2f196acfb1f54cbed.png

86c52785ca7ec8c9ffde73c917fec10e.png

2c103fb637b30440f74749431289aebe.png

导入TestNG依赖包,选择项目  右键  —  build path

c3d0ea1824bd89e91558e71e65aaf1f1.png

新建testng class文件 项目右键 new – other

f75c98a1e31bc032047629cbd74242fa.png

bd713e59bf672adbaa0a2f2d964effd3.png

d65cffa8512699d668d747063f30ca08.png

接口测试用例

package com.test.util;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import org.apache.http.HttpEntity;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.json.JSONObject;

public class HttpUtils {

static CloseableHttpClient httpclient = null;

public static void OpenHttpClient()

{

//打开浏览器

httpclient = HttpClients.createDefault();

}

public static void CloseHttpClient()

{

//关闭浏览器

try {

httpclient.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

httpclient = null;

}

public static JSONObject visitUrlPost(String url)

{

//CloseableHttpClient httpclient = HttpClients.createDefault();

//        HttpGet httpGet = new HttpGet(url);

HttpPost httpPost = new HttpPost(url);

//        HttpPost httpPost = new HttpPost(url);

JSONObject jsonObj=null;

try {

//            CloseableHttpResponse response = httpclient.execute(httpGet);

CloseableHttpResponse response = httpclient.execute(httpPost);

HttpEntity entity = response.getEntity();

StringBuilder jsonStr = new StringBuilder();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(), “UTF-8”),1024);

String line = null;

while ((line = bufferedReader.readLine()) != null) {

jsonStr.append(line + “/n”);

}

EntityUtils.consume(entity);

//获取JSON对象的值

jsonObj = new JSONObject(jsonStr.toString());

//            String jsonString = JSONObject.toJSONString(jsonStr.toString());

response.close();

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return jsonObj;

}

public static JSONObject visitUrlGet(String url)

{

//CloseableHttpClient httpclient = HttpClients.createDefault();

HttpGet httpGet = new HttpGet(url);

//        HttpPost httpPost = new HttpPost(url);

JSONObject jsonObj=null;

try {

//            CloseableHttpResponse response = httpclient.execute(httpGet);

CloseableHttpResponse response = httpclient.execute(httpGet);

HttpEntity entity = response.getEntity();

StringBuilder jsonStr = new StringBuilder();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(), “UTF-8”),1024);

String line = null;

while ((line = bufferedReader.readLine()) != null) {

jsonStr.append(line + “/n”);

}

EntityUtils.consume(entity);

//获取JSON对象的值

jsonObj = new JSONObject(jsonStr.toString());

//            String jsonString = JSONObject.toJSONString(jsonStr.toString());

response.close();

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return jsonObj;

}

}

package com.test.one;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import org.json.JSONObject;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeMethod;

import org.testng.asserts.Assertion;

import com.test.util.HttpUtils;

public class test {

public Assertion assertion;

@BeforeClass

public void beforeClass() {

assertion = new Assertion();

}

@BeforeMethod

public void runBeforeMethod() {

// 打开httpclient,相当于打开一个浏览器

HttpUtils.OpenHttpClient();//这边一定要记得在测试用例开始之前打开浏览器,否则会出现空指针的错误

}

@AfterMethod

public void runAfterMethod() {

// 打开httpclient,相当于打开一个浏览器

HttpUtils.CloseHttpClient();

}

@org.testng.annotations.Test

public void f() throws ClientProtocolException, IOException {

String loginUrl = “http://xxx/xxx/login?loginName=admin&passWord=123456”;

JSONObject json = HttpUtils.visitUrlPost(loginUrl);

//            boolean success = json.getBoolean(“success”);

Boolean success = null;

if(json.get(“message”).equals(“success”)){

success = true;

}else{

success = false;

}

//            boolean success = json.getBoolean(“message”);

String enterTrainningUrl = “http://xxx.xxx?appName=&createTimeEnd=&createTimeStart=&creatorName=&page=1&size=5”;

System.out.println(enterTrainningUrl);

JSONObject enterObj = HttpUtils.visitUrlGet(enterTrainningUrl);

System.out.println(enterObj.toString());

Boolean success2 = null;

if(json.get(“message”).equals(“success”)){

success2 = true;

}else{

success2 = false;

}

assertion.assertTrue(success);

}

}

右键单击testng.xml运行

59c28dee6039dae32261d1f889bc1fa6.png

结果如下

a46078726dbada3cee47ba50ba415328.png

运行完成之后,刷新工程,在根目录下会生成一个test_output文件夹,打开index.html,可以看见测试报告

b431c56c6911bfd1103cf594205501cf.png

原文出处:https://www.cnblogs.com/zhangjiahao/p/10083435.html

今天的文章java 接口测试 例子_JAVA+Maven+TestNG搭建接口测试框架及实例分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/27932.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注