day89 pytest的setup,setdown详解
学习日期:20241205
学习目标:pytest基础用法 -- pytest的setup,setdown详解
学习笔记:
setup、teardown详解
- 模块级
setup_module/teardown_module
开始于模块始末,生效一次
python
import pytest
import requests
def setup_module():
print("准备测试数据")
def teardown_module():
print("清理测试数据")
def test_mobile():
print("测试手机归属地get请求")
r = requests.get('http://api.binstd.com/shouji/query',
params={"shouji": "13456755448", "appkey": "0c818521d38759e1"})
print(r.status_code)
assert r.status_code == 200
result = r.json()
assert result['status'] == 0
assert result['msg'] == "ok"
assert result['result']["shouji"] == "13456755448"
assert result['result']["province"] == "浙江"
assert result['result']["city"] == "杭州"
assert result['result']["company"] == "中国移动"
assert result['result']["cardtype"] is None
assert result['result']["areacode"] == "0571"
def test_mobile_post():
params = {
"shouji": "13456755448",
"appkey": "0c818521d38759e1"
}
r = requests.post('https://api.binstd.com/shouji/query', params=params)
assert r.status_code == 200
result = r.json()
assert result['status'] == 0
assert result['msg'] == "ok"
assert result['result']["shouji"] == "13456755448"
assert result['result']["province"] == "浙江"
assert result['result']["city"] == "杭州"
assert result['result']["company"] == "中国移动"
assert result['result']["cardtype"] is None
assert result['result']["areacode"] == "0571"
if __name__ == '__main__':
pytest.main()
总结
- 模块级
setup_module/teardown_module
开始于模块始末,生效一次