pytest学习笔记(一)

1.安装pytest

pip install pytest

2.导入pytest包

import pytest

3.定义一个函数,倒序输出结果。并建立一个测试用例设置断言检查执行结果
pytest识别test_xxx.py或者xxx_test.py格式的函数作为测试用例来执行

import pytest
def reverse(str):
    return str[::-1]
def test_reverse():
    str='python'
    assert reverse(str) == 'nohtyp'
    print(reverse(str))

4.定义的断言值从pytest返回的错误信息取

在这里插入图片描述

import pytest
def division(a,b):
    return a/b

def test_div():

    with pytest.raises(ZeroDivisionError):
        division(5, 0)

测试脚本执行方式`

1.pytest命令执行

参数:
-vs 	-v:输出详细信息  -s:输出调试信息
-n		多线程运行(安装pytest -xdist)
--reruns num 失败重跑(安装pytest -rerunfailres)
-x 	出现一个失败则停止
-k 	运行测试用例名称包含某个字符串的用例
--html 生成html报告


2.通过主函数main方式执行
if __name__=='__main__':
    pytest.main(["-vs"])


3.通过全局配置文件pytest.ini文件执行
ps:
一般放在项目的根目录下
编码格式为ANSI
pytest.ini可以改变默认的测试用例规则
不管是命令运行还是主函数运行,都会加载这个配置文件

在这里插入图片描述
跳过用例:skip
1.有条件跳过

import pytest
class TestPC:
    def test_login(self):
        a=1
        b=2
        assert a + b == 3
        print('今天星期一')
        return a+b
    @pytest.mark.skipif(1==1,reason="无理由跳过q")
    def test_add(self):
        print(1+2,'天气很不错')

在这里插入图片描述
2.无条件跳过

import pytest
class TestPC:
    def test_login(self):
        a=1
        b=2
        assert a + b == 3
        print('今天星期一')
        return a+b
    @pytest.mark.skip(reason="无理由跳过")
    def test_add(self):
        print(1+2,'天气很不错')

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值