pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

No obvious way to set `self.maxDiff` with `pytest_django.asserts`

Open r-thomson opened this issue 1 year ago • 8 comments
trafficstars

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.

r-thomson avatar Oct 07 '24 18:10 r-thomson

Hmm, thanks for the issue. I figure we should plumb the pytest verbosity options into maxDiff.

bluetech avatar Feb 10 '25 10:02 bluetech

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.

bluetech avatar Feb 10 '25 14:02 bluetech

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?

r-thomson avatar Feb 10 '25 20:02 r-thomson

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

roniemartinez avatar Mar 19 '25 13:03 roniemartinez

What do you guys think of my naive approach https://github.com/pytest-dev/pytest-django/pull/1191

kingbuzzman avatar Apr 06 '25 19:04 kingbuzzman

Looks good IMO, but to be honest, I would prefer that this goes to a configuration file (like pyproject.toml, pytest.ini)

roniemartinez avatar Apr 07 '25 08:04 roniemartinez

What would that look like @roniemartinez?

[tool.pytest.ini_options]
addopts = "..."
max_diff = ??????

kingbuzzman avatar Apr 07 '25 08:04 kingbuzzman

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.

roniemartinez avatar Apr 07 '25 11:04 roniemartinez