Skip to content

Make warnings plugin opt-in #2443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion _pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if done like that, we will have to do a 4.0 release

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the most apparent blunder being the addition of the once filter

Not really, unfortunately catch_warnings will remove all current filters in order to be able to catch all warnings. That filter is there just to prevent the same warning to be triggered more than once by the same test.

How would you like to make this change without making a 4.0 release? Or do you want to make a 4.0 release regardless?



builtin_plugins = set(default_plugins)
Expand Down
13 changes: 6 additions & 7 deletions doc/en/warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://github.com/pytest-dev/pytest/issues/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
Expand Down
19 changes: 19 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down