[pytest-dev] Using pytest_assertrepr_compare() for marked tests only?

Shawn Brown 03sjbrown at gmail.com
Sun May 12 19:44:01 EDT 2019


I'm playing with the idea of returning a custom explanation for tests with
a particular marker but returning Pytest's standard explanation for tests
without the marker.

I was hoping to do something like this...

# content of conftest.py
def pytest_assertrepr_compare(config, op, left, right):
    if <current test uses "mymark">:
        return ['My custom explanation!']


# content of test_mystuff.py
import pytest

def test_mystuff1():
    assert 100 == 105

@pytest.mark.mymark
def test_mystuff2():
    assert 100 == 105


# Desired Output
__________ test_mystuff1 __________
    def test_mystuff1():
>       assert 100 == 105
E       assert 100 == 105
E         -100
E         +105
test_mystuff.py:4: AssertionError
__________ test_mystuff2 __________
    @pytest.mark.mymark
    def test_mystuff2():
>       assert 100 == 105
E       assert My custom explanation!
test_mystuff.py:8: AssertionError


I've looked through the plugin API but I don't see a straightforward way of
doing this. I was hoping I could get a reference to the current test
through the config (even if it was a convoluted reference) but I don't know
if this is possible. Digging deeper, I see some undesirable approaches I
could probably use to accomplish this (e.g., monkey-patch util._reprcompare
or possibly maintain a plugin-level global that points to the current test)
but nothing that seems to work with the grain of the plugin API.

So I have a few related questions I'm hoping someone can help me with:

Q: Is there a way to reference the current test from within
pytest_assertrepr_compare()?

Q: If not, is there a decent way to implement something like this that
isn't too hacky?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20190512/6bcc03cf/attachment.html>


More information about the pytest-dev mailing list