From 4b858ab716373e8c676f549038e2612671b85731 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 29 May 2017 12:49:23 -0300 Subject: [PATCH] Make warnings plugin opt-in Fix #2430 --- CHANGELOG.rst | 24 +++++++++++++++++++++++- _pytest/config.py | 2 +- doc/en/warnings.rst | 13 ++++++------- testing/test_warnings.py | 19 +++++++++++++++++++ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e6a6bd9b912..3beb2182008 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,28 @@ -3.1.1 (unreleased) +3.2.0 (unreleased) ================== + +Changes +------- + +* The warnings capture plugin introduced in 3.1 in now **opt-in**: users must explicitly enable it by adding this to their + ``pytest.ini`` file: + + .. code-block:: ini + + [pytest] + addopts = -p _pytest.warnings + + This change was made because it being automatically active has broken some test suites and the team reconsidered + it being active by default (See `#2430`_ for a complete overview of the discussion). + + +.. _#2430: https://github.com/pytest-dev/pytest/issues/2430 + + +Bug Fixes +--------- + * Fix encoding errors for unicode warnings in Python 2. (towncrier: 2436.bugfix) * Fix issue with non-ascii contents in doctest text files. (towncrier: 2434.bugfix) diff --git a/_pytest/config.py b/_pytest/config.py index c687e3df99c..b475c61cc8f 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -99,7 +99,7 @@ def directory_arg(path, optname): "mark main terminal runner python fixtures debugging unittest capture skipping " "tmpdir monkeypatch recwarn pastebin helpconfig nose assertion " "junitxml resultlog doctest cacheprovider freeze_support " - "setuponly setupplan warnings").split() + "setuponly setupplan").split() builtin_plugins = set(default_plugins) diff --git a/doc/en/warnings.rst b/doc/en/warnings.rst index 20ea00a6505..f027fe8d067 100644 --- a/doc/en/warnings.rst +++ b/doc/en/warnings.rst @@ -4,22 +4,21 @@ Warnings Capture ================ .. versionadded:: 3.1 +.. versionchanged:: 3.2 + This plugin must now be explicitly enabled in your ``pytest.ini`` file or in the command line. .. warning:: pytest captures all warnings between tests, which prevents custom warning - filters in existing test suites from working. If this causes problems to your test suite, - this plugin can be disabled in your ``pytest.ini`` file with: + filters in existing test suites from working. For this reason starting in pytest 3.2 this plugin + must be explicitly enabled in your ``pytest.ini`` file with: .. code-block:: ini [pytest] - addopts = -p no:warnings + addopts = -p _pytest.warnings - There's an ongoing discussion about this on `#2430 - `_. - -Starting from version ``3.1``, pytest now automatically catches all warnings during test execution +This plugin makes pytest automatically catch all warnings during test execution and displays them at the end of the session:: # content of test_show_warnings.py diff --git a/testing/test_warnings.py b/testing/test_warnings.py index df9c4cdc4e4..eb5746ca89f 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -8,6 +8,15 @@ WARNINGS_SUMMARY_HEADER = 'warnings summary' + +@pytest.fixture(autouse=True) +def enable_warnings_plugin(monkeypatch): + """ + Auto-use fixture for this module which enables the warnings plugin, because it is opt-in. + """ + monkeypatch.setenv('PYTEST_ADDOPTS', '-p _pytest.warnings') + + @pytest.fixture def pyfile_with_warnings(testdir, request): """ @@ -53,6 +62,16 @@ def test_normal_flow(testdir, pyfile_with_warnings): assert result.stdout.str().count('test_normal_flow.py::test_func') == 1 +def test_plugin_is_opt_in(testdir, pyfile_with_warnings, monkeypatch): + """ + Ensure warnings plugin is opt-in (#2430). + """ + monkeypatch.delenv('PYTEST_ADDOPTS') + result = testdir.runpytest() + assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() + assert 'DeprecationWarning' not in result.stdout.str() + + def test_setup_teardown_warnings(testdir, pyfile_with_warnings): testdir.makepyfile(''' import warnings