pytest.3.Assert
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的更多相关文章
- pytest 7 assert断言
前言:断言是自动化最终的目的,一个用例没有断言,就失去了自动化测试的意义了. 断言用到的是 assert关键字.之前的介绍,有的测试方法中其实用到了assert断言.简单的来说,就是预期的结果去和实际 ...
- pytest之assert断言
assert pytest允许您使用标准Python断言来验证Python测试中的期望和值.例如,你可以写下 # content of test_assert1.py def f(): return ...
- 3.pytest断言assert
pytest使用的python自带的断言assert关键字,和unittest封装的assert断言不一样 原理:用来测试某个断言条件,如果断言条件为True,则程序将继续正常执行:但如果断言条件为假 ...
- pytest测试框架 -- assert断言和fixture固件
一.断言 (1)使用assert语句进行断言 # test_run.py @pytest.mark.assert def test_assert(self): r = requests.get(&qu ...
- 关于pytest的一些问题
一. 测试模块内部使用fixture和测试模块调用外部公共的fixture 1. unittest框架下的测试用例模块 from selenium import webdriver import un ...
- 3、pytest中文文档--编写断言
目录 编写断言 使用assert编写断言 编写触发期望异常的断言 特殊数据结构比较时的优化 为失败断言添加自定义的说明 关于断言自省的细节 复写缓存文件 去使能断言自省 编写断言 使用assert编写 ...
- 8、pytest -- 捕获告警信息
目录 1. 告警信息的默认捕获行为 2. @pytest.mark.filterwarnings 3. 去使能告警信息的展示 4. 去使能告警的捕获行为 5. DeprecationWarning和P ...
- pytest框架与unittest框架的对比
一.pytest的优势 pytest是基于unittest之上的单元测试框架,它的优势如下: 自动发现测试模块和测试方法 断言使用 assert + 表达式 可以设置测试会话级(session).模块 ...
- Pytest -断言、跳过及运行
基本断言方法: Pytest框架assert断言使用 • 断言:支持显示最常见的子表达式的值,包括调用,属性,比较以及二元和一元运算 符. • 包含,相等,不等,大于 小于运算,assertnot 假 ...
随机推荐
- Python之路,第十篇:Python入门与基础10
python3 函数 函数(function) 什么是函数: 函数是可以重复执行的代码块,可以重复使用: 作用: 定义用户级的函数:实现了一个代码块的封装: 语法: def 函数名(参数列表): ...
- [LeetCode&Python] Problem 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 求区间最值---rmp
void get_rmp () { ;i<=n;i++) r[i][]=a[i];// a[] 原数组 ;(<<j)<=n;j++) ;i+(<<j)-<=n ...
- Linux使用sshfs挂载远程目录到本地
1安装sshfs [root@iZwz9hy7gff0kpg1swp1d3Z ~]# yum install sshfs 2创建本地目录 [root@iZwz9hy7gff0kpg1swp1d3Z ~ ...
- qduoj LC的课后辅导
描述 有一天,LC给我们出了一道题,如图: 这个图形从左到右由若干个 宽为1 高不确定 的小矩形构成,求出这个图形所包含的最大矩形面积. 输入 多组测试数据每组测试数据的第一行为n(0 <= n ...
- html css input定位 文本框阴影 灰色不可编辑
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- acm 2044
////////////////////////////////////////////////////////////////////////////////#include<iostream ...
- MYSQL 常用函数大全
1. 数学函数 greatest(x1,x2,...,xn)返回集合中最大的值 least(x1,x2,...,xn) 返回集合中最小的值 rand()返回0到1内的随机值,可以通过提供一个参数(种子 ...
- 【Jmeter】插件
一.插件管理 前提:很多时候,尤其是性能测试的时候,我们需要用到很多场景,需要得到一些参数值等等. 二.插件地址 URL : https://jmeter-plugins.org/downloads/ ...
- JAVA静态代码块的作用
一 般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情 况下,其他程序来调用的时候,需要使用静态方法,这种 ...