day95 pytest的fixture详解(二)
学习日期:20241210
学习目标:pytest基础用法 -- pytest的fixture详解(二)
学习笔记:
fixture(autouse=True)
- func的autouse是TRUE时,所有函数方法都会调用func
import pytest
import requests@pytest.fixture(autouse=True)
def func():print("我是前置步骤")def test_getmobile(func):print("测试get请求")params = {'key1': 'value1', 'key2': 'value2'}r=requests.get('https://httpbin.org/get',params=params)print(r.status_code)assert r.status_code == 200res = r.json()assert res['url'] == 'https://httpbin.org/get?key1=value1&key2=value2'assert res['origin'] == '163.125.202.248'assert res['args']['key1'] == 'value1'assert res['args']['key2'] == 'value2'def test_postmobile():print("测试post请求")params = {'key': 'value'}r = requests.post('https://httpbin.org/post', data=params)print(r.status_code)assert r.status_code == 200print(r.json())res=r.json()assert res['args'] == {}assert res['data'] == ''assert res['form']['key'] == 'value'if __name__ == '__main__':pytest.main()
总结
- @pytest.fixture(autouse=True),func的autouse是TRUE时,所有函数方法都会调用func