首页 > 其他 > 详细

(十二)使用pytest管理用例:fixture和conftest使用

时间:2019-12-01 20:18:35      阅读:102      评论:0      收藏:0      [点我收藏+]

pytest下载:

pip install pytest

 

技术分享图片

查看pytest版本

pip list

 

技术分享图片

回顾pytest基本用法:

pytest

标记:@pytest.mark.名称,可对类或者方法进行标记

指定运行:pytest -m 名称

运行所有用例pytest(以test_开头)

需要import pytest

pytest兼容unittest

以pytest方式运行,需要改该工程设置默认的运行器:file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test

 

conftest案例与用法:

案例:在pytest写法中如果test_1.py文件和test_*.py文件中都需要用到登录

用法:

1.在cases目录下新建一个conftest文件,前提该目录下有init.py文件

2.conftest.py配置脚本名称是固定的,不能改名称

3.不需要import导入 conftest.py,pytest用例会自动查找

4.通常与fixture一起使用

5.用例文件名以test开头 类名以test开头 方法名以test开头

 

conftest.py文件代码+讲解样式:

import pytest

from taobaoPO.pages.indexPage import indexPageClass

@pytest.fixture()

def init_obj():

    obj = indexPageClass()

    obj.wait()

    yield obj

    obj.exit()

fixture初始化

返回值不能用return,而用pytest中的yield

 

用例文件代码+讲解样式:

import pytest

@pytest.mark.usefixtures(‘init_obj‘)

class Test_indexCaseClass():#类目一定要test开头

 

    def test_1(self,init_obj):

        init_obj.search_operator(‘111‘)

    def test_2(self,init_obj):

        init_obj.search_operator(‘‘)

 

标记使用fixtures

类名一定要以test开头

init_obj函数作为参数,返回的是obj

(十二)使用pytest管理用例:fixture和conftest使用

原文:https://www.cnblogs.com/wx921308494/p/11967061.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!