首页
fixture固件装饰器做用例的前后置-4
fixture固件装饰器做用例的前后置-4
fixture固件装饰器做用例的前后置
方法
fixture一个结合conftest.py文件一起使用
@pytest.fixture
(scope="作用域",
params="数据驱动",
autouse="自动执行",
ids="数据驱动名参数名",
name="给fixture作用的函数重命名")
scope参数
function
函数
class
类
module
模块
package/session
会话
实例
autouse=True,会在每条用例执行前后置操作
import pytest
@pytest.fixture(scope="function",autouse=True)
def exe_clean():
print("前置处理")
yield
print("后置处理")
def testsouhulogin():
print("登录搜狐")
class TestSouHu():
age=18
@pytest.mark.run(order=2)
def testsearch(self):
print("搜狐搜索视频")
assert False
@pytest.mark.run(order=3)
@pytest.mark.smoke
@pytest.mark.skip(reason="太难了")
def testclick(self):
print("搜狐点击")
assert False
@pytest.mark.run(order=1)
@pytest.mark.skipif(age>=18,reason='已成年')
def testswipe(self):
print("搜狐滑动")
assert False
autouse不设置,单个函数引用,需要引用固件装饰器的函数名
import pytest
@pytest.fixture(scope="function")
def exe_clean():
print("前置处理")
yield
print("后置处理")
def testsouhulogin():
print("登录搜狐")
class TestSouHu():
age=18
@pytest.mark.run(order=2)
def testsearch(self,exe_clean):
print("搜狐搜索视频")
assert False
@pytest.mark.run(order=3)
@pytest.mark.smoke
@pytest.mark.skip(reason="太难了")
def testclick(self):
print("搜狐点击")
assert False
@pytest.mark.run(order=1)
@pytest.mark.skipif(age>=18,reason='已成年')
def testswipe(self):
print("搜狐滑动")
assert False
实例
autouse=True,会在每个类开始前执行前置,在类结束后执行后置操作
import pytest
@pytest.fixture(scope="class",autouse=True)
def exe_clean():
print("前置处理")
yield
print("后置处理")
def testsouhulogin():
print("登录搜狐")
class TestSouHu():
age=18
@pytest.mark.run(order=2)
def testsearch(self):
print("搜狐搜索视频")
assert False
@pytest.mark.run(order=3)
@pytest.mark.smoke
@pytest.mark.skip(reason="太难了")
def testclick(self):
print("搜狐点击")
assert False
@pytest.mark.run(order=1)
@pytest.mark.skipif(age>=18,reason='已成年')
def testswipe(self):
print("搜狐滑动")
assert False
class TestBaiDuYun():
def testloginBaiDuYun(self):
print("登录百度云")
autouse不设置,单个类引用,需要用@pytest.mark.usefixtures('exe_clean')
import pytest
@pytest.fixture(scope="class")
def exe_clean():
print("前置处理")
yield
print("后置处理")
def testsouhulogin():
print("登录搜狐")
class TestSouHu():
age=18
@pytest.mark.run(order=2)
def testsearch(self):
print("搜狐搜索视频")
assert False
@pytest.mark.run(order=3)
@pytest.mark.smoke
@pytest.mark.skip(reason="太难了")
def testclick(self):
print("搜狐点击")
assert False
@pytest.mark.run(order=1)
@pytest.mark.skipif(age>=18,reason='已成年')
def testswipe(self):
print("搜狐滑动")
assert False
@pytest.mark.usefixtures('exe_clean')
class TestBaiDuYun():
def testloginBaiDuYun(self):
print("登录百度云")
conftest.py与测试用例放在同一级目录下
conftest.py
import pytest
@pytest.fixture(scope="class")
def exe_clean():
print("前置处理")
yield
print("后置处理")
souhu_test.py
import pytest
def testsouhulogin():
print("登录搜狐")
class TestSouHu():
age=18
@pytest.mark.run(order=2)
def testsearch(self):
print("搜狐搜索视频")
assert False
@pytest.mark.run(order=3)
@pytest.mark.smoke
@pytest.mark.skip(reason="太难了")
def testclick(self):
print("搜狐点击")
assert False
@pytest.mark.run(order=1)
@pytest.mark.skipif(age>=18,reason='已成年')
def testswipe(self):
print("搜狐滑动")
assert False
@pytest.mark.usefixtures('exe_clean')
class TestBaiDuYun():
def testloginBaiDuYun(self):
print("登录百度云")
conftest.py单独用于存放fixture固件的配置文件
使用conftest.py中的固件不需要导包,可以执行使用
可以有多个conftest.py
autouse=True,会在每个会话开始前执行前置,在会话结束后执行后置操作
import pytest
@pytest.fixture(scope="session",autouse=True)
def exe_clean():
print("前置处理")
yield
print("后置处理")
autouse=True,会在每个py文件开始前执行前置,在py结束后执行后置操作
conftest.py
conftest.py
import pytest
@pytest.fixture(scope="module",autouse=True)
def exe_clean():
print("前置处理")
yield
print("后置处理")
Created With EdrawMind
pytest
相关
pytest学习05--之fixture
pytest学习07-生成html报告
Pytest (一)
pytest系列之-mark功能的使用
Pytest26--重温面向对象编程
Pytest25--重温面向过程编程之函数模型
Pytest26--重温面向对象编程
Pytest25--重温面向过程编程之函数模型
Pytest24--重温面向过程编程之线性编码方式(线性模型)
Pytest24--重温面向过程编程之线性编码方式(线性模型)
pytest 和unnitest区别
pytest---环境安装
标签