pom.xml 文件中dependency
<dependencies> <dependency> <groupId>org.testnggroupId> <artifactId>testngartifactId> <version>6.14.3version> dependency> <dependency> <groupId>com.relevantcodesgroupId> <artifactId>extentreportsartifactId> <version>2.41.1version> dependency> <dependency> <groupId>com.vimalselvamgroupId> <artifactId>testng-extentsreportartifactId> <version>1.3.1version> dependency> <dependency> <groupId>com.aventstackgroupId> <artifactId>extentreportsartifactId> <version>3.0.6version> dependency> <dependency> <groupId>org.apache.httpcomponentsgroupId> <artifactId>httpclientartifactId> <version>4.1.2version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>fastjsonartifactId> <version>1.2.47version> <scope>compilescope> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.16.14version> dependency> dependencies>
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
package com.testng.utils; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.io.IOException; public class HttpUtils { private static DefaultHttpClient defaultHttpClient = null; public static String doGet(String url) throws IOException { String result; defaultHttpClient = new DefaultHttpClient(); HttpGet get = new HttpGet(url); setHeader(get); result = EntityUtils.toString(defaultHttpClient.execute(get).getEntity(), "utf-8"); return result; } public static String doPost(String url, String data) throws IOException { String result; HttpPost post = new HttpPost(url); setHeader(post); post.setEntity(new StringEntity(data, "utf-8")); defaultHttpClient = new DefaultHttpClient(); result = EntityUtils.toString(defaultHttpClient.execute(post).getEntity(), "utf-8"); return result; } private static void setHeader(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); } }
package com.testng.utils; import lombok.Data; import java.util.Locale; import java.util.ResourceBundle; @Data public class ReadConfig { 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测试类:
package com.testng.utils; import lombok.Data; import java.util.Locale; import java.util.ResourceBundle; @Data public class ReadConfig { 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"); }
package com.testng.cases; import com.testng.utils.HttpUtils; import com.testng.utils.ReadConfig; import org.testng.Assert; import org.testng.Reporter; import org.testng.annotations.Test; import java.io.IOException; public class DoGetTest { @Test(description = "成功的案例") public void getTest() throws IOException { 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() throws IOException { 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
xml version="1.0" encoding="utf-8" ?> <suite name="your suite name"> <test name="your test name1"> <classes> <class name="com.testng.cases.DoGetTest"/> <class name="com.testng.cases.DoPostTest"/> classes> test> <listeners> <listener class-name="com.testng.config.ExtentTestNGIReporterListener"/> listeners> suite>