Python单元测试框架之pytest -- 断言

简介:

  对于测试来讲,不管是功能测试,自动化测试,还是单元测试。一般都会预设一个正确的预期结果,而在测试执行的过程中会得到一个实际的结果。测试的成功与否就是拿实际的结果与预期的结果进行比较。这个比的过程实际就是断言(assert)。

  在unittest单元测试框架中提供了丰富的断言方法,例如assertEqual()、assertIn()、assertTrue()、assertIs()等,而pytest单元测试框架中并没提供特殊的断言方法,而是直接使用python的assert进行断言。

  下面我们就来介绍assert 的使用。

 

 

比较大小与是否相等                      

test_assert.py

复制代码
#coding=utf-8
import pytest

# 功能
def add(a,b):
    return a + b

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

# 测试不相等
def test_add2():
    assert add(17,22) != 50

# 测试大于
def test_add3():
    assert add(17,22) <= 50

# 测试小于
def test_add4():
    assert add(17,22) >= 50


if __name__ == '__main__':
    pytest.main("test_assert.py")
复制代码

    定义一个add()函数,用于计算两个入参相加,并将相加的结果返回。

  而assert可以使用直接使用“==”、“!=”、“<”、“>”、“>=”、"<=" 等符号来比较相等、不相等、小于、大于、大于等于和小于等于。

  运行结果:

复制代码
============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile: 
plugins: html
collected 4 items

test_assert.py ...F

================================== FAILURES ===================================
__________________________________ test_add4 __________________________________

    def test_add4():
>       assert add(17,22) >= 50
E    assert 39 >= 50
E     +  where 39 = add(17, 22)

test_assert.py:22: AssertionError
===================== 1 failed, 3 passed in 0.02 seconds ======================
复制代码

  显然,17加22的结果并不大于50,所有最后一条用例失败。

 

 

测试包含或不包含                                                    

test_assert2.py

复制代码
#coding=utf-8
import pytest


# 测试相等
def test_in():
    a = "hello"
    b = "he"
    assert b in a 


# 测试不相等
def test_not_in():
    a = "hello"
    b = "hi"
    assert b not in a

if __name__ == '__main__':
    pytest.main("test_assert2.py")
复制代码

   通过定义a和b 字符串变量来比较包含的关系。

  assert 可以直接使用 in 和not in 来比较包含与不包含。

  运行结果:

复制代码
============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile: 
plugins: html
collected 2 items

test_assert2.py F.

================================== FAILURES ===================================
___________________________________ test_in ___________________________________

    def test_in():
        a = "hello"
        b = "hi"
>       assert b in a
E    assert 'hi' in 'hello'

test_assert2.py:9: AssertionError
===================== 1 failed, 1 passed in 0.01 seconds ======================
复制代码

  显然“hello”并不包含“hi”,所以第一条测试用例运行失败。

 

 

测试true或false                         

test_assert3.py

复制代码
#coding=utf-8
import pytest


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


# 判断是否为素数
def test_true():
    assert is_prime(13)


# 判断是否不为素数
def test_true():
    assert not is_prime(7)

if __name__ == '__main__':
    pytest.main("test_assert3.py")
复制代码

   通过is_prime()函数来判断n 是否为素数(只能被1和它本身整除的数)。返回值为ture或false。

  通过assert不需要任何辅助符号,直接判断对象是否为ture,而assert not 用于判断是否为false。

  运行结果:

复制代码
============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile: 
plugins: html
collected 1 items

test_assert3.py F

================================== FAILURES ===================================
__________________________________ test_true __________________________________

    def test_true():
>       assert not is_prime(7)
E    assert not True
E     +  where True = is_prime(7)

test_assert3.py:22: AssertionError
========================== 1 failed in 0.01 seconds ===========================
复制代码

 

  显示,对于第二条测试用例来讲,7是素数,所以,is_prime()函数的返回结果是Ture,而assert not 需要的正确结果是False,因此,用例执行失败。

 

目录
相关文章
|
3月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
365 1
|
4月前
|
Web App开发 人工智能 JavaScript
主流自动化测试框架的技术解析与实战指南
本内容深入解析主流测试框架Playwright、Selenium与Cypress的核心架构与适用场景,对比其在SPA测试、CI/CD、跨浏览器兼容性等方面的表现。同时探讨Playwright在AI增强测试、录制回放、企业部署等领域的实战优势,以及Selenium在老旧系统和IE兼容性中的坚守场景。结合六大典型场景,提供技术选型决策指南,并展望AI赋能下的未来测试体系。
|
2月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
161 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
2月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
207 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
3月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
308 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
3月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
415 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
3月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
576 0
|
4月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
276 18
|
3月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
188 0
|
4月前
|
安全 测试技术 API
Python 单元测试详解
单元测试是Python开发中不可或缺的环节,能确保代码按预期运行、发现Bug、提升代码质量并支持安全重构。本文从基础概念讲起,逐步介绍Python单元测试的实践方法,涵盖unittest框架、pytest框架、断言使用、Mock技巧及测试覆盖率分析,助你全面掌握单元测试技能。
259 0

推荐镜像

更多