pytest-dev / pytest-dev/pytest
Failed assertion explanation when using conditionals
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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