1、配置JDK
見另一篇博客:http://www.cnblogs.com/testlurunxiu/p/5933912.html
2、安裝Eclipse以及TestNG
Eclipse下載地址:http://beust.com/eclipse
TestNG安裝過程:
在線安裝
輸入網址:http://beust.com/eclipse
在線安裝會比較慢,有的人可能還會鏈接不上這個地址,所以下面介紹一個離線下載的方法
離線下載:TestNG Eclipse 插件下載地址http://testng.org/doc/download.html
a.下載離線安裝包並解壓
b.將解壓后的文件..\eclipse-testng離線包\features\目錄下的文件夾org.testng.eclipse_6.8.6.20130607_0745放到eclipse--》features目錄下;
c.將解壓后的文件..\eclipse-testng離線包\org.testng.eclipse_6.8.6.20130607_0745文件夾放到eclipse--》plugins目錄下;
d.重啟eclipse.
如何查看testng是否安裝成功了呢?
3、接口測試框架的搭建
新建一個maven程序
Finish之后,工程以及默認pxm.xml文件內容,如圖所示:
在pom.xml文件里面導入需要的jar包依賴,類似如下代碼
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
導入TestNG依賴包
新建testng class文件
新建的testng自動生成如下,其中<class>節點里面的為運行內容
導入成功之后的項目工程如下:
4、接口測試用例
獲取並且執行接口代碼如下:
public class HttpUtils { static CloseableHttpClient httpclient =null; public static void OpenHttpClient() {
//打開瀏覽器 httpclient = HttpClients.createDefault(); } public static void CloseHttpClient() {
//關閉瀏覽器 try { httpclient.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } httpclient = null; } public static JSONObject visitUrl(String url) { //CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // HttpPost httpPost = new HttpPost(url); JSONObject jsonObj=null; try { CloseableHttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); StringBuilder jsonStr = new StringBuilder(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8 * 1024); String line = null; while ((line = bufferedReader.readLine()) != null) { jsonStr.append(line + "/n"); } EntityUtils.consume(entity); //獲取JSON對象的值 jsonObj = new JSONObject(jsonStr.toString()); response.close(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObj; } }
測試用例代碼:
public class Test {
public Assertion assertion; @BeforeClass public void beforeClass() { assertion = new Assertion(); } @BeforeMethod public void runBeforeMethod() { // 打開httpclient,相當於打開一個瀏覽器 HttpUtils.OpenHttpClient();//這邊一定要記得在測試用例開始之前打開瀏覽器,否則會出現空指針的錯誤 } @AfterMethod public void runAfterMethod() { // 打開httpclient,相當於打開一個瀏覽器 HttpUtils.CloseHttpClient(); } @org.testng.annotations.Test public void f() throws ClientProtocolException, IOException { String loginUrl = "http://xx.xxx.cn/Org/PCUserLogin.do?u=11111&p=1111&groupId=1"; JSONObject json = HttpUtils.visitUrl(loginUrl); boolean success = json.getBoolean("success"); String enterTrainningUrl = "http://xx.xxx.cn/Training/enterTrainingCamp.do?roomid=1111"; System.out.println(enterTrainningUrl); JSONObject enterObj = HttpUtils.visitUrl(enterTrainningUrl); System.out.println(enterObj.toString()); boolean success2 = enterObj.getBoolean("success"); assertion.assertTrue(success); }
}
右鍵單擊testng.xml運行
結果如下,passed
運行完成之后,刷新工程,在根目錄下會生成一個test_output文件夾,打開index.html,可以看見測試報告