python接口自動化(二十五)--unittest斷言——下(詳解)


簡介

   本篇還是回歸到我們最初始的話題,想必大家都忘記了,沒關系看這里:傳送門  沒錯最初的話題就是登錄,由於博客園的登錄機制改變了,本篇以我找到的開源免費的登錄API為案例,結合 unittest 框架寫 2 個用例。同樣我們先來看一下接口文檔。

接口文檔

登錄接口

請求方式:POST

請求地址:https://api.apiopen.top/developerLogin

Query參數名 類型 必需 描述 示例 e.g.
name string 用戶名 peakchao
passwd string 密碼 123456

返回示例:

                                {
    "code": 200,
    "message": "成功!",
    "result": {
        "apikey": "b9b3a96f7554e3bead2eccf16506c13e"
    }
}
                            

 

設計測試用例

1、針對以上接口文檔,設計這個登錄接口的測試用例

2、參考代碼

 1 # coding=utf-8
 2 #1.先設置編碼,utf-8可支持中英文,如上,一般放在第一行
 3 
 4 #2.注釋:包括記錄創建時間,創建人,項目名稱。
 5 '''
 6 Created on 2019-4-28
 7 @author: 北京-宏哥
 8 Project:學習和使用unittest框架設計登錄測試用例和斷言-下 9 '''
10 #3.導入requests和unittest模塊
11 import requests
12 import unittest
13 #4.編寫測試用例和斷言
14 class Blog_login(unittest.TestCase):
15     def login(self, username, psw):
16         '''兩個個參數:賬號:username,密碼:psw'''
17         url = "https://api.apiopen.top/developerLogin"
18         par = {"name": username,
19                "passwd": psw
20         }
21         res = requests.post(url, params=par)
22 #        result1 = res.text  # 字節輸出
23 #        print(result1)
24         return res.json()
25 
26     def test_login1(self):
27         '''測試登錄:正確賬號,正確密碼'''
28         username = "peakchao",  # 正確賬號,抓包獲得的加密字符串
29         psw = "123456",  # 正確密碼,抓包獲得的加密字符串
30         result = self.login(username, psw)
31         self.assertEqual(200,result['code'])
32         print(result['message'])
33 
34     def test_login2(self):
35         '''測試登錄:正確賬號,錯誤密碼;反之也可以設計用例'''
36         username = "peakchao",#正確賬號
37         psw = "122222",#錯誤密碼
38         result = self.login(username, psw)
39         self.assertEqual(result['code'], 400)
40         print(result['message'])
41     def test_login3(self):
42         '''測試登錄:賬號為空,正確密碼;反之也可以設計用例'''
43         username = "",  # 錯誤賬號
44         psw = "123456",  # 正確密碼
45         result = self.login(username, psw)
46         self.assertEqual(result['code'], 400)
47         print(result['message'])
48 if __name__ == "__main__":
49  unittest.main()

無接口文檔

1、在實際工作中由於某些原因開發人員不能按時給出接口文檔,但是接口已經開發好了,需要測試人員提前介入,此時你又不能等接口文檔寫好了再開始測試,那樣黃花菜都涼了,還有就是接口文檔有是有,但是沒有人更新和維護,如果你按

照舊文檔保證是會出錯,或者接口不通。所以我們必須采用另一種方式開始測試接口。

2、那就是我們自己通過fiddler抓包獲取url和接口的參數以及返回結果等等,還是由於博客園的登錄機制的改變,我在這了一jenkins登錄接口為例給小伙伴們演示一下。

3、大致流程:web頁面jenkins點擊登錄—>fiddler抓包獲取登錄接口URL和參數—>從fiddler查看登錄接口返回的結果—>代碼模擬登錄接口並獲取接口登錄返回結果—>設計登錄接口測試用例—>斷言並執行用例

4、web頁面jenkins點擊登錄

5、fiddler抓包獲取登錄接口URL和參數

6、從fiddler查看登錄接口返回的結果

7、代碼模擬登錄接口並獲取接口登錄返回結果

 

8、設計登錄接口測試用例

 

9、斷言並執行用例

 

10、參考代碼

 1 # coding=utf-8
 2 #1.先設置編碼,utf-8可支持中英文,如上,一般放在第一行
 3 
 4 #2.注釋:包括記錄創建時間,創建人,項目名稱。
 5 '''
 6 Created on 2019-4-28
 7 @author: 北京-宏哥
 8 Project:學習和使用unittest框架設計jenkins登錄測試用例和斷言-下篇
 9 '''
10 #3.導入requests和unittest模塊
11 import requests
12 import unittest
13 #4.編寫測試用例和斷言
14 class Jenkins_login(unittest.TestCase):
15     def login(self, username, psw):
16         '''從fiddler抓包看到事個參數:
17         賬號:j_username,密碼:j_password,from,Submit,remember_me
18         '''
19         url = "http://localhost.:8080/jenkins/j_acegi_security_check"
20         headers = {
21             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"
22         }  # get方法其它加個ser-Agent就可以了
23         par = {"j_username": username,
24              "j_password": psw,
25              "from": "",
26              "Submit": u"登錄",
27              "remember_me": "on"
28         }
29         res = requests.post(url, headers=headers, data=par)
30         result = res.url  # 字節輸出
31         print(result)
32         result1 = res.status_code
33         print(result1)
34         return res.status_code
35 
36     def test_login1(self):
37         '''測試登錄:正確賬號,正確密碼'''
38         username = "admin",  # 正確賬號,抓包獲得的加密字符串
39         psw = "111111",  # 正確密碼,抓包獲得的加密字符串
40         result = self.login(username, psw)
41         self.assertEqual(200,result)
42 
43     def test_login2(self):
44         '''測試登錄:正確賬號,錯誤密碼;反之也可以設計用例'''
45         username = "admin",#正確賬號
46         psw = "122222",#錯誤密碼
47         result = self.login(username, psw)
48         self.assertEqual(result, 404)
49 
50     def test_login3(self):
51         '''測試登錄:賬號為空,正確密碼;反之也可以設計用例'''
52         username = "",  # 錯誤賬號
53         psw = "111111",  # 正確密碼
54         result = self.login(username, psw)
55         self.assertEqual(result, 404)
56 
57 if __name__ == "__main__":
58  unittest.main()

小結

1、fiddler抓取本地部署的jenkins登錄請求時,抓取不到,但是百度可以抓取到,解決方法:

(1)使用http://localhost.fiddler 代替http://localhost

(2)使用用 http://localhost. 在locahost后面加一個點號

(3)使用 http://127.0.0.1.  在127.0.0.1后面加一個點號

(4)使用 http://machinename  使用機器名

(5)打開Fiddler,菜單>Fiddler Options>General>Enable IPv6(if avaible)去掉該選項。

(6)在hosts文件中加入127.0.0.1  localsite這樣也可以被捕獲到。

2、一些免費開源的接口,可以提供給大家進行練習

隨機推薦熱門段子(包含文字、圖片、GIF、視頻): 
https://www.apiopen.top/satinApi?type=1&page=1

 

隨機推薦熱門段子【神評版本】(包含文字、圖片、GIF、視頻): 
https://www.apiopen.top/satinGodApi?type=1&page=1

 

隨機推薦熱門段子【神評版本】評論列表: 
https://www.apiopen.top/satinCommentApi?id=27610708&page=1

 

熱門小說推薦列表 
https://www.apiopen.top/novelApi

 

小說搜索接口 
https://www.apiopen.top/novelSearchApi?name=盜墓筆記

 

小說詳情接口 
https://www.apiopen.top/novelInfoApi?name=盜墓筆記

 

天氣獲取接口 
https://www.apiopen.top/weatherApi?city=成都

 

美圖獲取接口 
https://www.apiopen.top/meituApi?page=1

 

個性網名獲取接口 
https://www.apiopen.top/femaleNameApi?page=1

 

創建應用接口 
https://www.apiopen.top/createUserKey?appId=com.chat.peakchao&passwd=123456

 

增加統計信息接口 
https://www.apiopen.top/addStatistics?appKey=00d91e8e0cca2b76f515926a36db68f5&type=點擊統計&typeId=1&count=2

 

查詢統計信息接口 
https://www.apiopen.top/findStatistics?appKey=00d91e8e0cca2b76f515926a36db68f5

 

用戶注冊接口 
https://www.apiopen.top/createUser?key=00d91e8e0cca2b76f515926a36db68f5&phone=13594347817&passwd=123654


用戶登陸接口 

https://www.apiopen.top/login?key=00d91e8e0cca2b76f515926a36db68f5&phone=13594347817&passwd=123456


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM