执行自动化测试用例的时候,比如在所有的用例执行之前 需要准备一些测试数据, 所有的用例执行之后清理测试数据。这里的操作涉及到用例执行相关的操作。

pytest 提供内置的方法可以进行 运行时的配置功能。 需要使用 conftest.py 文件。

官方文档: https://docs.pytest.org/en/6.2.x/fixture.html

fixture 进行上下游传参

在项目的根目录下创建文件 conftest.py

注意:下面的代码一定放在 conftest.py 文件中, pytest在执行的时候会自动化加载此文件。 如果其他文件名 运行的时候 加载不到,就会报错。
conftest.py

  1. # 文件名一定为conftest.py pytest在执行的时候会自动化加载此文件。
  2. import requests
  3. import hashlib
  4. import pytest
  5. @pytest.fixture(scope='module')
  6. def token():
  7. """
  8. 用户登录成功之后返回token值
  9. :return:
  10. """
  11. url = "http://49.233.108.117:28019/api/v1/user/login"
  12. bodydata = {
  13. "loginName": "17088889999",
  14. "passwordMd5": hashlib.md5("123456".encode()).hexdigest()
  15. }
  16. r = requests.post(url,json=bodydata)
  17. print(r.json())
  18. return r.json()['data'] # 将token值返回

创建测试用例,在测试用例的文件中,通过使用 token 函数名可以直接获取到conftest.py 文件中定义的 token() 函数中的返回结果。
test_demo.py

  1. import requests
  2. def test_search(token):
  3. print(token)
  4. url = "http://49.233.108.117:28019/api/v1/search"
  5. headers = {
  6. "token": token
  7. }
  8. querydata= {
  9. "keyword":"iphone"
  10. }
  11. r = requests.get(url,params=querydata,headers=headers)
  12. print(r.json())
  13. def test_add_cart(token):
  14. print('添加购物车',token)

执行

  1. pytest -s -v testdemo.py

可以看到结果,能够从 conftest.py 文件中获取到对应的值。

fixture 配置运行脚手架

在pytest.fixture 中指定运行时 添加 autouse=True 参数可以自动配置运行时
主要有5个运行级别:

  1. session 整个执行之前
  2. package 包执行
  3. moudle py文件执行
  4. class 类执行
  5. function 函数

scope 定义了函数的运行级别。 autoUse 定义了是否自动启动,设置为True执行过程中可以自动加载。
conftest.py

  1. # 文件名一定为conftest.py pytest在执行的时候会自动化加载此文件。
  2. import pytest
  3. @pytest.fixture(scope='session',autouse=True)
  4. def ss():
  5. print("before sessions 这里定义所有用例执行之前的操作")
  6. yield
  7. print('after session 这里定义所有用例执行之后的操作')
  8. @pytest.fixture(scope="package",autouse=True)
  9. def pg():
  10. print("before all tests 这里定义所有用例执行之前的操作")
  11. yield
  12. print('after all tests 这里定义所有用例执行之后的操作')
  13. @pytest.fixture(scope='module',autouse=True)
  14. def md():
  15. print("before module 这里定义每个py文件执行之前的操作")
  16. yield
  17. print('after module 这里定义每个py文件执行之后的操作')
  18. @pytest.fixture(scope='class', autouse=True)
  19. def cls():
  20. print("before class 这里定义每个py文件中class执行之前的操作")
  21. yield
  22. print('after class 这里定义每个py文件中class执行之后的操作')
  23. @pytest.fixture(scope='function',autouse=True)
  24. def fn():
  25. print("before function 这里定义每个py文件中testcase执行之前的操作")
  26. yield
  27. print('after function 这里定义每个py文件中testcase执行之后的操作')

编写对应的测试用例
testcases/test_1.py

  1. class Test01:
  2. def test_1t1(self):
  3. assert True
  4. def test_1t2(self):
  5. assert True

testcases/test_2.py

  1. class Test02:
  2. def test_201(self):
  3. assert True
  4. def test_202(self):
  5. assert True

testunits/test_01.py

  1. def test_01():
  2. assert True

执行

  1. pytest -s -v

可以看到执行效果

  1. (venv) C:\Users\zengy\PycharmProjects\pythonProject5>pytest -s -v
  2. ================================================= test session starts =================================================
  3. platform win32 -- Python 3.10.0, pytest-7.0.1, pluggy-1.0.0 -- c:\users\zengy\pycharmprojects\pythonproject5\venv\scripts\python.exe
  4. cachedir: .pytest_cache
  5. rootdir: C:\Users\zengy\PycharmProjects\pythonProject5
  6. collected 5 items
  7. testcases/test_1.py::Test01::test_1t1 before sessions 这里定义所有用例执行之前的操作
  8. before all tests 这里定义所有用例执行之前的操作
  9. before module 这里定义每个py文件执行之前的操作
  10. before class 这里定义每个py文件中class执行之前的操作
  11. before function 这里定义每个py文件中testcase执行之前的操作
  12. PASSEDafter function 这里定义每个py文件中testcase执行之后的操作
  13. testcases/test_1.py::Test01::test_1t2 before function 这里定义每个py文件中testcase执行之前的操作
  14. PASSEDafter function 这里定义每个py文件中testcase执行之后的操作
  15. after class 这里定义每个py文件中class执行之后的操作
  16. after module 这里定义每个py文件执行之后的操作
  17. testcases/test_2.py::Test02::test_201 before module 这里定义每个py文件执行之前的操作
  18. before class 这里定义每个py文件中class执行之前的操作
  19. before function 这里定义每个py文件中testcase执行之前的操作
  20. PASSEDafter function 这里定义每个py文件中testcase执行之后的操作
  21. testcases/test_2.py::Test02::test_202 before function 这里定义每个py文件中testcase执行之前的操作
  22. PASSEDafter function 这里定义每个py文件中testcase执行之后的操作
  23. after class 这里定义每个py文件中class执行之后的操作
  24. after module 这里定义每个py文件执行之后的操作
  25. testunits/test_01.py::test_01 before module 这里定义每个py文件执行之前的操作
  26. before class 这里定义每个py文件中class执行之前的操作
  27. before function 这里定义每个py文件中testcase执行之前的操作
  28. PASSEDafter function 这里定义每个py文件中testcase执行之后的操作
  29. after class 这里定义每个py文件中class执行之后的操作
  30. after module 这里定义每个py文件执行之后的操作
  31. after all tests 这里定义所有用例执行之后的操作
  32. after session 这里定义所有用例执行之后的操作
  33. ================================================== 5 passed in 0.03s ==================================================

面试问题

  1. 接口自动化中上下游传参的方式有哪些?
    1. 通过使用字典的方式进行传参 https://www.yuque.com/imhelloworld/av0qdq/gl12rl#THJdE
    2. 通过使用类变量的方式进行传参 https://www.yuque.com/imhelloworld/av0qdq/gl12rl#mOwL3
    3. 通过使用pytest.fixture 装饰器进行上下游传参 https://www.yuque.com/imhelloworld/av0qdq/ft8nay#Ns7zn
  2. 自动化过程中 脚手架 是怎么配置的? (脚手架的含义就是用例执行时)

在conftest.py 文件中进行配置,使用 @pytest.fixture 进行对应的配置。作用域主要有

  1. session
  2. package
  3. module
  4. class
  5. function

配置之后 也可以通过 autoUse 参数配置是否自动加载。