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 后得到测试报告