微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐