pytest-dev / pytest-dev/pytest

`pytest -Werror` fails with ignore warnings in pytest.ini

Open
#8,534 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: warnings
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Hi, first of all, thanks for the wonderful library!!

My code raises UserWarnings and I want to ignore them when running python -Werror .. I've been following this doc.

Minimal repro:
# test_warning.py
from pytest import mark

import warnings

def test_warnings():
    warnings.warn(category=UserWarning, message=f"user warning")
# pytest.ini
[pytest]
filterwarnings =
    error
    ignore::UserWarning

Run:

$ pytest -Werror .
================================================================================================================================ test session starts ================================================================================================================================
platform darwin -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: test_warnings, configfile: pytest.ini
plugins: snail-0.1.0, typeguard-2.11.1, hydra-core-1.1.0.dev6
collected 1 item                                                                                                                                                                                                                                                                    

test_warnings.py F                                                                                                                                                                                                                                                            [100%]

===================================================================================================================================== FAILURES ======================================================================================================================================
___________________________________________________________________________________________________________________________________ test_warnings ___________________________________________________________________________________________________________________________________

    def test_warnings():
>       warnings.warn(category=UserWarning, message=f"user warning")
E       UserWarning: user warning

test_warnings.py:8: UserWarning
============================================================================================================================== short test summary info ==============================================================================================================================
FAILED test_warnings.py::test_warnings - UserWarning: user warning
================================================================================================================================= 1 failed in 0.24s =================================================================================================================================
(pytest38) jieru-mbp:test_warnings jieru$ 

If instead I decorate the test with @mark.filterwarnings("ignore::UserWarning"), pytest -Werror runs with no issue.
I have many tests that need the warning filter, so adding decorate to each of them is really not ideal. Is it possible to control the filtering with pytest.ini?

pip list
$ pip list
Package                         Version             Location
------------------------------- ------------------- --------------------------------------------------------------------------------
absl-py                         0.12.0
aiohttp                         3.7.3
aiohttp-cors                    0.7.0
aioredis                        1.3.1
alembic                         1.5.4
antlr4-python3-runtime          4.8
appdirs                         1.4.3
argcomplete                     1.11.1
async-timeout                   3.0.1
attrs                           20.3.0
ax-platform                     0.1.20
bandit                          1.7.0
black                           20.8b1
bleach                          3.2.1
blessings                       1.7
boto3                           1.17.17
botocore                        1.20.17
botorch                         0.4.0
cachetools                      4.2.0
certifi                         2020.12.5
cfgv                            3.2.0
chardet                         3.0.4
click                           7.1.2
cliff                           3.7.0
cloudpickle                     1.6.0
cmaes                           0.8.1
cmd2                            1.5.0
colorama                        0.4.4
colorful                        0.5.4
colorlog                        4.1.0
coverage                        5.3.1
distlib                         0.3.0
docutils                        0.16
filelock                        3.0.12
flake8                          3.8.4
flake8-copyright                0.2.2
fsspec                          0.8.7
future                          0.18.2
gitdb                           4.0.5
GitPython                       3.1.12
google-api-core                 1.25.0
google-auth                     1.24.0
google-auth-oauthlib            0.4.3
googleapis-common-protos        1.52.0
gpustat                         0.6.0
gpytorch                        1.4.0
grpcio                          1.34.1
hiredis                         1.1.0
identify                        1.5.13
idna                            2.10
importlib-resources             5.1.0
incremental                     17.5.0
iniconfig                       1.1.1
isort                           5.5.2
Jinja2                          2.11.2
jmespath                        0.10.0
joblib                          1.0.0
jsonschema                      3.2.0
keyring                         21.8.0
Mako                            1.1.4
Markdown                        3.3.4
MarkupSafe                      1.1.1
mccabe                          0.6.1
mkl-fft                         1.3.0
mkl-random                      1.1.1
mkl-service                     2.3.0
msgpack                         1.0.2
multidict                       5.1.0
mypy                            0.790
mypy-extensions                 0.4.3
nodeenv                         1.5.0
nox                             2020.12.31
numpy                           1.19.5
nvidia-ml-py3                   7.352.0
oauthlib                        3.1.0
olefile                         0.46
omegaconf                       2.1.0.dev24
opencensus                      0.7.12
opencensus-context              0.1.2
optuna                          2.4.0
packaging                       20.8
pandas                          1.2.2
pathspec                        0.8.1
pbr                             5.5.1
pickle5                         0.0.11
Pillow                          8.1.0
pip                             21.0.1
pkginfo                         1.7.0
plotly                          4.14.3
pluggy                          0.13.1
pre-commit                      2.9.3
prettytable                     2.0.0
prometheus-client               0.9.0
protobuf                        3.14.0
psutil                          5.8.0
py                              1.10.0
py-spy                          0.3.4
pyasn1                          0.4.8
pyasn1-modules                  0.2.8
pycodestyle                     2.6.0
pyflakes                        2.2.0
Pygments                        2.7.4
pyparsing                       2.4.7
pyperclip                       1.8.1
pyrsistent                      0.17.3
pytest                          6.2.3
pytest-snail                    0.1.0
python-dateutil                 2.8.1
python-editor                   1.0.4
pytorch-lightning               1.2.3
pytz                            2020.5
PyYAML                          5.3.1
ray                             1.2.0
readme-renderer                 28.0
redis                           3.5.3
regex                           2020.11.13
requests                        2.25.1
requests-oauthlib               1.3.0
requests-toolbelt               0.9.1
retrying                        1.3.3
rfc3986                         1.4.0
rsa                             4.7
s3transfer                      0.3.4
scikit-learn                    0.24.1
scipy                           1.6.0
setuptools                      52.0.0.post20210125
six                             1.14.0
smmap                           3.0.4
SQLAlchemy                      1.3.23
stevedore                       3.3.0
submitit                        1.1.5
tensorboard                     2.4.1
tensorboard-plugin-wit          1.8.0
threadpoolctl                   2.1.0
toml                            0.10.2
torch                           1.7.1
torchaudio                      0.7.0a0+a853dff
torchvision                     0.8.2
towncrier                       19.2.0
tqdm                            4.56.0
twine                           3.3.0
typed-ast                       1.4.2
typeguard                       2.11.1
typing-extensions               3.7.4.3
urllib3                         1.25.11
virtualenv                      20.0.15
wcwidth                         0.2.5
webencodings                    0.5.1
Werkzeug                        1.0.1
wheel                           0.36.2
yamllint                        1.25.0
yarl                            1.6.3

pytest and operating system versions

versions are in the example output, I'm on MAC OS catalina 10.15

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal test_warning.py and pytest.ini examples, then read the warnings configuration documentation and the code paths handling pytest.ini filters and the -Werror command-line option. Done means the documented ignore::UserWarning filter in pytest.ini takes effect when running pytest -Werror, with a regression test covering the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.