Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyTest Suppress Results Debug Statement

Tags:

pytest

I am using PyTest with the following options: -s, -v, and --resultlog=results.txt. This suppresses print statements from my test, but prints the test names and results as they are run and logs the results to results.txt.

However, if any tests fail, I also get a spew of information containing traceback, debug, etc. Since I am logging this to a file anyway, I don't want it printed to the screen, cluttering up my output.

Is there any way to disable the printing of just these debug statements, but still have it logged to my results file?

Visual example:

Currently, I see something like this:

$ py.test -sv --resultlog=results.txt test.py
=============================== test session starts =========================
platform darwin -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 -- /...
cachedir: .cache
rootdir: /Users/jdinkel/Documents, inifile:
plugins: profiling-1.1.1, session2file-0.1.9
collected 3 items

test.py::TestClass::test1 PASSED
test.py::TestClass::test2 PASSED
test.py::TestClass::test3 FAILED

===================================== FAILURES ==============================
__________________________________ TestClass.test3 __________________________
self = <test.TestClass instance at 0x10beb5320>

    def test3(self):
>     assert 0
E     assert 0

test.py:7: AssertionError
========================== 1 failed, 2 passed in 0.01 seconds ===============

But I would like to see this:

$ py.test -sv --resultlog=results.txt test.py
=============================== test session starts =========================
platform darwin -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 -- /...
cachedir: .cache
rootdir: /Users/jdinkel/Documents, inifile:
plugins: profiling-1.1.1, session2file-0.1.9
collected 3 items

test.py::TestClass::test1 PASSED
test.py::TestClass::test2 PASSED
test.py::TestClass::test3 FAILED

========================== 1 failed, 2 passed in 0.01 seconds ===============

With no change to the results.txt file.

like image 627
Jack Dinkel Avatar asked Jun 30 '16 20:06

Jack Dinkel


People also ask

How do I ignore warnings in pytest?

How do I stop deprecation warning in Python? Use warnings. filterwarnings() to ignore deprecation warnings Call warnings. filterwarnings(action, category=DeprecationWarning) with action as "ignore" and category set to DeprecationWarning to ignore any deprecation warnings that may rise.

How do you set a breakpoint in pytest?

To set a breakpoint in your code use the native Python import pdb;pdb. set_trace() call in your code and pytest automatically disables its output capture for that test: Output capture in other tests is not affected. Any prior test output that has already been captured and will be processed as such.

What is verbose in pytest?

Verbosity. The -v flag controls the verbosity of pytest output in various aspects: test session progress, assertion details when tests fail, fixtures details with --fixtures , etc.


2 Answers

You should use tb switch for controlling traceback.

e.g. pytest tests/ -sv --tb=no --disable-warnings

--disable-warnings disable occasional pytest warnings which I assume you don't want either.

From pytest help:

--tb=style traceback print mode (auto/long/short/line/native/no).

like image 172
SilentGuy Avatar answered Nov 12 '22 08:11

SilentGuy


In addition to the answer of @SilentGuy, -r N suppresses the summary of failed testcases.

like image 1
Andreas Maier Avatar answered Nov 12 '22 08:11

Andreas Maier