3

I have the following code in a python module called test_me.py:

@pytest.fixture()
def test_me():
     if condition:
        pytest.skip('Test Message')

def test_func(test_me):
    assert ...

The output looks like:

tests/folder/test_me.py::test_me SKIPPED

Question: Where does 'Test Message' get printed or output? I can't see or find it anywhere.

azro
  • 53,056
  • 7
  • 34
  • 70
Parazyne
  • 31
  • 4
  • 1
    I was going to refer you to the official documentation, but it doesn't appear to explain this. – Karl Knechtel Nov 10 '20 at 19:54
  • @KarlKnechtel exactly. It was from the original docs I got the idea to use this feature but it doesn't seem to work as I expected. – Parazyne Nov 10 '20 at 23:58

3 Answers3

1

According to the Pytest documentation, you can use the -rs flag to show it.

$ pytest -rs
======================== test session starts ========================
platform darwin -- Python 3.7.6, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: ...
collected 1 item                                                    

test_sample.py s                                              [100%]

====================== short test summary info ======================
SKIPPED [1] test_sample.py:5: Test Message
======================== 1 skipped in 0.02s =========================
import pytest

@pytest.fixture()
def test_me():
   pytest.skip('Test Message')

def test_1(test_me):
   pass

Not sure if this is platform-specific, or if it works with OPs configuration, since OP didn't provide any specific info.

ATOMP
  • 1,311
  • 10
  • 29
0

I don't believe there is any built-in way of printing these messages during the test runs. An alternative is to create your own skip function that does this:

def skip(message):
    print(message)  # you can add some additional info around the message
    pytest.skip()

@pytest.fixture()
def test_me():
     if condition:
        skip('Test Message')

You could probably turn this custom function into a decorator too.

Aziz Sonawalla
  • 2,482
  • 1
  • 5
  • 6
0

A cursory look at the code for pytest points to the fact that it becomes wrapped up as the message for the exception that's raised off the back of calling skip() itself. Its behavior, while not explicitly documented, is defined in outcomes.py:

def skip(msg: str = "", *, allow_module_level: bool = False) -> "NoReturn":
    """Skip an executing test with the given message.
    This function should be called only during testing (setup, call or teardown) or
    during collection by using the ``allow_module_level`` flag.  This function can
    be called in doctests as well.
    :param bool allow_module_level:
        Allows this function to be called at module level, skipping the rest
        of the module. Defaults to False.
    .. note::
        It is better to use the :ref:`pytest.mark.skipif ref` marker when
        possible to declare a test to be skipped under certain conditions
        like mismatching platforms or dependencies.
        Similarly, use the ``# doctest: +SKIP`` directive (see `doctest.SKIP
        <https://docs.python.org/3/library/doctest.html#doctest.SKIP>`_)
        to skip a doctest statically.
    """
    __tracebackhide__ = True
    raise Skipped(msg=msg, allow_module_level=allow_module_level)

Eventually, this exception is balled up through several layers and finally thrown as a BaseException. As such, you should be able to access the message itself by trapping the associated exception and reading its exception message (relevant SO thread).

esqew
  • 42,425
  • 27
  • 92
  • 132