pytest-dev / pytest-dev/pytest

Failed assertion explanation when using conditionals

Open
#7,727 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: rewrite
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Considering the following python file:

def myfunc():
    return 1

def test_nice():
    assert myfunc() == 0

def test_not_nice():
    assert myfunc() == 0 if True else 1

The second test test_not_nice does not have a nice explanation of the failure (where we get to see the value of myfunc), as we have in test_nice, even though they are basically the same in the end.

=================================== FAILURES ===================================
__________________________________ test_nice ___________________________________

    def test_nice():
>       assert myfunc() == 0
E       assert 1 == 0
E        +  where 1 = myfunc()

test_one.py:5: AssertionError
___________________________________ test_not_nice ___________________________________

    def test_not_nice():
>       assert myfunc() == 0 if True else 1
E       assert False

test_one.py:8: AssertionError
=========================== short test summary info ============================
FAILED test_one.py::test_nice - assert 1 == 0
FAILED test_one.py::test_not_nice - assert False
============================== 2 failed in 0.08s ===============================

It seems to me that the assertion rewriting could also evaluate the right handside of the == comparison just as it does with myfunc.

Tested in pytest 6.0.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the two examples from test_one.py, especially test_not_nice, using the assertion-rewriting behavior described for pytest 6.0.1. Start by tracing assertion rewriting for the conditional expression and comparison. Done means the conditional case provides a useful explanation that includes the evaluated value, comparable to test_nice.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.