pytest学习记录

本文详细介绍了pytest自动化测试框架的使用,包括安装、基本用法、断言、标记功能、参数化测试、夹具的运用以及如何生成HTML报告。还提到了通过标记控制测试执行顺序的方法,是学习和应用pytest的全面指南。
摘要由CSDN通过智能技术生成

1.使用pytest前先进行下载和引包

在控制台terminal输入

pip install -U pytest

在新建的类内import pytest即可使用。

2.pytest会自动检测以下格式的内容,并执行

模块:test_*.py或*_test.py
类:Test开头的类,且没有 初始化__init__方法 eg TestListPage
方法:test开头 eg def test_b()
以及unittest定义的测试类

例子如下:

import pytest

class TestExample():

    def test_b():
        print("test_b")
        return  1 / 0

if __name__ == '__main__':
    pytest.main(['-s'])
#会执行所有符合pytest检测规则的文件

如果想执行指定的类

pytest.main(['-s','test_case.py'])

如果不想采用pytest的检测规则,可以在pytest.ini内进行自定义。

注意在使用时要把ini内的中文删干净,因为可能会因为UTF-8编码而报错。

[pytest]
; ini文件中的 英文分号后面的内容都是注释
addopts = -s  ; 选项 通常是- 或者 -- 开头内容

testpaths = ./scripts  ; 测试模块所在目录

python_files = test_*.py *test.py ; 测试模块文件名称规则 多个内容用空格分隔

python_classes = Test_* ; 测试类名称规则

python_functions = test_* ; 测试函数或者方法的名称规则

3.断言

普通断言

def test_b():
    print("test_4")
    assert "a" in "hello" #断言字符a是否包含在hello中


if __name__ == '__main__':
    pytest.main(["-s", "test_py4.py"])

 运行就会在控制台报错如下

def test_b():
        print("test_4")
>       assert "a" in "hello"
E       AssertionError: assert 'a' in 'hello'

 异常断言

使用with 上下文管理,对其缩进的内容会出现(特定类型的)异常, 如果要使用异常信息,可以使用as接受。

def test_recursion_depth():
    with pytest.raises(RuntimeError) as excinfo:
        # 预期会出异常的代码
    assert "maximum recursion" in str(excinfo.value)

4.标记 @pytest.mark.skip与标记失败@pytest.mark.xfail

#标记跳过
@pytest.mark.skip(reason='需要跳过')
def test_4():
    print("test_5++++")
    return 1 / 0

# 标记失败
#标记预期会出异常的测试,只有出现异常才对,不出异常反而不对
#@pytest.mark.xfail(condition=None,reason=None,raises=None)
#condition 情况,boolean 或者 字符串形式的代码 可省略
#raises 预期抛出的异常类型
#reason 原因
@pytest.mark.xfail(raises=ZeroDivisionError)
def test_5():
    return 1 / 0

5.参数化

#@pytest.mark.parametrize(self,argnames, argvalues, ids=None):
#argnames 参数名称 列表或者元组
#argvalues 参数值 列表套元组
#ids 测试id,可省略

@pytest.mark.parametrize(['a','b'],[(1,2),(3,4),(5,6),(7,8),(9,10),(11,12)] ,ids='abcdef')
def test_a(b, a):
    print("test_6++++")
    assert b + a > 100
#需要注意的是@pytest内的a,b与test_a(b, a)是名称对应的关系,不是位置对应

6.夹具

在测试之前和之后执行,用于固定测试环境,及清理回收测试资源。

在pass的位置编写代码,setup会模块/类/方法/函数被执行前执行,teardown会模块/类/方法/函数被执行后执行

#模块级
def setup_module(module):
    pass
def teardown_module(module):
    pass

#类级别
def setup_class(cls):
    pass
def teardown_class(cls):
    pass

#方法级
def setup_method(self, method):
    pass
def teardown_method(self, method):
    pass

#函数级
def setup_function(function):
    pass
def teardown_function(function):
    pass



7.html报告

安装插件

pip install pytest-html

在pytest.ini内配置存储路径 

[pytest]
addopts = -s --html=./report.html

8.指定执行顺序

安装插件

pip install pytest-ordering
import pytest

# 标记运行的顺序  正数 负数 0
# 标了运行顺序的 正整数 或者0 数越小 运行的优先级越高  123 1先运行 2再运行3 最后运行
# 标了运行顺序正整数 或者0 运行的优先级大于 没有标记

# 0和正整数 > 没有标记 >负整数
# 在各个阶段  数越小的运行的有限级越高
@pytest.mark.run(order=-2)
def test_1():
    print("1")


@pytest.mark.run(order=-1)
def test_2():
    print("2")


@pytest.mark.run(order=0)
def test_3():
    print("3")


def test_4():
    print("4")


@pytest.mark.run(order=1)
def test_5():
    print("5")


@pytest.mark.run(order=2)
def test_6():
    print("6")

# 打印结果为3 5 6 4 1 2

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值