欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java+testng+json实现测试接口

程序员文章站 2022-06-04 12:06:24
...

接口测试

.java+testng+json实现(天气接口测试)
testng集成(连续运行多个用例,执行多个脚本)
testng:下一代测试技术,是一个插件
管理用例,生成测试报告
eclipse安装testng
1.下载、解压testng插件,将plugins文件夹下的文件夹放到eclipse对应的plugins目下
2.重启eclipse
3.项目中引入testng 右键项目 --BuildPath --AddLibraries–选择testng
json需要导入包
GET请求:
java+testng+json实现测试接口

package com.qiyi.JieKou;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONObject;
import org.testng.Assert;
import org.xml.sax.InputSource;

public class TestBeiJing {

	public static void main(String[] args) {
		// 设置请求地址
		String path="http://www.weather.com.cn/data/sk/101010100.html";
		// 通过地址创建一个url对象
		URL url;
		try {
			url = new URL(path);
			//打开网络连接
			HttpURLConnection conn= (HttpURLConnection) url.openConnection();
			//设置请求超时时间
			conn.setConnectTimeout(30000);//30秒
			//设置请求方式(默认的是get请求)
			conn.setRequestMethod("GET");
			//获取响应状态码,如果是200,正常返回数据
			if(conn.getResponseCode() == 200){
				//获取相应的输入流对象
				InputStream inputStream = conn.getInputStream();
				//创建一个reader对象(字节流转字符流)
				InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
				//读取服务器返回信息
				BufferedReader reader = new BufferedReader(inputStreamReader);
				String line = "";
				StringBuffer sb = new StringBuffer();
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				System.out.println(sb);
				// 关闭流
				reader.close();
				// 断言
				// Assert.assertTrue(sb.toString().contains("北京"));
				Assert.assertTrue(sb.toString().contains("101010100"), "测试北京天气预报");
				
				
				// 解析json数据
				JSONObject js = new JSONObject(sb.toString());
				// 通过weatherinfo获取值,值是一个对象,所以使用getJSONObject
				JSONObject joo = js.getJSONObject("weatherinfo");
				String cityId = joo.getString("cityid");
				Assert.assertEquals(cityId, "101010100");
			}
			// 关闭链接
				conn.disconnect();
					
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
	}

}

post请求
java+testng+json实现测试接口

package com.qiyi.JieKou;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.Buffer;

public class TestLogin {

	public static void main(String[] args) {
		// 设置请求的地址
		String path = "https://www.ratjin.com/rat/user/login";
		// 根据地址创建一个url对象
		try {
			URL url = new URL(path);
			// 打开一个网络链接
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			// 设置请求的时间
			conn.setConnectTimeout(30000);
			//允许对外输出
			conn.setDoOutput(true);
			// 设置请求方法
			conn.setRequestMethod("POST");
			// 设置请求头
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			//conn.setRequestProperty("Content-Type", "application/json");
			// 获取输出流
			OutputStream out = conn.getOutputStream();
			String content = "passwd=123456789&phone=13087099730";
			out.write(content.getBytes());
			
			//获取返回状态,200正常返回
			if(conn.getResponseCode()==200){
				// 获取输出流对象
				InputStream in = conn.getInputStream();
				InputStreamReader inputStreamReader = new InputStreamReader(in,"utf-8");
				// 读取服务器返回信息
				BufferedReader reader = new BufferedReader(inputStreamReader);
				String line = "";
				StringBuilder sb = new StringBuilder();
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				// 关闭流
				reader.close();
				out.close();
				System.out.println(sb.toString());
			}else{
				
			}
			// 关闭链接
			conn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
}
	}