pytest-django
pytest-django copied to clipboard
No obvious way to set `self.maxDiff` with `pytest_django.asserts`
When using pytest_django.asserts.assertXMLEqual, the diff in the console is truncated, with the following message at the end:
Diff is ### characters long. Set self.maxDiff to None to see it.
It's not obvious to me what the right way to accomplish this with pytest-django is, since the TestCase for these functions is defined internally. Right now I'm just monkeypatching unittest.case.TestCase.maxDiff, but it'd be nice to have an obvious way of doing this.
Hmm, thanks for the issue. I figure we should plumb the pytest verbosity options into maxDiff.
Looked into it. Unfortunately what the way pytest_django.asserts is currently implemented, the assertion functions are just standalone functions, and are not bound to the actual running test. I don't see a way to implement this without a lot of complexity, sorry. I suggest switching to plain assert if possible.
From looking at the source, it looks like a TestCase is instantiated here:
https://github.com/pytest-dev/pytest-django/blob/d8dc3d9a62b97c9730111ca7c656dd3b00c479c2/pytest_django/asserts.py#L25
Is it not possible at this location to check some global configuration, e.g. the pytest verbosity?
We can temporarily monkey-patch this in conftest.py but looks like an ugly solution
# top of conftest.py
import unittest
unittest.TestCase.maxDiff = None
# ... all other imports here
What do you guys think of my naive approach https://github.com/pytest-dev/pytest-django/pull/1191
Looks good IMO, but to be honest, I would prefer that this goes to a configuration file (like pyproject.toml, pytest.ini)
What would that look like @roniemartinez?
[tool.pytest.ini_options]
addopts = "..."
max_diff = ??????
Something like that. But would consult with a Pytest dev since this is for pytest-django and I am guessing that should be properly named (see https://pytest-django.readthedocs.io/en/stable/configuring_django.html)
EDIT: This is related to unittest.TestCase and not pytest-django. It requires a proper input from a Pytest dev.