四、Pytest Fixtures 详解
ch3/test_fixtures.py import pytest @pytest.fixture() def some_data(): """返回特定数值""" return 42 def test_some_data(some_data): """在测试中使用fixture返回数值""" assert some_data == 4
ch3/a/tasks_proj/test/conftest.py import pytest @pytest.fixture() def task_db(): """测试函数开始前的操作""" # #setup:start db yield #测试进行中,该fixture会返回给测试函数yield后面的值 """在测试中使用fixture返回数值""" #teardown:close
ch3/test_fixtures.py import pytest @pytest.fixture() def some_data(): """返回特定数值""" return 42 def test_some_data(some_data): """在测试中使用fixture返回数值""" assert some_data == 4
ch3/test_fixtures.py import pytest @pytest.fixture() def some_data(): """返回特定数值""" return 42 def other_data(): """返回特定数值""" return 5 def test_some_data(some_data, other_data): """在测试中使用fixture返回数值""" assert some_data == 42 assert other_data ==
import pytest @pytest.fixture(scope='function') def func_scope(): print("函数级别的fixtue") @pytest.fixture(scope='module') def module_scope(): print("模块级别的fixtue") @pytest.fixture(scope='session') def session_scope(): print("会话级别的fixtue") @pytest.fixture(scope='class') def class_scope(): print("类级别的fixtue") def test_func_01(func_scope,module_scope,session_scope): print("执行函数一") def test_func_02(func_scope,module_scope,session_scope): print("执行函数2") @pytest.mark.usefixtures('class_scope') class Test_class(): def test_func_03(self): print("执行类函数3") def test_func_04(self): print("执行类
test_add.py 会话级别的fixtue 模块级别的fixtue 函数级别的fixtue .执行函数一 函数级别的fixtue .执行函数2 类级别的fixtue .执行类函数3 .执行类函数4
@pytest.fixture(name="renamezhang") def some(): return 1 def test_someA(renamezhang): assert True