执行自动化测试用例的时候,比如在所有的用例执行之前 需要准备一些测试数据, 所有的用例执行之后清理测试数据。这里的操作涉及到用例执行相关的操作。
pytest 提供内置的方法可以进行 运行时的配置功能。 需要使用 conftest.py 文件。
官方文档: https://docs.pytest.org/en/6.2.x/fixture.html
fixture 进行上下游传参
在项目的根目录下创建文件 conftest.py
注意:下面的代码一定放在 conftest.py 文件中, pytest在执行的时候会自动化加载此文件。 如果其他文件名 运行的时候 加载不到,就会报错。
conftest.py
# 文件名一定为conftest.py pytest在执行的时候会自动化加载此文件。import requestsimport hashlibimport pytest@pytest.fixture(scope='module')def token():"""用户登录成功之后返回token值:return:"""url = "http://49.233.108.117:28019/api/v1/user/login"bodydata = {"loginName": "17088889999","passwordMd5": hashlib.md5("123456".encode()).hexdigest()}r = requests.post(url,json=bodydata)print(r.json())return r.json()['data'] # 将token值返回
创建测试用例,在测试用例的文件中,通过使用 token 函数名可以直接获取到conftest.py 文件中定义的 token() 函数中的返回结果。
test_demo.py
import requestsdef test_search(token):print(token)url = "http://49.233.108.117:28019/api/v1/search"headers = {"token": token}querydata= {"keyword":"iphone"}r = requests.get(url,params=querydata,headers=headers)print(r.json())def test_add_cart(token):print('添加购物车',token)
执行
pytest -s -v testdemo.py
可以看到结果,能够从 conftest.py 文件中获取到对应的值。
fixture 配置运行脚手架
在pytest.fixture 中指定运行时 添加 autouse=True 参数可以自动配置运行时
主要有5个运行级别:
- session 整个执行之前
- package 包执行
- moudle py文件执行
- class 类执行
- function 函数
scope 定义了函数的运行级别。 autoUse 定义了是否自动启动,设置为True执行过程中可以自动加载。
conftest.py
# 文件名一定为conftest.py pytest在执行的时候会自动化加载此文件。import pytest@pytest.fixture(scope='session',autouse=True)def ss():print("before sessions 这里定义所有用例执行之前的操作")yieldprint('after session 这里定义所有用例执行之后的操作')@pytest.fixture(scope="package",autouse=True)def pg():print("before all tests 这里定义所有用例执行之前的操作")yieldprint('after all tests 这里定义所有用例执行之后的操作')@pytest.fixture(scope='module',autouse=True)def md():print("before module 这里定义每个py文件执行之前的操作")yieldprint('after module 这里定义每个py文件执行之后的操作')@pytest.fixture(scope='class', autouse=True)def cls():print("before class 这里定义每个py文件中class执行之前的操作")yieldprint('after class 这里定义每个py文件中class执行之后的操作')@pytest.fixture(scope='function',autouse=True)def fn():print("before function 这里定义每个py文件中testcase执行之前的操作")yieldprint('after function 这里定义每个py文件中testcase执行之后的操作')
编写对应的测试用例
testcases/test_1.py
class Test01:def test_1t1(self):assert Truedef test_1t2(self):assert True
testcases/test_2.py
class Test02:def test_201(self):assert Truedef test_202(self):assert True
testunits/test_01.py
def test_01():assert True
执行
pytest -s -v
可以看到执行效果
(venv) C:\Users\zengy\PycharmProjects\pythonProject5>pytest -s -v================================================= test session starts =================================================platform win32 -- Python 3.10.0, pytest-7.0.1, pluggy-1.0.0 -- c:\users\zengy\pycharmprojects\pythonproject5\venv\scripts\python.execachedir: .pytest_cacherootdir: C:\Users\zengy\PycharmProjects\pythonProject5collected 5 itemstestcases/test_1.py::Test01::test_1t1 before sessions 这里定义所有用例执行之前的操作before all tests 这里定义所有用例执行之前的操作before module 这里定义每个py文件执行之前的操作before class 这里定义每个py文件中class执行之前的操作before function 这里定义每个py文件中testcase执行之前的操作PASSEDafter function 这里定义每个py文件中testcase执行之后的操作testcases/test_1.py::Test01::test_1t2 before function 这里定义每个py文件中testcase执行之前的操作PASSEDafter function 这里定义每个py文件中testcase执行之后的操作after class 这里定义每个py文件中class执行之后的操作after module 这里定义每个py文件执行之后的操作testcases/test_2.py::Test02::test_201 before module 这里定义每个py文件执行之前的操作before class 这里定义每个py文件中class执行之前的操作before function 这里定义每个py文件中testcase执行之前的操作PASSEDafter function 这里定义每个py文件中testcase执行之后的操作testcases/test_2.py::Test02::test_202 before function 这里定义每个py文件中testcase执行之前的操作PASSEDafter function 这里定义每个py文件中testcase执行之后的操作after class 这里定义每个py文件中class执行之后的操作after module 这里定义每个py文件执行之后的操作testunits/test_01.py::test_01 before module 这里定义每个py文件执行之前的操作before class 这里定义每个py文件中class执行之前的操作before function 这里定义每个py文件中testcase执行之前的操作PASSEDafter function 这里定义每个py文件中testcase执行之后的操作after class 这里定义每个py文件中class执行之后的操作after module 这里定义每个py文件执行之后的操作after all tests 这里定义所有用例执行之后的操作after session 这里定义所有用例执行之后的操作================================================== 5 passed in 0.03s ==================================================
面试问题
- 接口自动化中上下游传参的方式有哪些?
- 通过使用字典的方式进行传参 https://www.yuque.com/imhelloworld/av0qdq/gl12rl#THJdE
- 通过使用类变量的方式进行传参 https://www.yuque.com/imhelloworld/av0qdq/gl12rl#mOwL3
- 通过使用pytest.fixture 装饰器进行上下游传参 https://www.yuque.com/imhelloworld/av0qdq/ft8nay#Ns7zn
- 自动化过程中 脚手架 是怎么配置的? (脚手架的含义就是用例执行时)
在conftest.py 文件中进行配置,使用 @pytest.fixture 进行对应的配置。作用域主要有
- session
- package
- module
- class
- function
配置之后 也可以通过 autoUse 参数配置是否自动加载。
