1. 程式人生 > >JAVA+Maven+TestNG搭建介面測試框架及例項

JAVA+Maven+TestNG搭建介面測試框架及例項

1、配置JDK

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,可以看見測試報告