I'm writing tests using Pytest. I have a fixture like this:
@pytest.yield_fixture(autouse=True, scope='session')
def manage_tests():
print("Do stuff...")
do_stuff()
yield
I put a print statement there so I could see it in the console when I'm running the tests, for better visibility into what the program is doing. But I don't see that text in the console, I'm guessing pytest swallows it. Is there any way to print from a fixture?
Allows to print extra content onto the PyTest reporting. This can be used for example to report sub-steps for long running tests, or to print debug information in your tests when you cannot debug the code.
To access the fixture function, the tests have to mention the fixture name as input parameter. Pytest while the test is getting executed, will see the fixture name as input parameter. It then executes the fixture function and the returned value is stored to the input parameter, which can be used by the test.
What Are Pytest Fixtures? Pytest fixtures are functions that can be used to manage our apps states and dependencies. Most importantly, they can provide data for testing and a wide range of value types when explicitly called by our testing software. You can use the mock data that fixtures create across multiple tests.
Pytest doesn't not swallow the output, it's just not shown by default. To see output in console, try running test with -s
option, like:
pytest -s <path_to_file>
with pytest 3 use this fixture:
@pytest.fixture()
def tprint(request, capsys):
"""Fixture for printing info after test, not supressed by pytest stdout/stderr capture"""
lines = []
yield lines.append
with capsys.disabled():
for line in lines:
sys.stdout.write('\n{}'.format(line))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With