@pytest.fixture(scope="", params="", autouse="", ids="", name="")
步骤一:定义fixture函数(函数名不固定);
步骤二:将函数名作为参数写入用例方法;
yield作用:带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束
import pytest@pytest.fixture(scope="function")
def my_fixture():`在这里插入代码片`print("\n这是用例前置")yieldprint("\n这是用例后置")class TestDs(object):def test_01(self):print("测试01")def test_fixture(self, my_fixture):print("测试fixture前后置")
运行结果:
ui-testcaes/testcsae/test_ds.py::TestDs::test_01 测试01
PASSED
ui-testcaes/testcsae/test_ds.py::TestDs::test_fixture
这是用例前置
测试fixture前后置
PASSED
这是用例后置------------------------------------------- generated html file: file:///Users/dongshuai/PycharmProjects/pytest/ui-testcaes/output/report/report.html -------------------------------------------
======================================================================================= 2 passed in 0.02s =======================================================================================
autouse=True:自动执行,默认False,开启为True后,将不需要主动调用也能使用
比setup 和 teardown更好用,更灵活
import pytest@pytest.fixture(scope="function", autouse=True)
def my_fixture():print("\n这是用例前置")yieldprint("\n这是用例后置")class TestDs(object):def test_01(self):print("测试01")def test_fixture(self):print("测试fixture前后置")
执行结果:
ui-testcaes/testcsae/test_ds.py::TestDs::test_01
这是用例前置
测试01
PASSED
这是用例后置ui-testcaes/testcsae/test_ds.py::TestDs::test_fixture
这是用例前置
测试fixture前后置
PASSED
这是用例后置------------------------------------------- generated html file: file:///Users/dongshuai/PycharmProjects/pytest/ui-testcaes/output/report/report.html -------------------------------------------
======================================================================================= 2 passed in 0.02s =======================================================================================
(module、package/session 原理同)
import pytest@pytest.fixture(scope="class", autouse=True)
def my_fixture():print("\n这是用例前置")yieldprint("\n这是用例后置")class TestDs(object):def test_01(self):print("测试01")def test_fixture(self):print("测试fixture前后置")
执行结果:
ui-testcaes/testcsae/test_ds.py::TestDs::test_01
这是用例前置
测试01
PASSED
ui-testcaes/testcsae/test_ds.py::TestDs::test_fixture 测试fixture前后置
PASSED
这是用例后置------------------------------------------- generated html file: file:///Users/dongshuai/PycharmProjects/pytest/ui-testcaes/output/report/report.html -------------------------------------------
======================================================================================= 2 passed in 0.03s =======================================================================================
(base) dongshuai@dongshuaideMacBook-Air pytest %
params:参数化(支持,列表,元组,字典列表[{},{},{}],字典元组({},{},{}) )
**固定写法:**原理:params将参数遍历依次传给request,然后再传入添加该fixture的用例函数
该写法是固定的
import pytest@pytest.fixture(scope="class", params=['苹果', '香蕉', '牛奶']) # 参数名,有s
def my_fixture(request): # 固定写法return request.param # 属性,没有sclass TestDs(object):def test_01(self):print("测试01")def test_fixture(self, my_fixture):print("测试fixture前后置")print('-------->' + my_fixture)
执行结果:
ui-testcaes/testcsae/test_ds.py::TestDs::test_01 测试01
PASSED
ui-testcaes/testcsae/test_ds.py::TestDs::test_fixture[\u82f9\u679c] 测试fixture前后置
-------->苹果
PASSED
ui-testcaes/testcsae/test_ds.py::TestDs::test_fixture[\u9999\u8549] 测试fixture前后置
-------->香蕉
PASSED
ui-testcaes/testcsae/test_ds.py::TestDs::test_fixture[\u725b\u5976] 测试fixture前后置
-------->牛奶
PASSED------------------------------------------- generated html file: file:///Users/dongshuai/PycharmProjects/pytest/ui-testcaes/output/report/report.html -------------------------------------------
======================================================================================= 4 passed in 0.02s =======================================================================================
(base) dongshuai@dongshuaideMacBook-Air pytest %
ids:参数化时,给每一个值设置一个变量名,意义不大
import pytest@pytest.fixture(scope="class", params=['苹果', '香蕉', '牛奶'], ids=['apple', 'banana', 'milk'])
def my_fixture(request): # 固定写法print("\n前置")yield request.paramprint('\n后置')# return request.paramclass TestDs(object):def test_01(self):print("测试01")def test_fixture(self, my_fixture):print("测试fixture前后置")print('-------->' + my_fixture)
执行结果:可以看见原来的中文unicode编码,变为了ids定义的变量名
起别名后,本身的方法名不能使用
通过conftest.py 和 @pytest.fixture() 结合使用,实现全局的前置应用(比如:项目的全局登录,模块的全局处理)
ui-testcaes/testcsae/user/test_ds.py::TestDs::test_02[\u661f\u661f]
全局--前置user--前置
测试02
PASSED
user--后置全局--后置------------------------------------------- generated html file: file:///Users/dongshuai/PycharmProjects/pytest/ui-testcaes/output/report/report.html -------------------------------------------
======================================================================================= 5 passed in 0.03s =======================================================================================
总结: