1, Fixture Explanation
1. Fixture concept: Fixture is a code processing mechanism used by pytest to prepare and clean up before and after testing<2. Fixture has the following advantages over setup and teardown:
(1) Fixture naming is more flexible and has fewer limitations.
(2) Conftest.py configuration allows for data sharing and can automatically find some configurations without the need for import.
3. Fixture fixtures, @Pytest. fixture
(scope= 'function') Every function or method is called
(scope= 'class') Every class is called
(scope=' module ') Every. py file is called
(scope=' session ') Multiple files are called once, and. py files are modules.
The scope of fixture: session> Module> Class> Function.
(1) Each function or method will call.
'''
@Author : testing engineer Selina
@FileName : test_fixture.py
@Description:
'''
import pytest
@pytest.fixture()
def func():
print('i am the prerequisite step')
def test_rule1(func):
assert 1 == 1
def test_rule2(func):
assert 2 == 2
print('pytest -q001')
if __name__== '__main__':
pytest.main(['s', 'test_fixture.py'])
====================== test session starts ==========================
collecting ... collected 2 items
test_fixture.py::test_rule1
i am the prerequisite step
PASSED [ 50%]
test_fixture.py::test_rule2
i am the prerequisite step
PASSED [100%]pytest -q001
====================== 2 passed in 0.01s ==========================
'''
@Author : testing engineer Selina
@FileName : test_fixture.py
@Description:
'''
import pytest
## @pytest.fixture(scope='function', autouse=True)
@pytest.fixture(autouse=True)
def func():
print('i am the prerequisite step')
def test_rule1():
assert 1 == 1
def test_rule2():
assert 2 == 2
print('pytest -q001')
if __name__== '__main__':
pytest.main(['s', 'test_fixture.py'])
====================== test session starts ==========================
collecting ... collected 2 items
test_fixture.py::test_rule1
i am the prerequisite step
PASSED [ 50%]
test_fixture.py::test_rule2
i am the prerequisite step
PASSED [100%]pytest -q001
====================== 2 passed in 0.01s ==========================
(2) (scope= 'class') Each class is called once.
'''
@Author : testing engineer Selina
@FileName : test_fixture.py
@Description:
'''
import pytest
## @pytest.fixture(scope='class', autouse=True)
@pytest.fixture(scope='class')
def func():
print('i am the prerequisite step')
class Testclassfixture:
def test_rule1(self, func):
assert 1 == 1
def test_rule2(self, func):
assert 2 == 2
print('pytest -q001')
if __name__== '__main__':
pytest.main(['s', 'test_fixture.py'])
====================== test session starts =========================
collecting ... collected 2 items
test_fixture.py::Testclassfixture::test_rule1
i am the prerequisite step
PASSED [ 50%]
test_fixture.py::Testclassfixture::test_rule2
PASSED [100%]pytest -q001
======================== 2 passed in 0.01s ===========================
(3) (scope= 'module') Each. py file is called once (regardless of how many methods are in the file, only run once).
'''
@Author : testing engineer Selina
@FileName : test_fixture.py
@Description:
'''
import pytest
@pytest.fixture(scope='module', autouse=True)
def func():
print('i am the prerequisite step')
class Testclassfixture:
def test_rule1(self):
assert 1 == 1
def test_rule2(self):
assert 2 == 2
print('pytest -q001')
class Testfixture:
def test_rule3(self):
assert 1 == 1
def test_rule4(self):
assert 2 == 2
print('pytest -q001')
if __name__== '__main__':
pytest.main(['s', 'test_fixture.py'])
====================== test session starts ===========================
collecting ... collected 4 items
test_fixture.py::Testclassfixture::test_rule1
i am the prerequisite step
PASSED [ 25%]
test_fixture.py::Testclassfixture::test_rule2
PASSED [ 50%]pytest -q001
test_fixture.py::Testfixture::test_rule3
PASSED [ 75%]
test_fixture.py::Testfixture::test_rule4
PASSED [100%]pytest -q001
========================= 4 passed in 0.01s ================================
(4) (scope= 'session') is called from multiple files once, and the. py file is the module
. Generally, all the pre steps are placed in the conftest.py file.
2, Pytest uses Conftest to manage fixtures
In order to facilitate the use of the testing framework, all fixture pre - and post content will be placed in the convtest. py file

'''
@Author : testing engineer Selina
@FileName : test_fixture.py
@Description:
'''
import pytest
def test_rule1(func):
assert 1 == 1
def test_rule2(func):
assert 2 == 2
print('pytest -q001')
if __name__== '__main__':
pytest.main(['s', 'test_fixture.py'])
================= test session starts =========================
collecting ... collected 2 items
test_fixture.py::test_rule1
i am a prerequisite step, i need to run it first
PASSED [ 50%]
test_fixture.py::test_rule2
i am a prerequisite step, i need to run it first
PASSED [100%]
pytest -q001
======================== 2 passed in 0.01s ==========================
3, Pytest returns data using fixtures
(1) Return value.
@pytest.fixture(scope='function')
def func():
return params
'''
@Author : testing engineer Selina
@FileName : test_fixture.py
@Description:
'''
import pytest
import requests
@pytest.fixture()
def get_DoctorDept():
params = {'workerId': '1037', 'hospitalCode': '1'}
return params
def test_DoctorDept(get_DoctorDept):
Id = get_DoctorDept['workerId']
hospitalCode = get_DoctorDept['hospitalCode']
print('testing doctor visiting department')
r=requests.get('http://ip address : port number /outPatient/reception/getDoctorDept',
params={'workerId':Id,'hospitalCode':hospitalCode})
print(r.status_code)
(2) The parameters can be placed in the conftest.py file:
'''
@Author : testing engineer Selina
@FileName : conftest.py
@Description:
'''
import pytest
@pytest.fixture()
def get_DoctorDept():
params = {'workerId': '1037', 'hospitalCode': '1'}
return params
@pytest.fixture(scope='session')
def test_session():
print('i am session level fixture')
4, Pytest uses yield for post-processing
'''
@Author : testing engineer Selina
@FileName : conftest.py
@Description: yield it can be passed as a reference, but it is generally not recommended to use it this way
'''
import pytest
@pytest.fixture(scope='function')
def func():
print('i am a prerequisite step, i need to run it first')
yield 'Selina'
print('i am the post step, and i will run it at the end')
'''
@Author : testing engineer Selina
@FileName : test_fixture.py
@Description:
'''
import pytest
def test_rule1(func):
assert 1 == 1
def test_rule2(func):
assert 2 == 2
print('pytest -q001')
============================= test session starts ==============================
collecting ... collected 2 items
test_fixture.py::test_rule1
i am a prerequisite step, i need to run it first
PASSED [ 50%]
i am the post step, and i will run it at the end
test_fixture.py::test_rule2
i am a prerequisite step, i need to run it first
PASSED [100%]pytest -q001
i am the post step, and i will run it at the end
============================== 2 passed in 0.01s ===============================
5, The execution order of fixtures in pytest
Scope of Fixture: session> Module> Class> Function.
'''
@Author : testing engineer Selina
@FileName : test_fixture.py
@Description: pytest of fixture execution sequence
'''
import pytest
@pytest.fixture(scope='session')
def t_session():
print('i am session level fixture')
@pytest.fixture(scope='module')
def t_module():
print('i am module level fixture')
@pytest.fixture(scope='class')
def t_class():
print('i am class level fixture')
@pytest.fixture(scope='function')
def t_function():
print('i am function level fixture')
class TestOrder:
def test_order(self, t_session, t_module, t_function, t_class):
assert 1 == 1
======================= test session starts ==========================
collecting ... collected 1 item
test_fixture_order.py::TestOrder::test_order
i am session level fixture
i am module level fixture
i am class level fixture
i am function level fixture
PASSED [100%]
========================= 1 passed in 0.01s =========================
6, The usefixtures method of pytest
@pytest.fixture(scope='function')
def func():
xxx
the first option is to accept the return value
def test_func(func):
xxx
the second option is that the return value cannot be received
@pytest.mark.usefixtures('func')
def test_func():
xxx
7, Params and ids in the fixture of pytest
(Params= ['Parameter 1 ',' Parameter 2 '], ids= ['Use Case 1'], ['Use Case 2 ']).
'''
@Author : testing engineer Selina
@FileName : test_fixture_params.py
@Description:
'''
import pytest
'''ids it's the name of the test case, params it is the parameter passed by the test case'''
@pytest.fixture(params=['data 1', 'data 2'], ids=['case1', 'case2'])
def params_fixture(request):
return request.param
def test_params(params_fixture):
print(params_fixture)
if __name__== '__main__':
pytest.main(['s', 'test_fixture_params.py'])
====================== test session starts ===========================
collecting ... collected 2 items
test_fixture_params.py::test_params[case1]
PASSED [ 50%] data 1
test_fixture_params.py::test_params[case2]
PASSED [100%] data 2
===================== 2 passed in 0.01s ==============================
Process finished with exit code 0