单元测试框架--pytest初识

pytest是Python的第三方单元测试框架,提供丰富的扩展功能。本文介绍了pytest的基本使用,包括断言、Fixture、参数化、运行测试的方法,如查看帮助、指定运行测试等,并探讨了pytest-html、pytest-rerunfailures和pytest-parallel等扩展,帮助提升测试效率。
摘要由CSDN通过智能技术生成

pytest认知

pytest是Python的一个第三方单元测试框架,提供了更加丰富的扩展,更加简单、灵活,弥补了unittest在做web自动化测试的一些不足。

pytest支持pip安装

pip install -U pytest

查看pytest版本

pytest --version

对于pytest学习,可以参考:

pytest第一个简单例子

  1. 首先通过pytest编写一个简单的测试例子,test_sample.py
def inc(x):
    return x + 1

def test_answer():
    assert inc(3) == 5
  1. 切换到测试用例目录下,执行【pytest】命令

(py3_heima) D:\zhenghou\python_learning>cd test_pytest

(py3_heima) D:\zhenghou\python_learning\test_pytest>pytest
=============================================================================== test session starts ===============================================================================
platform win32 -- Python 3.6.4, pytest-5.4.3, py-1.8.2, pluggy-0.13.1
rootdir: D:\zhenghou\python_learning\test_pytest
collected 1 item                                                                                                                                                                   

test_sample.py F                                                                                                                                                             [100%]

==================================================================================== FAILURES =====================================================================================
___________________________________________________________________________________ test_answer ___________________________________________________________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_sample.py:13: AssertionError
============================================================================= short test summary info =============================================================================
FAILED test_sample.py::test_answer - assert 4 == 5
================================================================================ 1 failed in 0.46s ================================================================================

  • pytest更加简单,不需要想unittest一样必须创建测试类
  • 使用assert断言
  • pytest的测试文件和测试函数必须以【test】开头

此外,pytest也可以使用像unittest一样,通过main()方法执行测试用例

import pytest

def inc(x):
    return x + 1

def test_answer():
    assert inc(3) == 5


if __name__ == "__main__":
    pytest.main()

在一个类中执行多组测试

class TestClass:
    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")

进入test_class所在目录,执行【test_class.py】

(py3_heima) D:\zhenghou\python_learning\test_pytest>pytest -q test_class.py
.F                                                                                                                                                                           [100%]
==================================================================================== FAILURES =====================================================================================
_______________________________________________________________________________ TestClass.test_two ________________________________________________________________________________

self = <test_pytest.test_class.TestClass object at 0x000002D1ACA22780>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, "check")
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:16: AssertionError
============================================================================= short test summary info =============================================================================
FAILED test_class.py::TestClass::test_two - AssertionError: assert False
1 failed, 1 passed in 0.38s

pytest 使用方法

断言

pytest单元测试框架没有提供专门的断言方法,而是直接使用Python的asser进行断言。

# 计算a + b
def add(a, b):
    return a + b

# 判断是否为素数
def is_prime(n):
    if n <= 1:
        return False
    else:
        for i in range(2, n):
            if n % i == 0:
                return False
        return True


# 测试相等
def test_add_1():
    assert add(3, 4) == 7

# 测试不相等
def test_add_2():
    assert add(5, 8) != 12

# 测试小于等于
def test_add_3():
    assert add(5, 8) <= 10

# 测试大于等于
def test_add_4():
    assert add(4, 5) >= 3

# 测试包含于
def test_in():
    assert "h" in "hello"

# 测试不包含
def test_not_in():
    assert "he" not in "hello"

# 测试是否为True
def test_true_1():
    assert is_prime(1)

# 测试是否为True
def test_true_2():
    assert is_prime(1) is True

# 测试是否为True
def test_true_3():
    assert is_prime(1) is not True

# 测试是否为False
def test_false():
    assert is_prime(1) is False

Fixture

Fixture通常用来对测试方法、测试函数、测试类和整个测试文件进行初始化或还原测试环境

# 功能函数:计算两个数相加
def add(a, b):
    return a + b

# ==============fixture=======================
def setup_module(module):
    print("setup_module=========================>")

def teardown_module(module):
    print("teardown_module==========================>")

def setup_function(function):
    print("setup_function=====================>")

def teardown_function(function):
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值