pythonpytest不显示断言差异

2024-10-01 07:40:13 发布

您现在位置:Python中文网/ 问答频道 /正文

以下测试:

def test_something():
    assert "ddd" == "pepe"

当pytest出现以下错误时运行:

^{pr2}$

但是,如果我们将assert方法移动到另一个文件assertion.py

class CustomerAssertor(object):
    def __init__(self,name):
        self.name =name

    def assert_name(self,expected):
        assert self.name ==expected

我们将测试改为:

from sql_gen.test.utils.assertion_util import CustomerAssertor

def test_something():
    CustomerAssertor("ddd").assert_name("pepe")

现在我得到以下错误:

self = <assertions.CustomerAssertor object at 0x7fbcc3d31588>, expected = 'pepe'

    def assert_name(self,expected):
>       assert self.name ==expected
E       AssertionError

这条消息并没有告诉您名称的值是什么,这是为什么?在


Tags: nametestselfobjectpytestdef错误assert
1条回答
网友
1楼 · 发布于 2024-10-01 07:40:13

docs

Reporting details about a failing assertion is achieved by rewriting assert statements before they are run. Rewritten assert statements put introspection information into the assertion failure message. pytest only rewrites test modules directly discovered by its test collection process, so asserts in supporting modules which are not themselves test modules will not be rewritten.

You can manually enable assertion rewriting for an imported module by calling register_assert_rewrite before you import it (a good place to do that is in conftest.py).

相关问题 更多 >