Skip to content

Partially missed error description with running via Pytest #10572

Open
@david-shiko

Description

@david-shiko

Missed the important part of error description during running via Pytest.

Please compare this 2 outputs of pytest running and python running.
It's really blowmind :)

MVE:

(venv) david@david-ThinkPad-E480:~/PycharmProjects$ cat tests/delme.py
# Put this code inside your "conftest.py" file
from unittest.mock import Mock
class F:
    def x(self):
        pass
m = Mock()
m.x = Mock(spec_set=F.x)  # Antipattern! Use just "return_value"
m.x()
m.x.assert_called_once_with()

Pytest output:

(venv) david@david-ThinkPad-E480:~/PycharmProjects$ python3.10 -m pytest tests/delme.py 
ImportError while loading conftest '/home/david/PycharmProjects/tests/conftest.py'.
tests/conftest.py:1292: in <module>
    m.x.assert_called_once_with()
/usr/lib/python3.10/unittest/mock.py:941: in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
/usr/lib/python3.10/unittest/mock.py:929: in assert_called_with
    raise AssertionError(_error_message()) from cause
E   AssertionError: expected call not found.
E   Expected: x()  # WTF?
E   Actual: x()  # WTF?

Python output (missed important part of error):

(venv) david@david-ThinkPad-E480:~/PycharmProjects$ python3.10 tests/delme.py 
TypeError: missing a required argument: 'self'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/david/PycharmProjects/tests/delme.py", line 8, in <module>
    m.x.assert_called_once_with()
  File "/usr/lib/python3.10/unittest/mock.py", line 941, in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
  File "/usr/lib/python3.10/unittest/mock.py", line 929, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: expected call not found.
Expected: x()  # The real reason above
Actual: x()  # The real reason above
(venv) david@david-ThinkPad-E480:~/PycharmProjects$ pip list | grep pytest
pytest                7.1.3
(venv) david@david-ThinkPad-E480:~/PycharmProjects$ uname -a
Linux david-ThinkPad-E480 5.15.0-56-generic #62~20.04.1-Ubuntu SMP Tue Nov 22 21:24:20 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: reportingrelated to terminal output and user-facing messages and errors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions