pytest

1、pytest插件安装

新建一个requirements.txt 文件,把要安装的插件写在txt文件里

例如:

pytest

pytest.html

pytest.xdist

pytest.ordering

pytest.rerunfalires

allure-pytest

命令行运行:pip install -r requierments.txt 可安装所有的插件

2、运行方式

①命令行运行

执行参数

-vs:-v输出详细信息,-s输出调试信息,例如:pytest -vs

-n 多线程运行,例如pytest -vs -n=2

--reruns num 失败重跑,例如pytest -vs --reruns=2

-x 出现一个用例失败则停止测试,例如:pytest -vs -x

--maxfail 出现几个失败才终止,例如:

pytest -vs --maxfail=2

--html 生成html报告 例如:pytest -vs --html ./reports/result.html

-k   运行测试用例中名称中包含某个字符串的测试用例      例如:pytest -vs -k "zhangsan or lisi"

②main方式运行

if name == 'main':

pytest.main(["-vs"])

③全部配置文件pytest.ini文件执行

注意:

放在项目根目录下,名称必须是pytest.ini

可以更改默认的测试用例规则

不管是哪种运行方式,都会加载这个配置文件

pytest.ini文件里写的内容:

[pytest]

addopts = -vs

testpaths= ./testcases

python_files= zhangsan_*.py(执行zhangsan开头的文件的用例)(模块)

python_classes= www* (执行以www开头的测试用例)(类)

python_functions=test*(执行以test开头的函数)(用例)

测试用例分组执行:

smoke:冒烟用例

login:登录模块

project:工程模块

在测试用例之前加上“@pytest.mark.smoke”、“@pytest.mark.login”、

“@pytest.mark.project”

3、pytest 跳过测试用例

(测试用例包含正例和反例,当只想执行整理或只想执行反例时,可添加跳过装饰器)

①无条件跳过

@pytest.mark.skip(reason="无理由跳过")

②有条件跳过

@pytest.mark.skipif(workage<10,reason="工作经验少于10年")

测试场景:跳过反例,只执行正例时

4、pytest测试用例的前后置、固件

测试场景:UI自动化里打开浏览器,关闭浏览器、

setup()每个用例之前

teardown()每个用例之后

setup_class()每个类之前

teardown_class()每个类之后

一般写在公共类(封装)

5、使用fixtrue实现部分前置或后置

@pytest.fixture(scope=None,autouse=False,params=None,name=None)

scope:作用域

①function:在函数之前和之后执行

②class:在类之前和之后执行

③package/session:在整个项目会话之前和之后执行

例1:

在函数运行之前:

import pytest

@pytest.fixture(scope="function") #在函数执行前 scope是作用域
def exe_database_sql():
    print("这里写要执行的SQL")
class Test_fixture():
    def test01(self):
        print("第一个测试用例")

    def test02(self):
        print("第二个测试用例")

    def test03(self,exe_database_sql):
        print("第一三个测试用例,这个是要最先执行的用例,需要执行SQL")

执行结果

============================== 3 passed in 0.06s ==============================

Process finished with exit code 0
PASSED                              [ 33%]第一个测试用例
PASSED                              [ 66%]第二个测试用例
这里写要执行的SQL
PASSED                              [100%]第一三个测试用例,这个是要最先执行的用例,需要执行SQL

例2:

在函数运行之后:

import pytest

@pytest.fixture(scope="function") #在函数执行前 scope是作用域
def exe_database_sql():
    print("这里写要执行的SQL")
    yield
    print("关闭数据库")
class Test_fixture():
    def test01(self):
        print("第一个测试用例")

    def test02(self):
        print("第二个测试用例")

    def test03(self,exe_database_sql):
        print("第一三个测试用例,这个是要最先执行的用例,需要执行SQL")

运行结果:

============================= test session starts =============================
collecting ... collected 3 items

test_lianxi.py::Test_fixture::test01 PASSED                              [ 33%]第一个测试用例

test_lianxi.py::Test_fixture::test02 PASSED                              [ 66%]第二个测试用例

test_lianxi.py::Test_fixture::test03 这里写要执行的SQL
PASSED                              [100%]第一三个测试用例,这个是要最先执行的用例,需要执行SQL
关闭数据库

例3:

全部自动执行:

import pytest

@pytest.fixture(scope="function",autouse=True) #在函数执行前 scope是作用域
def exe_database_sql():
    print("这里写要执行的SQL")
    yield
    print("关闭数据库")
class Test_fixture():
    def test01(self):
        print("第一个测试用例")

    def test02(self):
        print("第二个测试用例")

    def test03(self,exe_database_sql):
        print("第一三个测试用例,这个是要最先执行的用例,需要执行SQL")

结果:

collecting ... collected 3 items

test_lianxi.py::Test_fixture::test01 这里写要执行的SQL
PASSED                              [ 33%]第一个测试用例
关闭数据库

test_lianxi.py::Test_fixture::test02 这里写要执行的SQL
PASSED                              [ 66%]第二个测试用例
关闭数据库

test_lianxi.py::Test_fixture::test03 这里写要执行的SQL
PASSED                              [100%]第一三个测试用例,这个是要最先执行的用例,需要执行SQL
关闭数据库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值