pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.17k stars 2.7k forks source link

Possible missing warning filters in pytest_configure #10128

Open akhilramkee opened 2 years ago

akhilramkee commented 2 years ago

Is there a reason why we don't apply any other warning filters passed in pytest.ini. This seems like a possible bug,

https://github.com/pytest-dev/pytest/blob/c2b1d5b368a1fc829c6c06c700ae2ba9cda55794/src/_pytest/config/__init__.py#L1010

Output

user@system: ~/workspace/pytest-issue-reproducer/filter_config_warning$ pytest
/u/user/workspace/pytest-issue-reproducer/filter_config_warning/conftest.py:5: UserWarning: Some warning
  warnings.warn("Some warning", category=UserWarning)
================================================================================================================================== test session starts ===================================================================================================================================
platform linux -- Python 3.9.7, pytest-7.0.1, pluggy-1.0.0
benchmark: 3.4.1 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /u/user/workspace/pytest-issue-reproducer/filter_config_warning, configfile: pytest.ini
plugins: html-3.1.1, typeguard-2.12.1, json-report-1.4.1, tavern-1.20.0, flaky-3.7.0, allure-pytest-2.9.45, Faker-10.0.0, mock-3.6.1, cov-3.0.0, pyfakefs-4.5.3, xdist-2.5.0, json-0.4.0, benchmark-3.4.1, memray-1.2.0, anyio-3.4.0, subtests-0.8.0, nbval-0.9.6, hypothesis-6.32.1, forked-1.4.0, metadata-1.11.0
collected 1 item

tests/test_dummy.py .                                                                                                                                                                                                                                                              [100%]

=================================================================================================================================== 1 passed in 0.35s ====================================================================================================================================

Warning is thrown before test session starts

/u/user/workspace/pytest-issue-reproducer/filter_config_warning/conftest.py:5: UserWarning: Some warning
  warnings.warn("Some warning", category=UserWarning)

Reproducer

conftest.py

import pytest
import warnings

def pytest_configure(config: pytest.Config):
    warnings.warn("Some warning", category=UserWarning)

Content -> pytest.ini

[pytest]
filterwarnings =
        ignore::UserWarning

Warning gets filtered out in testcases as expected.

Possible fix:

https://github.com/pytest-dev/pytest/blob/e54c6a1362589b32a2e63bb780192b86216ecec8/src/_pytest/config/__init__.py#L1709

akhilramkee commented 2 years ago

@nicoddemus , any views on this ?

nicoddemus commented 2 years ago

Hey @akhilramkee, sorry for not answering earlier.

I think this is mostly an oversight, can't think of a reason not to apply the filters at that stage.

akhilramkee commented 2 years ago

Thanks for replying in short notice! Would it be fine if I send in a PR to fix this?

Zac-HD commented 9 months ago

Hey @akhilramkee - sorry about the long delay! PRs always welcome 🙂