pytest fixture
pytest支持以xUnit格式型的测试模型(setup/teardown),但还与python自带的unittest还是有一点差别,如下
- 模块形式----使用setup_module/teardown_module
- 函数形式----使用setup_function/teardown_function
- 类形式----使用setup_class/teardown_class
- 方法形式---使用setup_method/teardown_method
注意:
1.pytest也可以直接运行unittest模式的测试用例
2.如果你在pytest模式中使用setupClass()函数是不行的,不会识别,但如果用例类继承之unittest.Testcase,还是可以识别的
1、fixture scope的范围参数
之前使用@pytest.fixture(scope='module')来定义框架,scope的参数有以下几种
- function 每一个用例都执行
- class 每个类执行
- module 每个模块执行(函数形式的用例)
- session 每个session只运行一次,在自动化测试时,登录步骤可以使用该session
import os
import pytest
"""
fixtures 前置条件/后置条件
fixture的作用范围
fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function
-function:每一个函数或方法都会调用
-class:每一个类调用一次,一个类中可以有多个方法
-module:每一个.py文件调用一次,该文件内又有多个function和class
-session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module,需要使用 session 需要在当前的 文件下新建一个 conftest.py,将 fixture 写在这个文件中
"""
@pytest.fixture
def login():
print("token!!!!!!")
return "token1128"
# fixtures 自定义作用域时使用 scope 来指定即可
@pytest.fixture(scope="class")
def db_connect():
print("db_connect")
return "console"
# fixtures 也可以自动调用,使用 autouse=True ,但这时候就没有返回值
@pytest.fixture(scope="module", autouse=True)
def check_dir():
if not os.path.exists("imgs"):
os.mkdir("imgs")
# yield 表示后置条件的开始,下边的代码就是后置条件,如果有返回值的话,写在 yield 后边
yield 1111
os.rmdir("imgs")
print("用例执行后删除了 imgs 目录")
class TestUser:
# def setup_class(self):
# token = login()
# self.token = token
# print("获取token")
def test_add_user(self, login, ):
print("test_add_user", login)
def test_delete_user(self, login):
print("test_delete_user", login)
def test_modify_user(self, login, db_connect):
print("test_modify_user", login)
class TestOrder:
def test_order_user(self, login):
print("test_order_user", login)
def test_delete_order(self, login):
print("test_delete_order", login)
def test_update_order(self, login):
print("test_update_order", login)
if __name__ == '__main__':
pytest.main(["-vs", __file__])
运行结果:
/Users/zhulixiang/Downloads/pythonProject/thz/venv/bin/python "/Users/zhulixiang/Downloads/pythonProject/thz/Day11/pytest_fixtures 使用.py"
============================= test session starts ==============================
platform darwin -- Python 3.7.5, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/zhulixiang/Downloads/pythonProject/thz/venv/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.7.5', 'Platform': 'Darwin-21.1.0-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '1.0.0'}, 'Plugins': {'PyTestReport': '0.2.1', 'metadata': '1.11.0', 'json-report': '1.4.1', 'ordering': '0.6', 'Faker': '9.5.1'}, 'JAVA_HOME': '/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home'}
rootdir: /Users/zhulixiang/Downloads/pythonProject/thz/Day11
plugins: PyTestReport-0.2.1, metadata-1.11.0, json-report-1.4.1, ordering-0.6, Faker-9.5.1
collecting ... collected 6 items
pytest_fixtures 使用.py::TestUser::test_add_user token!!!!!!
test_add_user token1128
PASSED
pytest_fixtures 使用.py::TestUser::test_delete_user token!!!!!!
test_delete_user token1128
PASSED
pytest_fixtures 使用.py::TestUser::test_modify_user db_connect
token!!!!!!
test_modify_user token1128
PASSED
pytest_fixtures 使用.py::TestOrder::test_order_user token!!!!!!
test_order_user token1128
PASSED
pytest_fixtures 使用.py::TestOrder::test_delete_order token!!!!!!
test_delete_order token1128
PASSED
pytest_fixtures 使用.py::TestOrder::test_update_order token!!!!!!
test_update_order token1128
PASSED用例执行后删除了 imgs 目录
============================== 6 passed in 0.07s ===============================
Process finished with exit code 0