JAVA+Maven+TestNG搭建接口測試框架及實例

一、配置JDKhtml

見另外一篇博客:http://www.cnblogs.com/testlurunxiu/p/5933912.htmljson

二、安裝Eclipse以及TestNG瀏覽器

Eclipse下載地址:http://beust.com/eclipseapp

TestNG安裝過程:框架

在線安裝eclipse

輸入網址:http://beust.com/eclipsemaven

在線安裝會比較慢,有的人可能還會連接不上這個地址,因此下面介紹一個離線下載的方法測試

離線下載:TestNG Eclipse 插件下載地址http://testng.org/doc/download.htmlui

a.下載離線安裝包並解壓url

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是否安裝成功了呢?

三、接口測試框架的搭建

新建一個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>節點裏面的爲運行內容

導入成功以後的項目工程以下:

 四、接口測試用例

獲取而且執行接口代碼以下:

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,能夠看見測試報告

相關文章
相關標籤/搜索