Pytest用例中断言详解

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

Pytest用例中断言详解

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也能够对 ExceptionWarnning 进行断言,来断定某种条件下,一定会出现某种异常或者警告。在功能测试和集成测试中,这两类断言用的不多,这里简单介绍一下。

对于异常的断言,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这个字符串,这在需要断言具体的异常信息时非常有用。

上一页12下一页


留言