如果打印存在,如何使 pytest 失败?
How to make pytest fail if print exists?
如果代码中有 print
语句,如何使 pytest
失败?
此外(出于安全考虑,它可能很有用),如果打印没有实际执行或显示在 CI 中,那就太好了。
尝试为 print
monkey patching 使用 session scope autouse fixture,像这样:
@pytest.fixture(scope='session', autouse=True)
def mock_print():
with mock.patch("builtins.print", side_effect=Exception('Print is not allowed!')) as _fixture:
yield _fixture
将此固定装置放入 conftest.py
文件中,该文件位于您的 test
目录中。我们可以在这个文件中定义夹具函数,使它们可以跨多个测试文件访问。您可以在此处阅读有关此文件的更多信息:
如果代码中有 print
语句,如何使 pytest
失败?
此外(出于安全考虑,它可能很有用),如果打印没有实际执行或显示在 CI 中,那就太好了。
尝试为 print
monkey patching 使用 session scope autouse fixture,像这样:
@pytest.fixture(scope='session', autouse=True)
def mock_print():
with mock.patch("builtins.print", side_effect=Exception('Print is not allowed!')) as _fixture:
yield _fixture
将此固定装置放入 conftest.py
文件中,该文件位于您的 test
目录中。我们可以在这个文件中定义夹具函数,使它们可以跨多个测试文件访问。您可以在此处阅读有关此文件的更多信息: