Disable printing of log output on console using pytest

7.1k Views Asked by At

I am writing tests using pytest and if my validation fails it generates a lot of logs which is needed. I am using the python's standard logging module to log these errors to a file using FileHandler class. I am using pytest-html to generate the html report. Everything is working as expected except that pytest is logging the log messages into the terminal also which is taking more time than actual test execution. (Imagine logging around 200k log messages on the terminal).

Is there any option in pytest that would disable logging any logs into the terminal but still capture and make those logs available in the html report. It would still be desirable to log the exceptions or the assertion errors if possible.

1

There are 1 best solutions below

1
On BEST ANSWER

You might be able to configure the output using the --show-capture option.

From pytest --help:

--show-capture={no,stdout,stderr,log,all}
  Controls how captured stdout/stderr/log is shown on failed tests. Default is 'all'.

If you want a finer-grained control of what you filter out, you might need to look into the logging configuration itself (change the captured log level, etc.)