Skip to content

No obvious way to set self.maxDiff with pytest_django.asserts #1155

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
r-thomson opened this issue Oct 7, 2024 · 8 comments
Open

No obvious way to set self.maxDiff with pytest_django.asserts #1155

r-thomson opened this issue Oct 7, 2024 · 8 comments

Comments

@r-thomson
Copy link

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.

@bluetech
Copy link
Member

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

@bluetech
Copy link
Member

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.

@r-thomson
Copy link
Author

From looking at the source, it looks like a TestCase is instantiated here:

test_case = TestCase("run")

Is it not possible at this location to check some global configuration, e.g. the pytest verbosity?

@roniemartinez
Copy link

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

@kingbuzzman
Copy link
Contributor

What do you guys think of my naive approach #1191

@roniemartinez
Copy link

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

@kingbuzzman
Copy link
Contributor

What would that look like @roniemartinez?

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

@roniemartinez
Copy link

roniemartinez commented Apr 7, 2025

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants