相关疑难解决方法(0)

如何正确断言在pytest中引发异常?

码:

# coding=utf-8
import pytest


def whatever():
    return 9/0

def test_whatever():
    try:
        whatever()
    except ZeroDivisionError as exc:
        pytest.fail(exc, pytrace=True)
Run Code Online (Sandbox Code Playgroud)

输出:

================================ test session starts =================================
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
plugins: django, cov
collected 1 items 

pytest_test.py F

====================================== FAILURES ======================================
___________________________________ test_whatever ____________________________________

    def test_whatever():
        try:
            whatever()
        except ZeroDivisionError as exc:
>           pytest.fail(exc, pytrace=True)
E           Failed: integer division or modulo by zero

pytest_test.py:12: Failed
============================== 1 failed in 1.16 seconds ==============================
Run Code Online (Sandbox Code Playgroud)

如何使pytest打印回溯,所以我会看到whatever函数中引发异常的位置?

python testing unit-testing pytest

246
推荐指数
8
解决办法
12万
查看次数

标签 统计

pytest ×1

python ×1

testing ×1

unit-testing ×1