環境搭建
1. 須要lib包
httpclient-4.2.5.jar -- http請求
jettison.jar -- 組裝解析Json
ojdbc7.jar -- 數據庫操做
testng.jar -- 測試用例執行與結果斷言,無需單獨下載,安裝eclipse插件便可
reportng-1.1.4.jar -- testng測試報告美化插件
velocity-dep-1.4.jar -- reportng-1.1.4.jar的依賴庫
guice-4.0.jar -- reportng-1.1.4.jar的依賴庫
2. 相關配置安裝
2.1 Eclipse安裝testng插件:
安裝:Eclipse --> Help --> Install NewSoftWare --> Add
2.2 美化測試報告插件Reportng配置:
配置:Eclipse --> Window --> Preferences --> testng
勾選Disable default listeners
PreDefinedListeners輸入框中輸入
org.uncommons.reportng.HTMLReporter
2.3 使用Reportng後測試報告結果路徑:
test-output --> html
TestNG簡介與實例
1. TestNG與Junit對比
1.1 Junit缺點:
● 最初的設計,使用於單元測試,如今只用於各類測試;html
● 不能依賴測試;前端
● 配置控制欠佳(安裝/拆卸);java
● 侵入性(強制擴展類,並以某種方式命名方法);web
● 靜態編程模型(沒必要要的從新編譯);數據庫
● 不適合管理複雜項目應用,JUnit複雜項目中測試很是棘手。編程
1.2 TestNG是什麼?
TestNG按照其文檔的定義是:json
● TestNG是一個測試框架,其靈感來自Junit和NUnit的,但引入了一些新功能,使其功能更強大,使用更方便。微信
● TestNG是一個開源自動化測試框架,TestNG表示下一代。TestNG是相似於Junit(特別是Junit4),但它不是一個Junit擴展。它的靈感來源於Junit。它的目的是優於Junit的,尤爲是當測試集成的類。多線程
● TestNG消除了大部分的舊框架的限制,使開發人員可以編寫更加靈活和強大的測試。由於它在很大程度上借鑑了Java註解(JDK5.0引入的)來定義的測試,它也能夠告訴你如何使用這個新功能在真實的Java語言生產環境中。app
1.3 TestNG特色
● 註解
● TestNG使用Java和麪向對象的功能;
● 支持綜合類測試(例如,默認狀況下,不用建立一個新的測試每一個測試方法的類的實例);
● 獨立的編譯時測試代碼和運行時配置/數據信息
● 靈活的運行時配置;
● 主要介紹「測試組」。當編譯測試,只要要求TestNG運行全部的「前端」的測試,或「快」,「慢」,「數據庫」等;
● 支持依賴測試方法,並行測試,負載測試,局部故障;
● 靈活的插件API;
● 支持多線程測試。
2. TestNG註解與基礎實例
2.1 註解
註解 | 描述 |
---|---|
@BeforeSuite | 註解的方法將只運行一次,運行全部測試前此套件中。 |
@AfterSuite | 註解的方法將只運行一次此套件中的全部測試都運行以後。 |
@BeforeClass | 註解的方法將只運行一次先行先試在當前類中的方法調用。 |
@AfterClass | 註解的方法將只運行一次後已經運行在當前類中的全部測試方法。 |
@BeforeTest | 註解的方法將被運行以前的任何測試方法屬於內部類的標籤的運行。 |
@AfterTest | 註解的方法將被運行後,全部的測試方法,屬於內部類的標籤的運行。 |
@BeforeGroups | 按組( @Test(groups= "findyou") )運行時,此註解在組(findyou組)執行以前運行,可作組(findyou組)執行以前,初始化數據準備類工做。 |
@AfterGroups | 按組( @Test(groups= "findyou") )運行時,此註解在組(findyou組)執行以後運行,可作組(findyou)執行以後,數據還原類工做。 |
@BeforeMethod | 註解的方法將每一個測試方法以前運行。 |
@AfterMethod | 被註釋的方法將被運行後,每一個測試方法。 |
@DataProvider | 標誌着一個方法,提供數據的一個測試方法。註解的方法必須返回一個Object[] [],其中每一個對象[]的測試方法的參數列表中能夠分配。該@Test 方法,但願從這個DataProvider的接收數據,須要使用一個dataProvider名稱等於這個註解的名字。 |
@Factory | 做爲一個工廠,返回TestNG的測試類的對象將被用於標記的方法。該方法必須返回Object[]。 |
@Listeners | 定義一個測試類的監聽器。 |
@Parameters | 介紹如何將參數傳遞給@Test方法。 |
@Test | 標記一個類或方法做爲測試的一部分。 |
2.2 基礎實例-1
2.2.1 新建java工程
1.新建:NEW-->JAVA Project-->輸入工程名稱-->Finish
2.引入lib庫
2.2.2 編寫測試用例
package a.testcase;
import org.testng.annotataions.Test;
public class TestCaseStudy{
//testcase1
@Test
public void testCase1(){
System.out.println("in testcase1")}
}
2.2.3 執行用例
執行:右鍵java文件-->Run as-->TestNG
2.2.4 查看測試報告
在項目文件的test-output文件內查看測試報告
2.3 基礎實例-2
2.3.1 編寫測試用例
package a.testcase;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class TestngStudy{
//test case 1
@Test
public void testcase1(){
System.out.println("This is a test case 1")}
//test case 2
@Test
public void testcase2(){
System.out.println("This is a test case 2")}}
@BeforeMethod
public void beforeMethod(){
System.out.println("This is beforeMethod")}
@AfterMethod
public void afterMethod(){
System.out.println("This is afterMethod")}
@BeforeClass
public void beforeClass(){
System.out.println("This is beforeClass")}
@AfterClass
public void afterClass(){
System.out.println("This is afterClass")}
@BeforeTest
public void beforeTest(){
System.out.println("This is beforeTest")}
@AfterTest
public void afterTest(){
System.out.println("This is afterTest")}
@BeforeSuite
public void beforeSuite(){
System.out.println("This is beforeSuite")}
@AfterSuite
public void afterSuite(){
System.out.println("This is afterSuite")}
}
2.3.2 執行用例結果
3. 實例應用
3.1 待測接口說明
例:北京市天氣
1. 接口地址:http://www.weather.com.cn/data/cityinfo/101010100.html
2. 請求方式:get
3. 請求結果:
{
"weatherinfo": {
"city": "北京",
"cityid": "101010100",
"temp1": "18℃",
"temp2": "31℃",
"weather": "多雲轉陰",
"img1": "n1.gif",
"img2": "d2.gif",
"ptime": "18:00"
}
}
4. 請求對應cityid代碼,返回城市是否爲預期城市。
3.2 新建java工程
3.2.1 工程結構說明
--httpAPITest
--src
--m.Interface
--Common.java --公共方法:JSON解析
--getCityWeather.java --請求接口進行封裝,根據傳入CityID組裝不一樣的請求地址,進行請求,並返回結果數據
--URLConnection.java --HTTP請求頭封裝
3.2 Common.java代碼
package m.Interface
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
public class Common{
public static String getJsonValue(String JsonString, String JsonId){
String JsonValue = "";
if(JsonString == null || JsonString.trim().length() < 1){
return null;
}
try{
JSONObject obj1 = new JSONObject(JsonString);
JsonValue = (String) object1.getString(JsonId);
} catch(JSONException e){
e.printStackTrace();
}
}
}
3.3 getCityWeather.java代碼
package m.Interface;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
public class getCityWeather{
private String url = "";
public String geturl(){
return url;
}
public String getHttpResponse(String cityCode) thorws IOException{
String line = "";
String httpResults = "";
url = ("http://www.weather.com.cn/data/cityinfo/" + cityCode + ".html");
try{
HttpURLConnection connection = URLConnection.getConnection(url);
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
while((line = reader.readLine()) != null){
httpResults = httpResults + line.toString();
}
reader.close();
// 斷開鏈接
connection.disconnect();
}catch(Execption e ){
e.printStackTrace();
}
return httpResults;
}
}
3.4 URLConnection.java代碼
package m.Interface;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLConnection{
public static HttpURLConnection getConnection(String url){
HttpURLConnection connection = null;
try{
//打開和URL之間的連續
URL postUrl = new URL(url);
connection = (HttpURLConnection) postUrl.openConnection();
//設置通用的請求屬性
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setInstanceFollowRedirets(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Charset","utf-8");
connection.setRequestProperty("Accept-Charset", "utf-8");
} catch(Exception e ){
e.printStackTrace();
}
return connection;
}
}
3.5 測試用例編寫
package m.testcase;
import java.io.IOException;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotation.Test;
import m.Interface.Common;
import m.Interface.getCityWeather;
public class test{
public String httpResult = null, weatherinfo = null, city = null, exp_city = null;
public static String cityCode = "";
getCityWeather weather = new getCityWeather();
@Test(group = {"BaseCase"})
public void getShenZhen_Succ() throws IOException{
exp_city = "深圳";
cityCode = "101280601"
resultCheck(cityCode, exp_city);
@Test(group = {"BaseCase"})
public void getBeiJing_Succ() throws IOException{
exp_city = "北京";
cityCode = "101010100";
resultCheck(cityCode, exp_city);
}
@Test(group = {"BaseCase"})
public void getShangHai_Succ() throws IOException{
exp_city = "上海";
cityCode = "101020100";
resultCheck(cityCode, exp_city);
}
public void resultCheck(String cityCode_str, String exp_city_str) throws IOException{
Reporter.log("【正經常使用例】:獲取" + exp_city_str + "天氣成功!");
httpResult = weather.getHttpResponse(cityCode_str);
Reporter.log("請求地址:" + weather.geturl());
Reporter.log("返回結果:" + httpResult);
weatherinfo = Common.getJsonValue(httpResult, "weatherinfo");
city = Common.getJsonValue(weatherinfo, "city");
Reporter.log("用例結果:resultCode=>expected:" + exp_city_str + " ,actual:" + city);
Assert.assertEquals(city,exp_city_str
}
}
3.6 測試結果
搜狗測試微信號:Qa_xiaoming
搜狗測試QQ粉絲羣:459645679
本文分享自微信公衆號 - 搜狗測試(SogouQA)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。