HttpClient + Testng实现接口测试
Posted Bamboo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpClient + Testng实现接口测试相关的知识,希望对你有一定的参考价值。
HttpClient教程 : https://www.yeetrack.com/?p=779
一,所需要的环境:
1,testng 、httpclient和相关的依赖包
二、使用HttpClient登陆CSDN接口,使用testng作为单元测试框架:
1 import org.apache.http.HttpEntity; 2 import org.apache.http.HttpResponse; 3 import org.apache.http.NameValuePair; 4 import org.apache.http.client.HttpClient; 5 import org.apache.http.client.entity.UrlEncodedFormEntity; 6 import org.apache.http.client.methods.HttpPost; 7 import org.apache.http.impl.client.DefaultHttpClient; 8 import org.apache.http.message.BasicNameValuePair; 9 import org.apache.http.util.EntityUtils; 10 import org.testng.annotations.Test; 11 12 import java.util.ArrayList; 13 import java.util.List; 14 15 public class FirstTest { 16 17 private HttpClient httpClient = new DefaultHttpClient(); 18 private HttpPost httppost; 19 private HttpResponse response; 20 private HttpEntity entity; 21 private String postResult = null; 22 23 @Test 24 public void loginJDTest() { 25 26 String loginURL = "https://passport.jd.com/uc/loginService?uuid=ac394a05-05c0-4667-9940-2848f0c03809&&r=0.8636558873495089&version=2015"; 27 //创建一个httppost请求 28 httppost = new HttpPost(loginURL); 29 30 //创建Post请求参数 31 List<NameValuePair> formparams1 = new ArrayList<NameValuePair>(); 32 formparams1.add(new BasicNameValuePair("uuid", "ac394a05-05c0-4667-9940-2848f0c03809")); 33 formparams1.add(new BasicNameValuePair("eid", "Z6RZKBBPUUOORPHGELPNEZE6QHFEIE2IS4EQEBWOKH7VJAPDRIYUCSERJ3DLPNXRINL2ON3JC3IWHIG6L6PQMINSRY")); 34 formparams1.add(new BasicNameValuePair("fp", "4d981fbd1f0cf45fc497eac85348c0f3")); 35 formparams1.add(new BasicNameValuePair("_t", "_nthEAWj")); 36 formparams1.add(new BasicNameValuePair("loginType", "c")); 37 formparams1.add(new BasicNameValuePair("loginname", "test")); 38 formparams1.add(new BasicNameValuePair("nloginpwd", "test")); 39 formparams1.add(new BasicNameValuePair("chkRememberMe", "")); 40 formparams1.add(new BasicNameValuePair("authcode", "")); 41 formparams1.add(new BasicNameValuePair("pubKey", "")); 42 formparams1.add(new BasicNameValuePair("sa_token", "B68C442BE645754F33277E701208059033541C4BBD4BE7A99A94927BD4C8D683E0AC371429CD887688E05A86C51F7C70CE1F231D2A69583750E3923BA39E17118B83A1A1535BAA4F550CDAFD8E0D42172C6687DE87E4A85EA190DE5B35DA60BB878C558875475CC8D7AC4EA721F380156DD8325B976C3C3266E677EACB204C70C0AE07DF75629F5D845602B5968A00D446F66A103CEE76CED0ED85D144B22580F9A9997B4C6606BBA4ACDA2E5675E1753923A7ED53A8CF7426C57380A1EEB3ACC25E396C9D72455D2F8A2EE727961C86C40637A355F83A8A4AC98AB4DB238EA80705100E51EAD01C3E00868DBD94D782ECF89A937893739D7EB48CD4A388D6DA51AF477822A57E7C2708E31DCEB0ADCA88A95D3FB8D20278B263B38D3679DFCD935E41A9612638671D699F59F0E494BF90B522095FF1E38DDB16541245EDE6703833CD15F1AE6D59575C55942B5306C749927362E5D3D7643BA060F53ACFC46F7222511019EBABBCA226F656DC896C7655A90663537C9BBB13657879FA7B99474D4EC9DB2879B409FCE973FA0A9E4E60F4160C3961B55C3401C7CC402917F6A0984544D666FB2C40B6001E1D740C97DAD646E692403DB4A3191F9C638549616672180317153D3EDC2FA55EA740BC4BAE3F5032FD1E3CE7BDA785DDFD5B65AFD0A4CAC07B18AB8B48FFE65E6C25D8E9D9C6B89D4AC495C2FE7937233EC85C60C8C3EA4A9368697EB136B02766908846FE18CF29CA73A07A0D9C3EA0EEB7A75369")); 43 formparams1.add(new BasicNameValuePair("seqSid", "3845068114278942000")); 44 45 try { 46 httppost.setEntity(new UrlEncodedFormEntity(formparams1, "UTF-8")); 47 response = httpClient.execute(httppost); 48 entity = response.getEntity(); 49 // 在这里可以用Jsoup之类的工具对返回结果进行分析,以判断创建是否成功 50 postResult = EntityUtils.toString(entity, "UTF-8"); 51 52 System.out.println("查看登录接口请求返回的结果:" + postResult); 53 } catch (Exception e) { 54 e.printStackTrace(); 55 } 56 57 httppost.releaseConnection(); 58 } 59 }
三、通过testng启动测试类:
右键单击,选择“Run “loginJDTest”
运行后的结果如下:
以上是关于HttpClient + Testng实现接口测试的主要内容,如果未能解决你的问题,请参考以下文章
最全面Java接口自动化测试实战 HttpClient+TestNG+Mock+MyBatis
基于Java+HttpClient+TestNG的接口自动化测试框架-------随机函数的处理
接口自动化:HttpClient + TestNG + Java - 第一个接口测试:get请求
接口自动化:HttpClient + TestNG + Java - 接口测试概述+自动化环境搭建