(1)测试组长针对某一个项目开发的代码框架,这个框架封装了很多的基础模块,报告模块等等
(2)作用降低人工干预、提高测试效率、增加代码重用性
pytest框架实现一些前后置处理(固件,夹具),常用三种,用例执行之前,用例执行之后的操作
setup/teardown,setup_class/teardown_class
为啥需要这些功能,因为需要前置条件呀!
def setup(self):在每个用例之前执行一次
def teardown(self):在每个用例之后执行一次
def setup_class(self):在所有用例之前执行一次
def teardown_class:在所有用例之后执行一次
案例一:
# -*- coding: utf-8 -*-
# 可可爱爱小七月
import timeimport pytestdef test_func():time.sleep(2)print('hash')class TestLogin:age = 19def setup(self):print("初始化至新安装状态")@pytest.mark.run(order=1)def test_01_baili5(selfself):print('baili5')@pytest.mark.skip(reason="用例要调整")@pytest.mark.smokedef test_01_baili6(selfself):print('冒烟baili6')@pytest.mark.skipif(age <= 18, reason="未成年")@pytest.mark.userdef test_01_baili9(selfself):print('用户分组baili9')def teardown(selfself):print("执行完关闭应用")
执行结果:
testcase/test_ zhuce.py::TestLogin::test_01_baili5 初始化至新安装状态
baili5
PASSED执行完关闭应用testcase/test_login.py::TestLogin::test_01_baili2 baili2
PASSED
testcase/test_ zhuce.py::TestLogin::test_01_baili6 SKIPPED (用例要调整)
testcase/test_ zhuce.py::TestLogin::test_01_baili9 初始化至新安装状态
用户分组baili9
PASSED执行完关闭应用testcase/test_login.py::TestLogin::test_01_baili3 冒烟baili3
PASSED
testcase/test_login.py::TestLogin::test_01_baili4 baili4
PASSED
不想所有用例执行前置和后置,上面方法做不到
fixture装饰器可以执行所有、还有部分的前后置
方法前声明该函数为fixture函数: @pytest.fixture(scope=“”, params=“”, ids=“”, autouse=“”)
scope表示被fixture标记的函数的作用域function、class、module(每个py文件)、package/session
params:参数化
autouse:true自动执行,默认false
ids:参数化时,每一个值给予一个变量名
name:给被 @pytest.fixture标记的方法加一个别名
(1)scope作用范围
scope=“function”
# -*- coding: utf-8 -*-
# 可可爱爱小七月
import timeimport pytestclass Testproduc:@pytest.fixture(scope="function")def myfixture(self):print("这是前置方法,可以实现部分以及全部用例的前后置")yield print("这是后置方法,可以实现部分以及全部用例的前后置")@pytest.mark.smokedef test_01_xinyo(selfself,myfixture):print('冒烟妖媚')def test_01_haha(self):print("haha")def test_01_xixi(self,myfixture):print("xixi")
运行结果:
scope=“class”
# -*- coding: utf-8 -*-
# 可可爱爱小七月
import timeimport pytestclass Testproduc:@pytest.fixture(scope="class", autouse=True)def myfixture(self):print("这是前置方法,可以实现部分以及全部用例的前后置")yieldprint("这是后置方法,可以实现部分以及全部用例的前后置")@pytest.mark.smokedef test_01_xinyo(selfself ):print('冒烟妖媚')def test_01_haha(self):print("haha")def test_01_xixi(self ):print("xixi")
执行结果:
scope=“moudle”
如果一个模块里面有两个类,scope="class"的时候会执行两次,如果是moudle只会执行一次
(2)autouse自动使用,不用在方法的参数中调用就可以用,默认false
# -*- coding: utf-8 -*-
# 可可爱爱小七月
import timeimport pytestclass Testproduc:@pytest.fixture(scope="function", autouse=True)def myfixture(self):print("这是前置方法,可以实现部分以及全部用例的前后置")yieldprint("这是后置方法,可以实现部分以及全部用例的前后置")@pytest.mark.smokedef test_01_xinyo(selfself ):print('冒烟妖媚')def test_01_haha(self):print("haha")def test_01_xixi(self ):print("xixi")
执行结果:
(3)params参数化
支持列表【】 ,元组(),字典列表:列表中有字典【{},{}】,字典元组:元组中有字典({},{})
学习进度p6 19:09
怎么进行传参?
首先要在声明的时候加上params参数,可以是列表、元组、列表字典、元组字典
然后在该装饰器函数中返回参数,注意是固定写法
必须是request
@pytest.fixture(scope="class", params=['成龙', '李小龙'])
def myfixture(request):return request.param//这里的return值=引用此装饰器的函数的参数,params为参数名 param为属性名 固定写法
(4)ids
@pytest.fixture(scope="class", params=['成龙', '李小龙'], ids=['cl', 'lxl'])
def myfixture(request):return request.param
(5)name
起了别名原来的名称就用不了,相当于改名字
在企业当中是如何使用的呢?
1.单独存放: conftest.py是单独存放的夹具配置文件,名称不能更改
assert
os.system('allure generate ./temp -o ./report --clean') : 命令行解释:命令固定的 临时json格式报告位置 输出output 输出allure报告的位置 清空report之前内容
本文发布于:2024-01-27 21:09:21,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063609622654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |