Skip to content

Test works normally but fails under pytest with assertion rewriting #4412

@oscarbenjamin

Description

@oscarbenjamin

Given a test file p.py containing:

def test_add():
    add = lambda *t: sum(t)
    l = range(8)
    e = iter(l)
    assert sum(l[:4]) == add(*[next(e) for j in range(4)])

the test doesn't work under pytest with assertion rewriting. Note that the expression on the rhs of == will return a different result when evaluated a second time. The first time it is evaluated it will give 6 and the second time 22.

The test passes (as expected) when imported normally:

>>> import p
>>> p.test_add()

It also works under pytest without assertion rewriting

$ pytest p.py --assert=plain
==================================================================== test session starts =====================================================================
platform darwin -- Python 3.6.2, pytest-3.10.0, py-1.7.0, pluggy-0.8.0
rootdir: /Users/enojb/current/sympy/sympy, inifile:
plugins: faulthandler-1.5.0
collected 1 item                                                                                                                                             

p.py .                                                                                                                                                 [100%]

================================================================== 1 passed in 0.01 seconds ==================================================================

However it fails under pytest with assertion rewriting

$ pytest p.py
==================================================================== test session starts =====================================================================
platform darwin -- Python 3.6.2, pytest-3.10.0, py-1.7.0, pluggy-0.8.0
rootdir: /Users/enojb/current/sympy/sympy, inifile:
plugins: faulthandler-1.5.0
collected 1 item                                                                                                                                             

p.py F                                                                                                                                                 [100%]

========================================================================== FAILURES ==========================================================================
__________________________________________________________________________ test_add __________________________________________________________________________

    def test_add():
        add = lambda *t: sum(t)
        l = range(8)
        e = iter(l)
>       assert sum(l[:4]) == add(*[next(e) for j in range(4)])
E       assert 6 == 22
E        +  where 6 = sum(range(0, 4))
E        +  and   22 = <function test_add.<locals>.<lambda> at 0x10454e048>(*[0, 1, 2, 3])

p.py:6: AssertionError
================================================================== 1 failed in 0.08 seconds ==================================================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: rewriterelated to the assertion rewrite mechanismtype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions