The assert keyword in python takes an expression, and an optional message to display. The message is useful when running pytest.
assert False, 'My message'
In addition to strings, you can raise any object, for example a dict or list.
However, pytest will truncate it if the message is a list with long strings:
def test():
assert False, ['a' * 1000, 'a' * 1000, 'a' * 1000]
Pytest will show this which is confusing, because it appears like one item in the list instead of three:
def test():
> assert False, ['a' * 1000, 'a' * 1000, 'a' * 1000]
E AssertionError: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
E assert False
How can I prevent pytest from truncating the assert message if the message is a list/dict with long strings?
The same is true for dictionaries, pytest makes it look like there is only one item in the dictionary, not three:
def test():
assert False, {'foo': 'a' * 1000, 'bar': 'a' * 1000, 'baz': 'a' * 1000}
def test():
> assert False, {'foo': 'a' * 1000, 'bar': 'a' * 1000, 'baz': 'a' * 1000}
E AssertionError: {'bar': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'}
E assert False
The interesting thing is that if you convert it to a string it will not truncate the string:
def test():
assert False, '\n'.join(['a' * 1000, 'a' * 1000, 'a' * 1000])
I don't want to have to remember to do this everytime I am returning a list of long strings.
CodePudding user response:
pytest rewrites all the assertions it finds in the source code unless you explicitly disable this behaviour. To disable with
pytest test.py --assert=plain
Or by writing it in the module docstring
"""module xyz
PYTEST_DONT_REWRITE
"""
You can find here the documentation and, if you are interested, here the implementation of the visitor that rewrites all the assert
statement.
CodePudding user response:
You can use the command line flag --fixtures
. It does add extra content to the beginning of the output.
➜ pytest --fixtures eg.py
=============================================== test session starts ===============================================
platform linux -- Python 3.8.10, pytest-7.0.1, pluggy-1.0.0
rootdir: /c/Users/freeda/dev
collected 0 items / 1 error
cache -- .../_pytest/cacheprovider.py:510
Return a cache object that can persist state between testing sessions.
capsys -- .../_pytest/capture.py:878
Enable text capturing of writes to ``sys.stdout`` and ``sys.stderr``.
capsysbinary -- .../_pytest/capture.py:895
Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``.
capfd -- .../_pytest/capture.py:912
Enable text capturing of writes to file descriptors ``1`` and ``2``.
capfdbinary -- .../_pytest/capture.py:929
Enable bytes capturing of writes to file descriptors ``1`` and ``2``.
doctest_namespace [session scope] -- .../_pytest/doctest.py:731
Fixture that returns a :py:class:`dict` that will be injected into the
namespace of doctests.
pytestconfig [session scope] -- .../_pytest/fixtures.py:1365
Session-scoped fixture that returns the session's :class:`pytest.Config`'
object.
record_property -- .../_pytest/junitxml.py:282
Add extra properties to the calling test.
record_xml_attribute -- .../_pytest/junitxml.py:305
Add extra xml attributes to the tag for the calling test.
record_testsuite_property [session scope] -- .../_pytest/junitxml.py:343
Record a new ``<property>`` tag as child of the root ``<testsuite>``.
tmpdir_factory [session scope] -- .../_pytest/legacypath.py:295
Return a :class:`pytest.TempdirFactory` instance for the test session.
tmpdir -- .../_pytest/legacypath.py:302
Return a temporary directory path object which is unique to each test
function invocation, created as a sub directory of the base temporary
directory.
caplog -- .../_pytest/logging.py:483
Access and control log capturing.
monkeypatch -- .../_pytest/monkeypatch.py:29
A convenient fixture for monkey-patching.
recwarn -- .../_pytest/recwarn.py:29
Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions.
tmp_path_factory [session scope] -- .../_pytest/tmpdir.py:183
Return a :class:`pytest.TempPathFactory` instance for the test session.
tmp_path -- .../_pytest/tmpdir.py:198
Return a temporary directory path object which is unique to each test
function invocation, created as a sub directory of the base temporary
directory.
===================================================== ERRORS ======================================================
_____________________________________ ERROR collecting ivz-edp-airflow/eg.py ______________________________________
eg.py:6: in <module>
assert False, d
E AssertionError: {'1': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'}
============================================= short test summary info =============================================
ERROR eg.py - AssertionError: {'1': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
================================================ 1 error in 0.08s =================================================
Old answer
While not exactly solving your issue, there is a workaround that will show the full value of your second argument to assert
. This requires you to store that second argument as a variable before passing it to assert
.
# eg.py
d = {'1': 'a' * 1000, '2': 'a' * 1000}
assert False, d
# pytest.ini
[pytest]
addopts = -vv --showlocals
➜ pytest eg.py
=============================================== test session starts ===============================================
platform linux -- Python 3.8.10, pytest-7.0.1, pluggy-1.0.0 -- /home/ado/.virtualenvs/mwaa/bin/python
cachedir: .pytest_cache
rootdir: /c/Users/freeda/dev/ivz-edp-airflow, configfile: pytest.ini
collected 0 items / 1 error
===================================================== ERRORS ======================================================
_____________________________________________ ERROR collecting eg.py ______________________________________________
eg.py:2: in <module>
assert False, d
E AssertionError: {'1': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'}
E assert False
__builtins__ = <builtins>
__cached__ = '/c/Users/freeda/dev/ivz-edp-airflow/__pycache__/eg.cpython-38.pyc'
__doc__ = None
__file__ = '/c/Users/freeda/dev/ivz-edp-airflow/eg.py'
__loader__ = <_pytest.assertion.rewrite.AssertionRewritingHook object at 0x7fc4c35afb50>
__name__ = 'eg'
__package__ = ''
__spec__ = ModuleSpec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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'}
============================================= short test summary info =============================================
ERROR eg.py - AssertionError: {'1': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================ 1 error in 0.11s =================================================
This solution is obviously quite double-edged.
# eg_yuck.py
import string
d = {'1': 'a' * 1000, '2': 'a' * 1000}
other = {ord(s): s * 1000 for s in string.printable}
assert False, d