## 實戰要求
>[info] 鞏固前面章節中關于requests庫,pytests庫,threading庫的使用
>1. 對一個HTTP接口進行功能測試
>2. 對一個HTTP接口進行壓力測試
>練習接口1:http://httpbin.org/json
>練習接口2:https://postman-echo.com/get
## 規則
* 請獨立完成實戰要求,完成后再參考下面的示例代碼
* 如果覺得自己的代碼<span style="color:red">*更加優雅,更加高效*</span>,歡迎留言**,與大家一起**分享**哦~
:-: <span style="color:green;font-size:30;">一起來挑戰吧~</span>
<br>
## 參考代碼:
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import pytest
@pytest.fixture(params=[{"name": "Milton"}, {"name": "Cherish"}])
def test_data(request):
return request.param
class UrlData(object):
def __init__(self):
self.title_url = "http://httpbin.org/json"
self.name_url = "https://postman-echo.com/get"
class TestJson(object):
@pytest.fixture(scope="session")
def ud(self):
return UrlData()
def test_title(self, ud):
resp = requests.get(ud.title_url)
assert resp.status_code == 200, "HTTP返回碼不等于200"
assert resp.json().get("slideshow").get("title") == "Sample Slide Show", "標題與預期值不符"
def test_name(self, ud, test_data):
resp = requests.get(ud.name_url, params=test_data)
assert resp.status_code == 200, "HTTP返回碼不等于200"
assert resp.json().get("args").get("name") == test_data.get("name"), "返回名稱與傳入值不相等"
if __name__ == '__main__':
pytest.main()
```
為了對某個接口施壓,加入多線程,調整如下`test_title`測試用例如下
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import pytest
import threading
class UrlData(object):
def __init__(self):
self.title_url = "http://httpbin.org/json"
self.name_url = "https://postman-echo.com/get"
class TestJson(object):
@pytest.fixture(scope="session")
def ud(self):
return UrlData()
def title(self,ud):
resp = requests.get(ud.title_url)
assert resp.status_code == 200, "HTTP返回碼不等于200"
print(resp.json())
assert resp.json().get("slideshow").get("title") == "Sample Slide Show", "標題與預期值不符"
def test_title(self, ud):
t1 = threading.Thread(target=self.title,args=(ud,))
t2 = threading.Thread(target=self.title,args=(ud,))
t1.start()
t2.start()
if __name__ == '__main__':
pytest.main()
```
<hr style="margin-top:100px">
:-: 
***微信掃一掃,關注“python測試開發圈”,了解更多測試教程!***
- 前言
- chapter01_開發環境
- chapter02_字符串的使用
- chapter03_列表的使用
- chapter04_字典的使用
- chapter05_數字的使用
- chapter06_元組的使用
- chapter07_集合的使用
- chapter08_輸入輸出
- chapter09_控制流程
- chapter10_實例練習_登錄1
- chapter11_python函數入門
- chapter12_python中的類
- chapter13_輕松玩轉python中的模塊管理
- chapter14_掌握學習新模塊的技巧
- chapter15_通過os模塊與操作系統交互
- chapter16_子進程相關模塊(subprocess)
- chapter17_時間相關模塊(time & datetime)
- chapter18_序列化模塊(json)
- chapter19_加密模塊(hashlib)
- chapter20_文件的讀與寫
- chapter21_階段考核2_登錄
- chapter22_小小算法挑戰(排序&二分法)
- chapter23_用多線程來搞事!
- chapter24_HTTP接口請求(requests)
- chapter25_接口測試框架(pytest)
- chapter26_階段考核3_HTTP接口測試
- chapter27_HTML解析(pyquery)
- chapter28_階段考核4_爬蟲下載網易汽車
- chapter29_python中的那些編碼坑
- chapter30_MySQL數據庫操作
- chapter31 高級特性_迭代器與生成器
- chapter32 高級特性_裝飾器
- chapter33 高級特性_列表處理