How can I make pytest
fail if there is a print
statement in the code?
Also (for security it can be useful), it will be great if the print does not actually get executed or shown in the CI.
How can I make pytest
fail if there is a print
statement in the code?
Also (for security it can be useful), it will be great if the print does not actually get executed or shown in the CI.
Try to use session scope autouse fixture for print
monkey patching , like so:
@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
Put this fixture in the conftest.py
file, which is located in your test
directory. We can define the fixture functions in this file to make them accessible across multiple test files. You can read more about this file here: