Marking Tests as “Happy” or “Sad” with pytest

I was watching the video of a talk on Advanced pytest from EuroPython 2019 where the presenter (also a pytest core developer) used some interesting decorators in his slides (actually his blog). Being rather familiar with @pytest.mark.parametrize(), it wasn’t entirely obvious at first what @pytest.mark.wip, @pytest.mark.happy, & @pytest.mark.sad were doing or functionality they provided. However, a small amount of patience–& checking out the pytest documentation–revealed that they were custom marks.

Before I get into the technical details, it’s worth mentioning that I’d recently been adding “Happy path” or “Sad path” in the docstrings of my tests. (“Happy path” implies that the object under test should work/pass; “sad path” for objects that should fail.) These marks move this need out of the docstring & place them at the top of the test. But what really sold me on these custom marks–and oh, by the way, he used @pytest.mark.wip is for any work-in-progress test(s)–is that you can run all tests with a particular custom mark like this:

# To run all "happy path" tests--regardless of what file(s), module(s),
# and/or classes they may be found in!

pytest -m happy

# To run all "sad path" tests

pytest -m sad

How awesome is that?!

(Also, the video later reveals that the @pytest.mark.wip was used with a pytest plugin that in his case, which would run that particular test 10X for additional consistency checks. Cool use!)

1 thought on “Marking Tests as “Happy” or “Sad” with pytest

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.