From: http://www.testclass.net/pytest/assert/

Assert就是断言,每个测试用例都需要断言。

与unittest不同,pytest使用的是python自带的assert关键字来进行断言,大大降低了学习成本。

assert关键字后面可以接一个表达式,只要表达式的最终结果为True,那么断言通过,用例执行成功,否则用例执行失败。

详尽的用例失败描述

pytest的用例失败描述非常详尽,一目了人。考虑下面的例子

# content of test_assert1.py
def f():
return 3 def test_function():
assert f() == 4

执行上面的用例

$ pytest test_assert1.py
======= test session starts ========
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 1 item test_assert1.py F ======= FAILURES ========
_______ test_function ________ def test_function():
> assert f() == 4
E assert 3 == 4
E + where 3 = f() test_assert1.py:5: AssertionError
======= 1 failed in 0.12 seconds ========

可以很明显的看出,pytest给出的错误提示是:f()的值是3,也就是实际结果是3,而预期结果是4,3不等于4,因此断言未通过,用例失败。

断言异常抛出

pytest有自己的异常抛出断言套路,下面是最简单的形式

import pytest

def test_zero_division():
with pytest.raises(ZeroDivisionError):
1 / 0

上面代码的意思是: 1/0的时候应该抛出ZeroDivisionError,否则用例失败,断言不通过。

另外pytest还允许我们访问异常的具体信息,如下面的例子

def test_recursion_depth():
with pytest.raises(RuntimeError) as excinfo:
def f():
f()
f()
assert 'maximum recursion' in str(excinfo.value)

我们还可以定制断言异常的错误信息,比如

>>> with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"):
... pass
... Failed: Expecting ZeroDivisionError

总结

更多断言异常以及定制assert中比较方式的例子,请参阅官方文档

pytest.3.Assert的更多相关文章

  1. pytest 7 assert断言

    前言:断言是自动化最终的目的,一个用例没有断言,就失去了自动化测试的意义了. 断言用到的是 assert关键字.之前的介绍,有的测试方法中其实用到了assert断言.简单的来说,就是预期的结果去和实际 ...

  2. pytest之assert断言

    assert pytest允许您使用标准Python断言来验证Python测试中的期望和值.例如,你可以写下 # content of test_assert1.py def f(): return ...

  3. 3.pytest断言assert

    pytest使用的python自带的断言assert关键字,和unittest封装的assert断言不一样 原理:用来测试某个断言条件,如果断言条件为True,则程序将继续正常执行:但如果断言条件为假 ...

  4. pytest测试框架 -- assert断言和fixture固件

    一.断言 (1)使用assert语句进行断言 # test_run.py @pytest.mark.assert def test_assert(self): r = requests.get(&qu ...

  5. 关于pytest的一些问题

    一. 测试模块内部使用fixture和测试模块调用外部公共的fixture 1. unittest框架下的测试用例模块 from selenium import webdriver import un ...

  6. 3、pytest中文文档--编写断言

    目录 编写断言 使用assert编写断言 编写触发期望异常的断言 特殊数据结构比较时的优化 为失败断言添加自定义的说明 关于断言自省的细节 复写缓存文件 去使能断言自省 编写断言 使用assert编写 ...

  7. 8、pytest -- 捕获告警信息

    目录 1. 告警信息的默认捕获行为 2. @pytest.mark.filterwarnings 3. 去使能告警信息的展示 4. 去使能告警的捕获行为 5. DeprecationWarning和P ...

  8. pytest框架与unittest框架的对比

    一.pytest的优势 pytest是基于unittest之上的单元测试框架,它的优势如下: 自动发现测试模块和测试方法 断言使用 assert + 表达式 可以设置测试会话级(session).模块 ...

  9. Pytest -断言、跳过及运行

    基本断言方法: Pytest框架assert断言使用 • 断言:支持显示最常见的子表达式的值,包括调用,属性,比较以及二元和一元运算 符. • 包含,相等,不等,大于 小于运算,assertnot 假 ...

随机推荐

  1. The NMEA 0183 Protocol

    The NMEA 0183 Protocol NMEA0183 协议是由美国国家海洋电子协会开发.维护并发布的标准,用于航海远洋时使用的电子仪器之间的通信. 目前大部分的 GPS 接受设备都遵循这一标 ...

  2. html播放音乐

    如何在网站网页中添加音乐代码 告诉你多种格式文件的详细使用代码.    width_num——指定一个作为宽度的数字:    height_num——指定一个作为高度的数字:     1.mp3    ...

  3. dfs——n的全排列(回溯)

    #include <iostream> #include <cstring> #include <string> #include <map> #inc ...

  4. java利用Comparator接口对自定义数组排序

    import java.util.Arrays; import java.util.Comparator; public class MySort { public static void main( ...

  5. Windows下的matplotlib画图中文显示设置

    一.在测试matplotlib时遇到X轴中文字符不显示的问题,参考网上 源代码如下 from matplotlib import pyplot as plt import random import ...

  6. (23)ajax实现上传文件的功能

    form表单上传文件 urls.py from django.conf.urls import urlfrom django.contrib import adminfrom app01 import ...

  7. 实验吧—隐写术——WP之 Fair-Play

    首先,我们读题发现题目是Playfair​,其实我也不知道这是什么,那么就百度一下啊 Playfair解密算法: 首先将密钥填写在一个5*5的矩阵中(去Q留Z),矩阵中其它未用到的字母按顺序填在矩阵剩 ...

  8. 【EOJ Monthly 2018.7】【D数蝌蚪】

    https://acm.ecnu.edu.cn/contest/92/problem/D/ D. 数蝌蚪 Time limit per test: 2.0 seconds Memory limit:  ...

  9. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  10. python------软件目录结构规范

    一. 目录结构 www.cnblogs.com/alex3714/articles/5765046.html print(__file__) 获得相对路径 import osprint(os.path ...