I'm using py.test to run tests. I'm using it with pytest-xdist to run the tests in parallel. I want to see the output of print statements in my tests.
I have: Ubuntu 15.10, Python 2.7.10, pytest-2.9.1, pluggy-0.3.1.
Here's my test file:
def test_a(): print 'test_a' def test_b(): print 'test_b'
When I run py.test, nothing is printed. That's expected: by default, py.test captures output.
When I run py.test -s, it prints test_a and test_b, as it should.
When I run py.test -s -n2, again nothing is printed. How can I get the print statements to work while using -n2?
I've already read pytest + xdist without capturing output and this bug report.
If you want to be able to see that output even if the test does not fail, then you'll want to run your pytest test with a special flag. That special flag is the -s flag. Alternatively, you could also use the --show-capture flag, which makes things a little more clear but also is a little bit longer.
The pytest-xdist plugin extends pytest with new test execution modes, the most used being distributing tests across multiple CPUs to speed up test execution: pytest -n auto.
By default, pytest captures the output to sys. stdout/stderr and redirects it to a temporary file.
To overcome this, pytest provides us with an option to run tests in parallel. For this, we need to first install the pytest-xdist plugin. -n <num> runs the tests by using multiple workers, here it is 3. We will not be having much time difference when there is only a few tests to run.
I just see the explain about this issue in github https://github.com/pytest-dev/pytest/issues/1693
pytest-xdist
use sys stdin/stdout to control execution, the only way to show print out should be std.err.
import sys def test_a(): print >> sys.stderr, 'test_a' def test_b(): print >> sys.stderr, 'test_b'
Not a good idea, but work.
Also you should note that the arg -s
has no use if you use xdist plugin.
In python 3, I think logging.warning
is a better choice, since that it is set up to write to stderr by default.
import logging logging.basicConfig(format='%(message)s') def test_a(): logging.warning('test_a') def test_b(): logging.warning('test_b')
to see the log file, create a file called pytest.ini
and add the below to it
log_cli = 1 log_cli_level = WARN log_file = ./TestResults/test_log.log log_file_level = WARN
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