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

Floris Bruynooghe flub at devork.be
Mon May 13 15:59:00 EDT 2019


Hi Shawn,

On Sun 12 May 2019 at 19:44 -0400, Shawn Brown wrote:

> 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.

Interesting, would you mind describing your usecase a bit more why this
makes sense?  Not because I want to stop such an feature but really just
curious what information you want that you don't have in the type.

> 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?

As Florian said, currently we didn't think of exposing the Item to this
hook, but it sounds reasonable to have.  Such a change would be
backwards compatible so would be a great feature for you to contribute?
IIRC the mechanism of how the hook is provided should allow to add the
item to the call.  If you're up to creating the feature and would like
some guidance I'd be happy to help, perhaps create an issue on github to
discuss further details (and follow up on this thread with a link).

Kind Regards,
Floris


More information about the pytest-dev mailing list