Pytest自动化测试用例的断言
目录
前言
测试的主要工作目标就是验证实际结果与预期结果是否一致;在接口自动化测试中,通过断言来实现这一目标。Pytest中断言是通过assert语句实现的(pytest对Python原生的assert语句进行了优化),确定实际情况是否与预期一致。
pytest断言assert的用法
在自动化测试用例中,最常用的断言是相等断言,就是断言预期结果和实际结果是一致的。pytest通过 “assert 实际结果 == 预期结果” 实现。通常我们断言的预期结果和实际结果的数据类型包括字符串、元组、字典、列表和对象。
1、断言字符串
-
# content of test_assertions.py
-
class TestAssertions(object):
-
def test_string(self):
-
assert "spam" == "eggs"
执行测试用例结果(在pycharm中以pytest执行用例,后面示例都如此):
-
test_assertions.py:2 (TestAssertions.test_string)
-
spam != eggs
-
-
Expected :eggs
-
Actual :spam
-
-
-
self =
-
-
def test_string(self):
-
> assert "spam" == "eggs"
-
E AssertionError: assert 'spam' == 'eggs'
-
-
test_assertions.py:4: AssertionError
说明:Expected 为期望结果(即 == 右侧的预期结果),Actual 为实际结果(即 == 左侧的实际结果),> 后面为出错的代码行,E 后面为错误信息。
2、断言函数返回值
-
class TestAssertions(object):
-
def test_function(self):
-
def f():
-
return [1, 2, 3]
-
-
assert f() == [1, 2, 4]
执行测试用例结果:
-
test_assertions.py:1 (TestAssertions.test_function)
-
[1, 2, 3] != [1, 2, 4]
-
-
Expected :[1, 2, 4]
-
Actual :[1, 2, 3]
-
-
-
self =
-
-
def test_function(self):
-
def f():
-
return [1, 2, 3]
-
-
> assert f() == [1, 2, 4]
-
E assert [1, 2, 3] == [1, 2, 4]
-
-
test_assertions.py:6: AssertionError
3、断言集合类型
断言字典、列表、元组和集合等类型在测试中也是很常见的。比如下面这段测试用例代码:
-
class TestCollections(object):
-
def test_dict(self):
-
assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0}
-
-
def test_dict2(self):
-
assert {"a": 0, "b": {"c": 0}} == {"a": 0, "b": {"c": 2}}
-
-
def test_list(self):
-
assert [0, 1, 2] == [0, 1, 3]
-
-
def test_list2(self):
-
assert [0, 1, 2] == [0, 1, [1, 2]]
-
-
def test_tuple(self):
-
assert (0, 1, 2) ==(0, 1, 3)
-
-
def test_set(self):
-
assert {0, 10, 11, 12} == {0, 20, 21}
执行测试用例结果:
-
FAILED [ 16%]
-
test_assertions.py:1 (TestCollections.test_dict)
-
{'a': 0, 'b': 1, 'c': 0} != {'a': 0, 'b': 2, 'd': 0}
-
-
Expected :{'a': 0, 'b': 2, 'd': 0}
-
Actual :{'a': 0, 'b': 1, 'c': 0}
-
-
-
self =
-
-
def test_dict(self):
-
> assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0}
-
E AssertionError: assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0}
-
-
test_assertions.py:3: AssertionError
-
FAILED [ 33%]
-
test_assertions.py:4 (TestCollections.test_dict2)
-
{'a': 0, 'b': {'c': 0}} != {'a': 0, 'b': {'c': 2}}
-
-
Expected :{'a': 0, 'b': {'c': 2}}
-
Actual :{'a': 0, 'b': {'c': 0}}
-
-
-
self =
-
-
def test_dict2(self):
-
> assert {"a": 0, "b": {"c": 0}} == {"a": 0, "b": {"c": 2}}
-
E AssertionError: assert {'a': 0, 'b': {'c': 0}} == {'a': 0, 'b': {'c': 2}}
-
-
test_assertions.py:6: AssertionError
-
FAILED [ 50%]
-
test_assertions.py:7 (TestCollections.test_list)
-
[0, 1, 2] != [0, 1, 3]
-
-
Expected :[0, 1, 3]
-
Actual :[0, 1, 2]
-
-
-
self =
-
-
def test_list(self):
-
> assert [0, 1, 2] == [0, 1, 3]
-
E assert [0, 1, 2] == [0, 1, 3]
-
-
test_assertions.py:9: AssertionError
-
FAILED [ 66%]
-
test_assertions.py:10 (TestCollections.test_list2)
-
[0, 1, 2] != [0, 1, [1, 2]]
-
-
Expected :[0, 1, [1, 2]]
-
Actual :[0, 1, 2]
-
-
-
self =
-
-
def test_list2(self):
-
> assert [0, 1, 2] == [0, 1, [1, 2]]
-
E assert [0, 1, 2] == [0, 1, [1, 2]]
-
-
test_assertions.py:12: AssertionError
-
FAILED [ 83%]
-
test_assertions.py:13 (TestCollections.test_tuple)
-
(0, 1, 2) != (0, 1, 3)
-
-
Expected :(0, 1, 3)
-
Actual :(0, 1, 2)
-
-
-
self =
-
-
def test_tuple(self):
-
> assert (0, 1, 2) ==(0, 1, 3)
-
E assert (0, 1, 2) == (0, 1, 3)
-
-
test_assertions.py:15: AssertionError
-
FAILED [100%]
-
test_assertions.py:16 (TestCollections.test_set)
-
{0, 10, 11, 12} != {0, 20, 21}
-
-
Expected :{0, 20, 21}
-
Actual :{0, 10, 11, 12}
-
-
-
self =
-
-
def test_set(self):
-
> assert {0, 10, 11, 12} == {0, 20, 21}
-
E assert {0, 10, 11, 12} == {0, 20, 21}
-
-
test_assertions.py:18: AssertionError
-
-
Assertion failed
除了相等断言,常用的类型断言有以下几种:
-
assert xx #判断xx为真
-
assert not xx #判断xx不为真
-
assert a > b #判断a大于b
-
assert a < b #判断a小于b
-
assert a != b #判断a不等于b
-
assert a in b #判断b包含a
-
assert a not in b #判断b不包含a
更多断言的例子,大家可以参考Pytest的官方文档:
https://docs.pytest.org/en/latest/example/reportingdemo.html
这里一共有44个断言的例子,非常全面,几乎涵盖了所有的结果断言场景。
Pytest断言Excepiton
除了支持对代码正常运行的结果断言之外,Pytest也能够对 Exception 和 Warnning 进行断言,来断定某种条件下,一定会出现某种异常或者警告。在功能测试和集成测试中,这两类断言用的不多,这里简单介绍一下。
对于异常的断言,Pytest的语法是:with pytest.raises(异常类型),可以看下面的这个例子:
-
def test_zero_division():
-
with pytest.raises(ZeroDivisionError):
-
1 / 0
这个测试用例断言运算表达式1除以0会产生ZeroDivisionError异常。除了对异常类型进行断言,还可以对异常信息进行断言,比如:
-
import pytest
-
-
# content of test_assertions.py
-
class TestAssertions(object):
-
def test_zero_division(self):
-
with pytest.raises(ZeroDivisionError) as excinfo:
-
1 / 0
-
assert 'division by zero' in str(excinfo.value)
这个测试用例,就断言了excinfo.value的内容中包含division by zero这个字符串,这在需要断言具体的异常信息时非常有用。对于Warnning的断言,其实与Exception的断言的用法基本一致。这里就不介绍了,
优化断言
我们可以在异常的时候,输出一些提示信息。这样报错后。可以方便我们来查看原因。
拿最开始的例子来说,在assert后面加上说明(在断言等式后面加上 , 然在后写上说明信息):
-
# content of test_assertions.py
-
class TestAssertions(object):
-
def test_string(self):
-
assert "spam" == "eggs","校验字符串'spam'是否等于'eggs'"
执行测试用例结果:
-
FAILED [100%]
-
AssertionError: 判断字符串'spam'是否等于'eggs'
-
spam != eggs
-
-
Expected :eggs
-
Actual :spam
-
-
-
self =
-
-
def test_string(self):
-
> assert "spam" == "eggs","校验字符串'spam'是否等于'eggs'"
-
E AssertionError: 校验字符串'spam'是否等于'eggs'
-
E assert 'spam' == 'eggs'
-
-
test_assertions.py:4: AssertionError
这样当断言失败的时候,会给出自己写的失败原因了 E AssertionError: 校验字符串'spam'是否等于'eggs'
总结:
感谢每一个认真阅读我文章的人!!!
我个人整理了我这几年软件测试生涯整理的一些技术资料,包含:电子书,简历模块,各种工作模板,面试宝典,自学项目等。欢迎大家点击下方链接加入群聊免费领取,群里还有大佬帮忙解答问题,千万不要错过哦。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhggbicb
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13