pytest(二)

一、简介

  • 1.基于python的单元测试框架,它可以和selenium,requests,appium结合实现自动化测试;
  • 2.实现用例跳过skip和reruns失败用例重跑;
  • 3.它可以结合allure-pytest插件生成allure报告;
  • 4.很方便和jenkins实现持续集成;
  • 5.有很多强大的插件:

pytest-html(生成html报告的插件)
pytest-xdist(多线程运行的插件)
pytest-orderding(改变用例的执行顺序插件)
pytest-rerunfailres(失败用例重跑的插件)
allure-pytest(生成美观自定义的allure报告)

放到一个requirements.txt的文档中,如:

pytest
pytest-html 
pytest-xdist 
pytest-ordering 
pytest-rerunfailures 
allure-pytest 

然后通过:pip install -r requirements.txt

二、常用断言

  • 与unittest不同,pytest使用的是python自带的assert关键字来进行断言
  • assert关键字后面可以接一个表达式,只要表达式的最终结果为True,那么断言通过,用例执行成功,否则用例执行失败

pytest 里面断言实际上就是 python 里面的 assert 断言方法,常用的有以下几种

assert xx :判断 xx 为真
assert not xx :判断 xx 不为真
assert a in b :判断 b 包含 a
assert a == b :判断 a 等于 b
assert a != b :判断 a 不等于 b

三、执行方式

(一)带参数

1.pytest

pytest
在这里插入图片描述

2.pytest -vs

  • -v:输出更加详细的信息。比如文件和用例名称等;
  • -s:输出调试信息。打印信息等;
pytest -vs

在这里插入图片描述

3.pytest -vs -n=2 多线程

pytest -vs -n=2

在这里插入图片描述

4.--reruns num失败重跑(前提安装插件:pytest-rerunfailres)

pytest -vs --reruns=2
  • raise Exception()抛出异常
  • try except 解决异常
    在这里插入图片描述

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

在这里插入图片描述

6.–maxfail 出现N个失败才终止测试,如:pytest -vs --maxfail=2

在这里插入图片描述

7.–html 生成html测试报告(前提安装插件:pytest-html )

如:pytest -vs --html ./reports/result.html
在这里插入图片描述

8.-k 运行测试用例中包含某个字符串的测试用例;

pytest  -vs -k  "can"

在这里插入图片描述

(二)、通过主函数运行

import pytest

if __name__ == '__main__':
    pytest.main(["-vs"])

在这里插入图片描述

(三)、指定模块运行

import pytest

if __name__ == '__main__':
    pytest.main(["-vs","testcase/test_hello.py"])

在这里插入图片描述

(四)、指定文件夹

import pytest

if __name__ == '__main__':
    pytest.main(["-vs","testcase/"])

在这里插入图片描述

四、通过全局配置文件pytest.ini文件执行

(不管是命令行还是主函数都会读取这个配置文件)

注意:

  • 一般放在项目的根目录下,名称必须是pytest.ini
  • 编码格式为ANSI(当有中文时可能需要改变编码格式为GB2312
  • pytest.ini文件可以改变默认的测试用例规则
  • 不管是命令行运行还是主函数运行,都会加载运行这个配置文件
import pytest

class TestDemo:

    @pytest.mark.smoke
    def test_beijing(self):
        print("01")

    def test_cheng(self):
        print("02")

    @pytest.mark.smoke
    def test_03(self):
        print("03")
[pytest] #标识当前配置文件是pytest配置文件
addopts = -vs -m "smoke" # 标识pytest执行时增加的命令行参数
testpaths=./demo #匹配搜索测试用例的范围目录
python_files=test_*.py # 匹配测试文件
python_classes=Test* # 匹配测试类
python_functions=test_* # 匹配测试用例

#用例分组标记
markers=
    smoke:冒烟用例
    product_manger:商品管理

-m "smoke"指的是只执行冒烟测试用例

注意:文件中最好不要出现中文,如果有中文的情况下,比如使用notpad++改成GBK的编码。

在这里插入图片描述

五、pytest跳过测试用例

  • (1):无条件跳过
import pytest

class TestDemo:

    @pytest.mark.skip(reason="无理由条件跳过")
    def test_beijing(self):
        print("01")

    def test_cheng(self):
        print("02")

    def test_03(self):
        print("03")

在这里插入图片描述

  • (2):有条件跳过
@pytest.mark.skipif(age<10,reason="年龄<10")

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值