簡體   English   中英

pytest.skip('Output string') 在哪里打印?

[英]Where does pytest.skip('Output string') get printed?

我在名為 test_me.py 的 python 模塊中有以下代碼:

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

def test_func(test_me):
    assert ...

輸出看起來像:

tests/folder/test_me.py::test_me SKIPPED

問題:“測試消息”在哪里打印或輸出? 我無法在任何地方看到或找到它。

根據Pytest 文檔,您可以使用-rs標志來顯示它。

$ 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

不確定這是否是特定於平台的,或者它是否適用於 OP 配置,因為 OP 沒有提供任何特定信息。

我不相信在測試運行期間有任何內置的方式來打印這些消息。 另一種方法是創建自己的跳過函數來執行此操作:

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')

你也可以把這個自定義函數變成一個裝飾器。

粗略地看一下 pytest 的代碼,會發現它被包裝成異常的消息,該異常是在調用skip()本身后引發的。 它的行為雖然沒有明確記錄,但在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)

最終,這個異常通過幾個層被BaseException起來,最后作為一個BaseException拋出。 因此,您應該能夠通過捕獲關聯的異常並讀取其異常消息( 相關 SO 線程)來訪問消息本身。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM