In the below example, the normal Python traceback correctly points to line 5, where the actual division by zero happens. pytest points to line 4.
$ cat test.py
def test_thing():
x = 0
y = 3
assert [1 / y,
1 / x]
if __name__ == '__main__':
test_thing()
$ python3.6 test.py
Traceback (most recent call last):
File "test.py", line 8, in <module>
test_thing()
File "test.py", line 5, in test_thing
1 / x]
ZeroDivisionError: division by zero
$ python3.6 -m pytest test.py
================================================================================ test session starts ================================================================================
platform darwin -- Python 3.6.3, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: /Users/alexhall, inifile:
collected 1 item
test.py F [100%]
===================================================================================== FAILURES ======================================================================================
____________________________________________________________________________________ test_thing _____________________________________________________________________________________
def test_thing():
x = 0
y = 3
> assert [1 / y,
1 / x]
E ZeroDivisionError: division by zero
test.py:4: ZeroDivisionError
============================================================================= 1 failed in 0.13 seconds ==============================================================================
Theoretically this certainly seems like a serious issue, but for the practical question of how often this is a problem, don't count me as a datapoint. I only noticed this problem while playing with some dark magic code that uses the line numbers of frames. The above example is completely contrived.
Thanks for the report @alexmojaki!
This also appears to be relevant for doctests (https://github.com/Frozenball/pytest-sugar/issues/134).
It was bisected to https://github.com/pytest-dev/pytest/commit/fbc45be83f279f936121355649728f7aeec6e6a6 (https://github.com/Frozenball/pytest-sugar/issues/134#issuecomment-406067245) - /cc @will133.
This is due to assertion rewriting, with --assert=plain pytest also reports line 5.
Probably, bug is here: https://github.com/pytest-dev/pytest/blob/1df593f97890245a8eaaa89444a3ba7bada2a3b0/src/_pytest/assertion/rewrite.py#L891
Rewrite ignores multiline assert statements and uses assert.lineno - first assert line num (reporting do nothing wrong). Assert statement comes in one piece and there is no way for reporter to determine correct line for now. May be we can detect correct substatement line and attach to Assert object for future usage (by reporter a.k.a. error printer).