java+testng接口测试入门
生活随笔
收集整理的這篇文章主要介紹了
java+testng接口测试入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
testNG是一個測試框架,它能組織測試用例按照你想要的方式進行運行,并輸出一定格式的便于閱讀的測試報告(結果),通過java+testng的方式說明一下接口測試的基本使用方法。
?
一、環境搭建
? a)千年不變的java環境配置,度娘可以提供
? b)千年不變的eclipse包下載,度娘可以提供
? c)在elipse中安裝testng,方法度娘可以提供
二、基于http協議的接口測試編碼
步驟一:eclipse中創建java工程,導入HttpClient對象所屬的jar包,這在我的上一篇文章中已經提及。
步驟二:創建testng類,并默認添加testng.xml文件
步驟三:在類中編寫兩個方法用于實現get和post請求,當然為了能將這兩個方法扔給testng執行,需要在方法明上添加@Test注解
1 public class Login { 2 3 String url = null; 4 HttpClient client = null; 5 6 @Test 7 public void get() throws ClientProtocolException, IOException { 8 9 url += "?userName=xxxx&password=xxxx"; 10 //建立get請求 11 HttpGet get = new HttpGet(url); 12 //發送請求,得到響應 13 HttpResponse response = client.execute(get); 14 //返回響應體 15 HttpEntity entity = response.getEntity(); 16 //將響應體以字符串形式返回 17 String content = EntityUtils.toString(entity); 18 System.out.println((content)); 19 } 20 21 @Test 22 public void post() throws ClientProtocolException, IOException{ 23 //建立post請求 24 HttpPost post = new HttpPost(url); 25 //封裝參數信息,使用list保存 26 List<NameValuePair> pairs = new ArrayList(); 27 NameValuePair pair1 = new BasicNameValuePair("userName", "xxx"); 28 NameValuePair pair2 = new BasicNameValuePair("password","xxx"); 29 pairs.add(pair1); 30 pairs.add(pair2); 31 post.setEntity(new UrlEncodedFormEntity(pairs)); 32 33 //發送請求,得到響應 34 HttpResponse response = client.execute(post); 35 //返回響應體 36 HttpEntity entity = response.getEntity(); 37 //將響應體以字符串形式返回 38 String content = EntityUtils.toString(entity); 39 System.out.println((content)); 40 } 41 42 @BeforeMethod 43 public void beforeTest() { 44 url = "http://xxx.com//loginValidate.do"; 45 //客戶端 46 client = HttpClients.createDefault(); 47 } 48 49 @AfterMethod 50 public void afterTest() { 51 System.out.println("用例執行完畢"); 52 } 53 54 }步驟四:執行,如果需要右擊testng.xml文件執行,需要配置testng.xml文件
步驟五:查看運行后的結果
轉載于:https://www.cnblogs.com/udiOrange/p/9629715.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java+testng接口测试入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python:列表生成器
- 下一篇: 盛大游戏回归A股进入倒计时:估值近300