conftest.py
通过yield分隔执行前和执行后运行
session:整个用例执行前后
module:py文件执行前后
class:类前后执行
function:每个方法前后
conftest.py 的文件名称是固定的, pytest 会自动识别该文件,我们可以理解成一个专门存放 fixture 的配置文件。
一个工程下可以建多个 conftest.py 文件,一般我们都是在工程根目录下设置的 conftest 文件,这样会起到一个全局的作用。 我们也可以在不同的子目录下放 conftest.py ,这样作用范围只能在该层级的子目录下生效。
1 import pytest 2 @pytest.fixture(scope='session',autouse=True) 3 def session(): 4 print('before session...') 5 yield 6 print('after session...') 7 8 @pytest.fixture(scope='module',autouse=True) 9 def module(): 10 print('before module...') 11 yield 12 print('after module...') 13 14 @pytest.fixture(scope='class',autouse=True) 15 def classdemo(): 16 print('before class...') 17 yield 18 print('after class...') 19 20 21 @pytest.fixture(scope='function',autouse=True) 22 def func(): 23 print('before function...') 24 yield 25 print('after function...')
D:\tools\pycharm\pythonProject1>pytest -v 自动化 -s
========================================================================================================== test session starts ===========================================================================================================
platform win32 -- Python 3.8.0, pytest-7.1.3, pluggy-1.0.0 -- d:\tools\python3.8\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '7.1.3', 'py': '1.11.0', 'pluggy': '1.0.0'}, 'Plugins': {'html': '3.1.1', 'metadata': '2.0.2'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jdk
1.8.0_111'}
rootdir: D:\tools\pycharm\pythonProject1
plugins: html-3.1.1, metadata-2.0.2
collected 5 items
自动化/fixture/test_fixture.py::test_pay before session...
before module...
before class...
before function...
PASSEDafter function...
after class...
after module...
自动化/测试pytest参数/test_demo.py::TestDemo::test_one before module...
before class...
before function...
Hello test Demo one
PASSEDafter function...
自动化/测试pytest参数/test_demo.py::TestDemo::test_two before function...
FAILEDafter function...
自动化/测试pytest参数/test_demo.py::TestDemo::test_three before function...
PASSEDafter function...
自动化/测试pytest参数/test_demo.py::TestDemo::test_four before function...
FAILEDafter function...
after class...
after module...
after session...
================================================================================================================ FAILURES ================================================================================================================
___________________________________________________________________________________________________________ TestDemo.test_two ____________________________________________________________________________________________________________
self = <test_demo.TestDemo object at 0x00000000037436A0>
def test_two(self):
> assert False
E assert False
自动化\测试pytest参数\test_demo.py:9: AssertionError
___________________________________________________________________________________________________________ TestDemo.test_four ___________________________________________________________________________________________________________
self = <test_demo.TestDemo object at 0x0000000003743A60>
def test_four(self):
> assert False
E assert False
自动化\测试pytest参数\test_demo.py:15: AssertionError
======================================================================================================== short test summary info =========================================================================================================
FAILED 自动化/测试pytest参数/test_demo.py::TestDemo::test_two - assert False
FAILED 自动化/测试pytest参数/test_demo.py::TestDemo::test_four - assert False
====================================================================================================== 2 failed, 3 passed in 0.51s =======================================================================================================
D:\tools\pycharm\pythonProject1>
标签:function,...,py,fixture,conftest,pytest,test,before From: https://www.cnblogs.com/yitian395/p/16829413.html