avasin
avasin

Reputation: 9726

Is it possible to run pytest totally silent?

I have simple test:

def test_noindex(data):
    assert 0

I need to handle this test by py.test, but totally silent (as i see py.test has exit code 1 for failed tests).

I tried:

py.test -s test.py
py.test -qq test.py
py.test --capture=no test.py

result is always the same: i see full report. What i am doing wrong?

P.S. Running on windows, executing pytest using pytest.main([params]) in python code

Upvotes: 5

Views: 2075

Answers (2)

rr-
rr-

Reputation: 14811

@smassey's answer works in most cases, but since you execute pytest from Python itself, you need to approach things differently.

For example, have your python's stdout and stderr redirect to nothing while you run your tests. This can be accomplished with following code:

#!/usr/bin/python
import os
import sys

old_out = sys.stdout
old_err = sys.stderr
sys.stdout = open(os.devnull, 'w')
sys.stderr = open(os.devnull, 'w')

print 'suppressed'

sys.stdout = old_out
sys.stderr = old_err

print 'restored'

open(os.devnull) opens your operating system's equivalent of /dev/null for writing. Having it redirect simply to None will not work, because sys.stdout and sys.stderr need to expose certain methods so that print won't throw exceptions.

There is also question: why would you want to do that?

Upvotes: 3

smassey
smassey

Reputation: 5931

Why not simply redirect all output, like so:

py.test test.py 2>&1 >/dev/null

This works for any output (stderr, stdout) of any cli app.

Upvotes: 4

Related Questions