JavaSa
JavaSa

Reputation: 6241

How can we simply surpress log error messages in pytest

Consider you have a unit test written in pytest,
in which you check a state of some exception raised and caught within a function.

When an exception is caught, and error log message is written, which is fine - but less for the context of tests running, as it not clean and may cause confusion when tests are passing but an error log message appears.

Is there a simple way to suppress error messages from pytest perspective?

Upvotes: 2

Views: 2036

Answers (2)

Shivam Bharadwaj
Shivam Bharadwaj

Reputation: 2296

Run the test using the command :

python -m pytest test_pytest_summary.py --tb=short

where, -- tb=short is the flag for shorter traceback format.

OR

You can put -- tb=short in pytest.ini file :

[pytest]
addopts = -vs -ra --tb=short -p no:cacheprovider

Upvotes: 2

damon
damon

Reputation: 15128

Logging in pytest is captured by default -- meaning, the logs are not output to the log handlers when pytest is running.

This can be configured using setting log_cli option. To prevent logs from being output to the console, try setting log_cli to False in your pytest configuration file.

For example, if you're using pytest.ini:

[pytest]
log_cli = False

You can further disable the display of stdout, stderr, and logs on failed tests completely by using the --show-capture=no option when you run pytest on the command line or by adding it to your pytest.ini file:

[pytest]
addopts=--show-capture=no

Upvotes: 2

Related Questions