【pytest】assert 断言

使用 assert 断言:

assert 断言是判断测试执行结果是否符合预期的重要方法!

pytest 中的 assert 要比 unittest 中的 assertEqual 等断言方法灵活很多,它支持各种条件表达式,只要表达式成立(条件最终为True),便是断言通过,如果表达式失败(条件最终为False),便是断言失败。

assert 支持的表达式:

# test_moduleName.py  测试模块名

class TestClassName:
    """测试类"""

    def test_func_name(self):
        """测试方法"""

        # 预期结果
        a = ...

        # 用例执行结果
        x = ''

        # 断言
        assert x == a  # 是否相等
        assert x != a  # 是否不相等
        assert x is a  # 是否是同一内存地址
        assert x in a  # a 是否包含 x
        assert x not in a  # a 是否不包含 x
        assert x > a  # 是否大于 a
        assert x < a  # 是否小于 a
        assert isinstance(x, dict)  # x 是否是 dict 类型
        ...

断言结果分析:

当断言通过时,pytest 便只会输出用例执行结果:PASSED;

class TestClassName:
    """测试类"""

    def test_func_name(self):
        """测试方法"""
        
        a = 1
        b = 2

        # 断言失败
        assert a < b
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: E:\PycharmProject\Demo, inifile: pytest.ini
collected 1 item

test_moduleName.py::TestClassName::test_func_name PASSED                 [100%]

============================== 1 passed in 0.04s ==============================

当断言失败时,pytest 便会打印断言失败的回溯信息,显示断言失败的位置和变量值等信息:

class TestClassName:
    """测试类"""

    def test_func_name(self):
        """测试方法"""
        
        a = 1
        b = 2

        # 断言失败
        assert a == b
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: E:\PycharmProject\Demo, inifile: pytest.ini
collected 1 item

test_moduleName.py::TestClassName::test_func_name FAILED                 [100%]

================================== FAILURES ===================================
________________________ TestClassName.test_func_name _________________________

self = <pytestDemo.pytest_new_demo.test_moduleName.TestClassName object at 0x0000021C6850B148>

    def test_func_name(self):
        """测试方法"""
    
        a = 1
        b = 2
    
>       assert a == b
E       assert 1 == 2
E         -1
E         +2

test_moduleName.py:60: AssertionError
============================== 1 failed in 0.48s ==============================

断言异常信息:

pytest 的 assert 除了可以断言一般的条件表达式外,还可以断言异常信息!

使用上下文管理器 with 结合 pytest.raises(xxx) 来断言是否抛出 xxx 异常,如是则用例 pass,如没有抛出 xxx 异常信息,则用例 failed;

  1. 下面这段代码 pytest 检测到预期的异常信息,所以用例 PASSED;
class TestClassName:
    """测试类"""

    def test_func_name(self):
        """测试方法"""

        # 断言是否抛出 ZeroDivisionError
        with pytest.raises(ZeroDivisionError):
            # 除法运算中分母不能为 0 ,否则会抛出异常 ZeroDivisionError;
            assert 1 / 0  

    2. 这段代码 pytest 未检测到预期的异常信息,所以用例 Failed;

class TestClassName:
    """测试类"""

    def test_func_name(self):
        """测试方法"""

        # 断言是否抛出 ZeroDivisionError
        with pytest.raises(ZeroDivisionError):
            # 不会抛出 ZeroDivisionError ,用例会失败;
            assert 5 // 2
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: E:\PycharmProject\Demo, inifile: pytest.ini
collected 1 item

test_moduleName.py::TestClassName::test_func_name FAILED                 [100%]

================================== FAILURES ===================================
________________________ TestClassName.test_func_name _________________________

self = <pytestDemo.pytest_new_demo.test_moduleName.TestClassName object at 0x000001F1F87CB588>

    def test_func_name(self):
        """测试方法"""
    
        # 断言是否抛出 ZeroDivisionError
        with pytest.raises(ZeroDivisionError):
            # 除法运算中分母不能为 0 ,否则会抛出异常 ZeroDivisionError;
>           assert 5 // 2
E           Failed: DID NOT RAISE <class 'ZeroDivisionError'>

test_moduleName.py:60: Failed
============================== 1 failed in 0.07s ==============================

Process finished with exit code 0
发布了44 篇原创文章 · 获赞 23 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/waitan2018/article/details/104032935