• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pytest自动化测试用例的断言

武飞扬头像
小米测试开发
帮助1

目录

前言

pytest断言assert的用法

1、断言字符串

2、断言函数返回值

3、断言集合类型

Pytest断言Excepiton

优化断言

总结:


前言

测试的主要工作目标就是验证实际结果与预期结果是否一致;在接口自动化测试中,通过断言来实现这一目标。Pytest中断言是通过assert语句实现的(pytest对Python原生的assert语句进行了优化),确定实际情况是否与预期一致。

pytest断言assert的用法

在自动化测试用例中,最常用的断言是相等断言,就是断言预期结果和实际结果是一致的。pytest通过 “assert 实际结果 == 预期结果” 实现。通常我们断言的预期结果和实际结果的数据类型包括字符串、元组、字典、列表和对象。

  学新通

1、断言字符串

  1.  
    # content of test_assertions.py
  2.  
    class TestAssertions(object):
  3.  
    def test_string(self):
  4.  
    assert "spam" == "eggs"

执行测试用例结果(在pycharm中以pytest执行用例,后面示例都如此):

  1.  
    test_assertions.py:2 (TestAssertions.test_string)
  2.  
    spam != eggs
  3.  
     
  4.  
    Expected :eggs
  5.  
    Actual :spam
  6.  
     
  7.  
     
  8.  
    self =
  9.  
     
  10.  
    def test_string(self):
  11.  
    > assert "spam" == "eggs"
  12.  
    E AssertionError: assert 'spam' == 'eggs'
  13.  
     
  14.  
    test_assertions.py:4: AssertionError

说明:Expected 为期望结果(即 == 右侧的预期结果),Actual 为实际结果(即 == 左侧的实际结果),> 后面为出错的代码行,E 后面为错误信息。

2、断言函数返回值

  1.  
    class TestAssertions(object):
  2.  
    def test_function(self):
  3.  
    def f():
  4.  
    return [1, 2, 3]
  5.  
     
  6.  
    assert f() == [1, 2, 4]

执行测试用例结果:

  1.  
    test_assertions.py:1 (TestAssertions.test_function)
  2.  
    [1, 2, 3] != [1, 2, 4]
  3.  
     
  4.  
    Expected :[1, 2, 4]
  5.  
    Actual :[1, 2, 3]
  6.  
     
  7.  
     
  8.  
    self =
  9.  
     
  10.  
    def test_function(self):
  11.  
    def f():
  12.  
    return [1, 2, 3]
  13.  
     
  14.  
    > assert f() == [1, 2, 4]
  15.  
    E assert [1, 2, 3] == [1, 2, 4]
  16.  
     
  17.  
    test_assertions.py:6: AssertionError
学新通

3、断言集合类型

断言字典、列表、元组集合等类型在测试中也是很常见的。比如下面这段测试用例代码:

  1.  
    class TestCollections(object):
  2.  
    def test_dict(self):
  3.  
    assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0}
  4.  
     
  5.  
    def test_dict2(self):
  6.  
    assert {"a": 0, "b": {"c": 0}} == {"a": 0, "b": {"c": 2}}
  7.  
     
  8.  
    def test_list(self):
  9.  
    assert [0, 1, 2] == [0, 1, 3]
  10.  
     
  11.  
    def test_list2(self):
  12.  
    assert [0, 1, 2] == [0, 1, [1, 2]]
  13.  
     
  14.  
    def test_tuple(self):
  15.  
    assert (0, 1, 2) ==(0, 1, 3)
  16.  
     
  17.  
    def test_set(self):
  18.  
    assert {0, 10, 11, 12} == {0, 20, 21}
学新通

执行测试用例结果:

  1.  
    FAILED [ 16%]
  2.  
    test_assertions.py:1 (TestCollections.test_dict)
  3.  
    {'a': 0, 'b': 1, 'c': 0} != {'a': 0, 'b': 2, 'd': 0}
  4.  
     
  5.  
    Expected :{'a': 0, 'b': 2, 'd': 0}
  6.  
    Actual :{'a': 0, 'b': 1, 'c': 0}
  7.  
     
  8.  
     
  9.  
    self =
  10.  
     
  11.  
    def test_dict(self):
  12.  
    > assert {"a": 0, "b": 1, "c": 0} == {"a": 0, "b": 2, "d": 0}
  13.  
    E AssertionError: assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0}
  14.  
     
  15.  
    test_assertions.py:3: AssertionError
  16.  
    FAILED [ 33%]
  17.  
    test_assertions.py:4 (TestCollections.test_dict2)
  18.  
    {'a': 0, 'b': {'c': 0}} != {'a': 0, 'b': {'c': 2}}
  19.  
     
  20.  
    Expected :{'a': 0, 'b': {'c': 2}}
  21.  
    Actual :{'a': 0, 'b': {'c': 0}}
  22.  
     
  23.  
     
  24.  
    self =
  25.  
     
  26.  
    def test_dict2(self):
  27.  
    > assert {"a": 0, "b": {"c": 0}} == {"a": 0, "b": {"c": 2}}
  28.  
    E AssertionError: assert {'a': 0, 'b': {'c': 0}} == {'a': 0, 'b': {'c': 2}}
  29.  
     
  30.  
    test_assertions.py:6: AssertionError
  31.  
    FAILED [ 50%]
  32.  
    test_assertions.py:7 (TestCollections.test_list)
  33.  
    [0, 1, 2] != [0, 1, 3]
  34.  
     
  35.  
    Expected :[0, 1, 3]
  36.  
    Actual :[0, 1, 2]
  37.  
     
  38.  
     
  39.  
    self =
  40.  
     
  41.  
    def test_list(self):
  42.  
    > assert [0, 1, 2] == [0, 1, 3]
  43.  
    E assert [0, 1, 2] == [0, 1, 3]
  44.  
     
  45.  
    test_assertions.py:9: AssertionError
  46.  
    FAILED [ 66%]
  47.  
    test_assertions.py:10 (TestCollections.test_list2)
  48.  
    [0, 1, 2] != [0, 1, [1, 2]]
  49.  
     
  50.  
    Expected :[0, 1, [1, 2]]
  51.  
    Actual :[0, 1, 2]
  52.  
     
  53.  
     
  54.  
    self =
  55.  
     
  56.  
    def test_list2(self):
  57.  
    > assert [0, 1, 2] == [0, 1, [1, 2]]
  58.  
    E assert [0, 1, 2] == [0, 1, [1, 2]]
  59.  
     
  60.  
    test_assertions.py:12: AssertionError
  61.  
    FAILED [ 83%]
  62.  
    test_assertions.py:13 (TestCollections.test_tuple)
  63.  
    (0, 1, 2) != (0, 1, 3)
  64.  
     
  65.  
    Expected :(0, 1, 3)
  66.  
    Actual :(0, 1, 2)
  67.  
     
  68.  
     
  69.  
    self =
  70.  
     
  71.  
    def test_tuple(self):
  72.  
    > assert (0, 1, 2) ==(0, 1, 3)
  73.  
    E assert (0, 1, 2) == (0, 1, 3)
  74.  
     
  75.  
    test_assertions.py:15: AssertionError
  76.  
    FAILED [100%]
  77.  
    test_assertions.py:16 (TestCollections.test_set)
  78.  
    {0, 10, 11, 12} != {0, 20, 21}
  79.  
     
  80.  
    Expected :{0, 20, 21}
  81.  
    Actual :{0, 10, 11, 12}
  82.  
     
  83.  
     
  84.  
    self =
  85.  
     
  86.  
    def test_set(self):
  87.  
    > assert {0, 10, 11, 12} == {0, 20, 21}
  88.  
    E assert {0, 10, 11, 12} == {0, 20, 21}
  89.  
     
  90.  
    test_assertions.py:18: AssertionError
  91.  
     
  92.  
    Assertion failed
学新通

除了相等断言,常用的类型断言有以下几种:

  1.  
    assert xx #判断xx为真
  2.  
    assert not xx #判断xx不为真
  3.  
    assert a > b #判断a大于b
  4.  
    assert a < b #判断a小于b
  5.  
    assert a != b #判断a不等于b
  6.  
    assert a in b #判断b包含a
  7.  
    assert a not in b #判断b不包含a


更多断言的例子,大家可以参考Pytest的官方文档:
https://docs.pytest.org/en/latest/example/reportingdemo.html

这里一共有44个断言的例子,非常全面,几乎涵盖了所有的结果断言场景。

学新通

Pytest断言Excepiton

除了支持对代码正常运行的结果断言之外,Pytest也能够对 ExceptionWarnning 进行断言,来断定某种条件下,一定会出现某种异常或者警告。在功能测试和集成测试中,这两类断言用的不多,这里简单介绍一下。

对于异常的断言,Pytest的语法是:with pytest.raises(异常类型),可以看下面的这个例子:

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

这个测试用例断言运算表达式1除以0会产生ZeroDivisionError异常。除了对异常类型进行断言,还可以对异常信息进行断言,比如:

  1.  
    import pytest
  2.  
     
  3.  
    # content of test_assertions.py
  4.  
    class TestAssertions(object):
  5.  
    def test_zero_division(self):
  6.  
    with pytest.raises(ZeroDivisionError) as excinfo:
  7.  
    1 / 0
  8.  
    assert 'division by zero' in str(excinfo.value)

这个测试用例,就断言了excinfo.value的内容中包含division by zero这个字符串,这在需要断言具体的异常信息时非常有用。对于Warnning的断言,其实与Exception的断言的用法基本一致。这里就不介绍了,

优化断言

我们可以在异常的时候,输出一些提示信息。这样报错后。可以方便我们来查看原因。

拿最开始的例子来说,在assert后面加上说明(在断言等式后面加上 , 然在后写上说明信息):

  1.  
    # content of test_assertions.py
  2.  
    class TestAssertions(object):
  3.  
    def test_string(self):
  4.  
    assert "spam" == "eggs","校验字符串'spam'是否等于'eggs'"

执行测试用例结果:

  1.  
    FAILED [100%]
  2.  
    AssertionError: 判断字符串'spam'是否等于'eggs'
  3.  
    spam != eggs
  4.  
     
  5.  
    Expected :eggs
  6.  
    Actual :spam
  7.  
     
  8.  
     
  9.  
    self =
  10.  
     
  11.  
    def test_string(self):
  12.  
    > assert "spam" == "eggs","校验字符串'spam'是否等于'eggs'"
  13.  
    E AssertionError: 校验字符串'spam'是否等于'eggs'
  14.  
    E assert 'spam' == 'eggs'
  15.  
     
  16.  
    test_assertions.py:4: AssertionError
学新通

这样当断言失败的时候,会给出自己写的失败原因了 E AssertionError: 校验字符串'spam'是否等于'eggs'

总结:

感谢每一个认真阅读我文章的人!!!

我个人整理了我这几年软件测试生涯整理的一些技术资料,包含:电子书,简历模块,各种工作模板,面试宝典,自学项目等。欢迎大家点击下方链接加入群聊免费领取,群里还有大佬帮忙解答问题,千万不要错过哦。

  Python自动化测试学习交流群:全套自动化测试面试简历学习资料获取点击链接加入群聊【python自动化测试交流】:学新通http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=DhOSZDNS-qzT5QKbFQMsfJ7DsrFfKpOF&authKey=eBt+F/BK81lVLcsLKaFqnvDAVA8IdNsGC7J0YV73w8V/Jpdbby66r7vJ1rsPIifg&noverify=0&group_code=198408628

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggbicb
系列文章
更多 icon
同类精品
更多 icon
继续加载