day91 pytest的setup,setdown详解(三)
学习日期:20241207
学习目标:pytest基础用法 -- pytest的setup,setdown详解(三)
学习笔记:
setup、teardown详解(三)
- 类级
setup_class/teardown_class
只在类中前后运行一次(在类中)
import requests
import pytestclass TestMobile:@classmethoddef setup_class(cls):print("准备测试数据")@classmethoddef teardown_class(cls):print("清理测试数据")def test_mobile(self):print("测试手机归属地get请求")r = requests.get('https://api.binstd.com/shouji/query',params={"shouji": "13456755448", "appkey": "0c818521d38759e1"})assert r.status_code == 200result = r.json()assert result['status'] == 0assert 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 Noneassert result['result']["areacode"] == "0571"def test_mobile_post(self):print("测试手机归属地post请求")params = {"shouji": "13456755448","appkey": "0c818521d38759e1"}r = requests.post('https://api.binstd.com/shouji/query', params=params)assert r.status_code == 200result = r.json()assert result['status'] == 0assert 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 Noneassert result['result']["areacode"] == "0571"if __name__ == '__main__':pytest.main()
- 方法级
setup_method/teardown_method
开始于方法始末(在类中)
import requests
import pytestclass TestMobile:@classmethoddef setup_method(cls):print("准备测试数据")@classmethoddef teardown_method(cls):print("清理测试数据")def test_mobile(self):print("测试手机归属地get请求")r = requests.get('https://api.binstd.com/shouji/query',params={"shouji": "13456755448", "appkey": "0c818521d38759e1"})assert r.status_code == 200result = r.json()assert result['status'] == 0assert 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 Noneassert result['result']["areacode"] == "0571"def test_mobile_post(self):print("测试手机归属地post请求")params = {"shouji": "13456755448","appkey": "0c818521d38759e1"}r = requests.post('https://api.binstd.com/shouji/query', params=params)assert r.status_code == 200result = r.json()assert result['status'] == 0assert 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 Noneassert result['result']["areacode"] == "0571"if __name__ == '__main__':pytest.main()
总结
- 类级
setup_class/teardown_class
只在类中前后运行一次(在类中) - 方法级
setup_method/teardown_method
开始于方法始末(在类中)