I'm already aware of the stackoverflow answer describing how to set-up a new section with a general message for all tests, but I would like to have a special section to only show up if a test from a certain module fails.
The story behind this is that I wrote a test for an analysis I conducted. I still want to develop the analysis functions further, but I'm quite confident that the results are correct. Therefore, I pickled all the results of my original version and wrote a test which compares the output under the current functions to the pickled original output (test_stability.py
). To help my colleagues and myself, I now want to create a custom message which appears when a test in test_stability.py
fails.
So, I have a file test_stability.py
containing two tests:
#test_stability.py
import pytest
def test_form_stable():
assert True
def test_content_stable():
assert False
And another file containting two regular test test_regular.py
:
#test_regular.py
import pytest
def test_first_reg():
assert True
def test_second_reg():
assert False
Ideally my pytest report would look like this:
collected 4 items
test_stability.py::test_form_stable PASSED
test_stability.py::test_content_stable FAILED
test_regular.py::test_first_reg PASSED
test_regular.py::test_second_reg Failed
============================================= FAILURES =============================================
____________________________________________ test_content_stability ____________________________________________
def test_content_stability():
> assert False
E assert False
test_spam.py:9: AssertionError
____________________________________________ test_second_reg ____________________________________________
def test_second_reg():
> assert False
E assert False
test_spam.py:9: AssertionError
---------------------------------------- My custom section -----------------------------------------
test_stability.py::test_content_stability says: "My custom message"
================================ 2 failed, 2 passed in 0.07 seconds ================================