java接口测试学习_Java接口自动化测试之HTTPClient学习(四)

pom.xml  文件中dependency

org.testng

testng

6.14.3

com.relevantcodes

extentreports

2.41.1

com.vimalselvam

testng-extentsreport

1.3.1

com.aventstack

extentreports

3.0.6

org.apache.httpcomponents

httpclient

4.1.2

com.alibaba

fastjson

1.2.47

compile

org.projectlombok

lombok

1.16.14

application.properties 文件, 配置一些常量, 例如:

# 请求URL

test.uri=http://localhost:8889

test.post.path1=/postDemo

test.post.path2=/postDemoWithCookie

test.get.path1=/getDemo?

test.get.path2=/getDemoWithCookie?

# 请求头信息

header.accept=*/*

header.user.agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36

header.content.type=application/json;charset=utf-8

header.accept.charset=utf-8

header.cookie=login=true

公共类的提取, 例如HttpUtils.java , ReadConfig.java

packagecom.testng.utils;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.methods.HttpRequestBase;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.util.EntityUtils;importjava.io.IOException;public classHttpUtils {private static DefaultHttpClient defaultHttpClient = null;public static String doGet(String url) throwsIOException {

String result;

defaultHttpClient= newDefaultHttpClient();

HttpGet get= newHttpGet(url);

setHeader(get);

result= EntityUtils.toString(defaultHttpClient.execute(get).getEntity(), "utf-8");returnresult;

}public static String doPost(String url, String data) throwsIOException {

String result;

HttpPost post= newHttpPost(url);

setHeader(post);

post.setEntity(new StringEntity(data, "utf-8"));

defaultHttpClient= newDefaultHttpClient();

result= EntityUtils.toString(defaultHttpClient.execute(post).getEntity(), "utf-8");returnresult;

}private static voidsetHeader(HttpRequestBase httpRequestBase) {

httpRequestBase.setHeader("Accept", ReadConfig.ACCEPT);

httpRequestBase.setHeader("User-Agent", ReadConfig.USER_AGENT);

httpRequestBase.setHeader("Content-Type", ReadConfig.CONTENT_TYPE);

httpRequestBase.setHeader("Accept-Charset", ReadConfig.ACCEPT_CHARSET);

httpRequestBase.setHeader("Cookie", ReadConfig.COOKIE);

}

}

packagecom.testng.utils;importlombok.Data;importjava.util.Locale;importjava.util.ResourceBundle;

@Datapublic classReadConfig {private static ResourceBundle bundle = ResourceBundle.getBundle("application", Locale.CHINA);public static String ACCEPT = bundle.getString("header.accept");public static String USER_AGENT = bundle.getString("header.user.agent");public static String CONTENT_TYPE = bundle.getString("header.content.type");public static String ACCEPT_CHARSET = bundle.getString("header.accept.charset");public static String COOKIE = bundle.getString("header.cookie");public static String URI = bundle.getString("test.uri");public static String POST_PATH1 = bundle.getString("test.post.path1");public static String POST_PATH2 = bundle.getString("test.post.path2");public static String GET_PATH1 = bundle.getString("test.get.path1");public static String GET_PATH2 = bundle.getString("test.get.path2");

}

TestNG测试类:

packagecom.testng.utils;importlombok.Data;importjava.util.Locale;importjava.util.ResourceBundle;

@Datapublic classReadConfig {private static ResourceBundle bundle = ResourceBundle.getBundle("application", Locale.CHINA);public static String ACCEPT = bundle.getString("header.accept");public static String USER_AGENT = bundle.getString("header.user.agent");public static String CONTENT_TYPE = bundle.getString("header.content.type");public static String ACCEPT_CHARSET = bundle.getString("header.accept.charset");public static String COOKIE = bundle.getString("header.cookie");public static String URI = bundle.getString("test.uri");public static String POST_PATH1 = bundle.getString("test.post.path1");public static String POST_PATH2 = bundle.getString("test.post.path2");public static String GET_PATH1 = bundle.getString("test.get.path1");public static String GET_PATH2 = bundle.getString("test.get.path2");

}

packagecom.testng.cases;importcom.testng.utils.HttpUtils;importcom.testng.utils.ReadConfig;importorg.testng.Assert;importorg.testng.Reporter;importorg.testng.annotations.Test;importjava.io.IOException;public classDoGetTest {

@Test(description= "成功的案例")public void getTest() throwsIOException {

String uri= ReadConfig.URI +ReadConfig.GET_PATH1;

String param= "name=zhangsan&password=123456";

String url= uri +param;

Reporter.log("请求地址" +url);

String result=HttpUtils.doGet(url);

Reporter.log("请求结果" +result);

Assert.assertEquals(result,"{\"msg\":\"success\",\"status\":1011,\"token\":\"XXXXXXXXXXXXXXXX\"}");

}

@Test(description= "失败的案例")public void getTest2() throwsIOException {

String uri= ReadConfig.URI +ReadConfig.GET_PATH2;

String param= "name=zhangsan&password=1234567";

String url= uri +param;

Reporter.log("请求地址" +url);

String result=HttpUtils.doGet(url);

Reporter.log("请求结果" +result);

Assert.assertEquals(result,"{\"msg\":\"success\",\"status\":1011,\"token\":\"AAAAAAAAAAAAAAAAAAAAAAAA\"}");

}

}

testng.xml

运行testng.xml 后得到测试报告

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值