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. #python str.format 方法被用于字符串的格式化输出。

    #python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...

  2. 基于hiredis,redis C客户端封装

    项目中需要用到redis就封装了一下,基于hiredis,只封装了string和哈希的部分方法.编译时加入-D__USER_LOCK__添加线程安全. suntelRedisCli.h #ifndef ...

  3. 田螺便利店—ipconfig命令不是内部命令或外部命令怎么解决?

    查询网卡ID在运行后输入ipconfig/all点回车后提示ipconfig不是内部或外部命令,也不是可运行的程序或批处理文件? 首先确认你的输入是无误的,确保输入无误,仍提示 ipconfig 不是 ...

  4. BinarySearch(Java)

    private int binarySearch(int[] input, int target) { if (input == null) { return -1; } int index1 = 0 ...

  5. org.apache.commons.lang3.StringUtils中的StringUtils常用方法

    https://my.oschina.net/funmo/blog/615202?p=1 public static void TestStr(){ //null 和 ""操作~~ ...

  6. python 常用库及安装使用

    #win10 + python3.5.2 #pip install xxx   自动下载的缓存位置: #win7 - c:\用户\(你的用户名)\AppData\Local\pip\cache\ #l ...

  7. ZOJ2112 Dynamic Rankings

    题意 Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings ...

  8. mysql-The-server-quit-without-updating-PID-file

    mysql 编译后执行 /etc/init.d/mysqld start 启动失败 提示:"The server quit without updating PID file" 排 ...

  9. linux 控制结构

    一.if 注: 格式1.格式2:一个条件一个命令: 格式3:一个条件两个命令: 格式4:两个条件三个命令,注意条件的写法. 例1: #!/bin/sh#ifTest#to show the metho ...

  10. sofa graphql 2 rest api 试用

      大部分代码还是来自sofa 的官方文档,同时添加了docker && docker-compose集成 备注: 代码使用typescript 同时运行的时候为了方便直接运行使用ts ...