2019-01-10 04:58:51 +08:00
|
|
|
import warnings
|
2017-05-26 13:12:02 +08:00
|
|
|
|
2016-11-21 18:26:43 +08:00
|
|
|
import pytest
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
WARNINGS_SUMMARY_HEADER = "warnings summary"
|
2017-02-25 23:02:48 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-11-21 18:26:43 +08:00
|
|
|
@pytest.fixture
|
2017-03-05 07:53:42 +08:00
|
|
|
def pyfile_with_warnings(testdir, request):
|
|
|
|
"""
|
|
|
|
Create a test file which calls a function in a module which generates warnings.
|
|
|
|
"""
|
|
|
|
testdir.syspathinsert()
|
|
|
|
test_name = request.function.__name__
|
2018-05-23 22:48:46 +08:00
|
|
|
module_name = test_name.lstrip("test_") + "_module"
|
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
|
|
|
module_name: """
|
2017-03-05 07:53:42 +08:00
|
|
|
import warnings
|
|
|
|
def foo():
|
2017-05-30 05:59:34 +08:00
|
|
|
warnings.warn(UserWarning("user warning"))
|
|
|
|
warnings.warn(RuntimeWarning("runtime warning"))
|
2017-03-05 07:53:42 +08:00
|
|
|
return 1
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
test_name: """
|
2017-03-05 07:53:42 +08:00
|
|
|
import {module_name}
|
|
|
|
def test_func():
|
|
|
|
assert {module_name}.foo() == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
module_name=module_name
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2016-11-21 18:26:43 +08:00
|
|
|
|
|
|
|
|
2018-09-02 08:58:48 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2016-11-21 18:26:43 +08:00
|
|
|
def test_normal_flow(testdir, pyfile_with_warnings):
|
2017-03-05 07:53:42 +08:00
|
|
|
"""
|
2018-09-04 07:13:41 +08:00
|
|
|
Check that the warnings section is displayed.
|
2017-03-05 07:53:42 +08:00
|
|
|
"""
|
2017-02-18 22:57:26 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
|
2018-10-25 04:33:50 +08:00
|
|
|
"test_normal_flow.py::test_func",
|
2018-05-23 22:48:46 +08:00
|
|
|
"*normal_flow_module.py:3: UserWarning: user warning",
|
|
|
|
'* warnings.warn(UserWarning("user warning"))',
|
|
|
|
"*normal_flow_module.py:4: RuntimeWarning: runtime warning",
|
|
|
|
'* warnings.warn(RuntimeWarning("runtime warning"))',
|
|
|
|
"* 1 passed, 2 warnings*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.filterwarnings("always")
|
2017-03-05 03:07:37 +08:00
|
|
|
def test_setup_teardown_warnings(testdir, pyfile_with_warnings):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-03-05 03:07:37 +08:00
|
|
|
import warnings
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix():
|
|
|
|
warnings.warn(UserWarning("warning during setup"))
|
|
|
|
yield
|
|
|
|
warnings.warn(UserWarning("warning during teardown"))
|
|
|
|
|
|
|
|
def test_func(fix):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-03-05 03:07:37 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
|
|
|
|
"*test_setup_teardown_warnings.py:6: UserWarning: warning during setup",
|
|
|
|
'*warnings.warn(UserWarning("warning during setup"))',
|
|
|
|
"*test_setup_teardown_warnings.py:8: UserWarning: warning during teardown",
|
|
|
|
'*warnings.warn(UserWarning("warning during teardown"))',
|
|
|
|
"* 1 passed, 2 warnings*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["cmdline", "ini"])
|
2016-11-21 18:26:43 +08:00
|
|
|
def test_as_errors(testdir, pyfile_with_warnings, method):
|
2018-05-23 22:48:46 +08:00
|
|
|
args = ("-W", "error") if method == "cmdline" else ()
|
|
|
|
if method == "ini":
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2016-11-21 18:26:43 +08:00
|
|
|
[pytest]
|
2019-03-24 18:17:55 +08:00
|
|
|
filterwarnings=error
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-03-24 18:17:55 +08:00
|
|
|
# Use a subprocess, since changing logging level affects other threads
|
|
|
|
# (xdist).
|
|
|
|
result = testdir.runpytest_subprocess(*args)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"E UserWarning: user warning",
|
|
|
|
"as_errors_module.py:3: UserWarning",
|
|
|
|
"* 1 failed in *",
|
|
|
|
]
|
|
|
|
)
|
2016-11-21 18:26:43 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("method", ["cmdline", "ini"])
|
2016-11-21 18:26:43 +08:00
|
|
|
def test_ignore(testdir, pyfile_with_warnings, method):
|
2018-05-23 22:48:46 +08:00
|
|
|
args = ("-W", "ignore") if method == "cmdline" else ()
|
|
|
|
if method == "ini":
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2016-11-21 18:26:43 +08:00
|
|
|
[pytest]
|
|
|
|
filterwarnings= ignore
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-11-21 18:26:43 +08:00
|
|
|
|
2017-02-18 22:57:26 +08:00
|
|
|
result = testdir.runpytest(*args)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed in *"])
|
2017-02-25 23:02:48 +08:00
|
|
|
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
|
2016-11-21 18:26:43 +08:00
|
|
|
|
2017-05-25 17:59:42 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.filterwarnings("always")
|
2017-05-25 17:59:42 +08:00
|
|
|
def test_unicode(testdir, pyfile_with_warnings):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
2019-06-03 06:40:34 +08:00
|
|
|
"""\
|
2017-05-25 17:59:42 +08:00
|
|
|
import warnings
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix():
|
2019-06-05 08:48:06 +08:00
|
|
|
warnings.warn("测试")
|
2017-05-25 17:59:42 +08:00
|
|
|
yield
|
|
|
|
|
|
|
|
def test_func(fix):
|
|
|
|
pass
|
2019-06-03 06:40:34 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2017-05-25 17:59:42 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
|
2019-06-03 06:59:44 +08:00
|
|
|
"*test_unicode.py:7: UserWarning: \u6d4b\u8bd5*",
|
2018-05-23 22:48:46 +08:00
|
|
|
"* 1 passed, 1 warnings*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-05-30 05:59:34 +08:00
|
|
|
def test_works_with_filterwarnings(testdir):
|
|
|
|
"""Ensure our warnings capture does not mess with pre-installed filters (#2430)."""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-05-30 05:59:34 +08:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
class MyWarning(Warning):
|
|
|
|
pass
|
2017-07-17 07:25:06 +08:00
|
|
|
|
2017-05-30 05:59:34 +08:00
|
|
|
warnings.filterwarnings("error", category=MyWarning)
|
2017-07-17 07:25:06 +08:00
|
|
|
|
2017-05-30 05:59:34 +08:00
|
|
|
class TestWarnings(object):
|
|
|
|
def test_my_warning(self):
|
|
|
|
try:
|
|
|
|
warnings.warn(MyWarning("warn!"))
|
|
|
|
assert False
|
|
|
|
except MyWarning:
|
|
|
|
assert True
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-05-30 05:59:34 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*== 1 passed in *"])
|
2017-07-21 09:02:21 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("default_config", ["ini", "cmdline"])
|
2017-07-21 09:02:21 +08:00
|
|
|
def test_filterwarnings_mark(testdir, default_config):
|
|
|
|
"""
|
|
|
|
Test ``filterwarnings`` mark works and takes precedence over command line and ini options.
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
if default_config == "ini":
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2017-07-21 09:02:21 +08:00
|
|
|
[pytest]
|
|
|
|
filterwarnings = always
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-07-21 09:02:21 +08:00
|
|
|
import warnings
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.filterwarnings('ignore::RuntimeWarning')
|
|
|
|
def test_ignore_runtime_warning():
|
|
|
|
warnings.warn(RuntimeWarning())
|
|
|
|
|
|
|
|
@pytest.mark.filterwarnings('error')
|
|
|
|
def test_warning_error():
|
|
|
|
warnings.warn(RuntimeWarning())
|
|
|
|
|
|
|
|
def test_show_warning():
|
|
|
|
warnings.warn(RuntimeWarning())
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-W always" if default_config == "cmdline" else "")
|
|
|
|
result.stdout.fnmatch_lines(["*= 1 failed, 2 passed, 1 warnings in *"])
|
2017-11-28 09:07:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_non_string_warning_argument(testdir):
|
|
|
|
"""Non-str argument passed to warning breaks pytest (#2956)"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
2019-06-05 08:48:06 +08:00
|
|
|
"""\
|
2017-11-28 09:07:29 +08:00
|
|
|
import warnings
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test():
|
2019-06-05 08:48:06 +08:00
|
|
|
warnings.warn(UserWarning(1, 'foo'))
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
result = testdir.runpytest("-W", "always")
|
|
|
|
result.stdout.fnmatch_lines(["*= 1 passed, 1 warnings in *"])
|
2018-08-15 05:07:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_filterwarnings_mark_registration(testdir):
|
|
|
|
"""Ensure filterwarnings mark is registered"""
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.filterwarnings('error')
|
2018-08-15 09:29:42 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
2018-08-15 05:07:29 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-04-27 21:52:12 +08:00
|
|
|
result = testdir.runpytest("--strict-markers")
|
2018-08-15 05:07:29 +08:00
|
|
|
assert result.ret == 0
|
2018-08-26 07:41:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.filterwarnings("always")
|
2018-09-05 02:22:07 +08:00
|
|
|
def test_warning_captured_hook(testdir):
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2018-12-12 06:02:36 +08:00
|
|
|
from _pytest.warnings import _issue_warning_captured
|
2018-09-05 02:22:07 +08:00
|
|
|
def pytest_configure(config):
|
2018-12-12 06:02:36 +08:00
|
|
|
_issue_warning_captured(UserWarning("config warning"), config.hook, stacklevel=2)
|
2018-09-05 02:22:07 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest, warnings
|
|
|
|
|
|
|
|
warnings.warn(UserWarning("collect warning"))
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix():
|
|
|
|
warnings.warn(UserWarning("setup warning"))
|
|
|
|
yield 1
|
|
|
|
warnings.warn(UserWarning("teardown warning"))
|
|
|
|
|
|
|
|
def test_func(fix):
|
|
|
|
warnings.warn(UserWarning("call warning"))
|
|
|
|
assert fix == 1
|
|
|
|
"""
|
|
|
|
)
|
2018-08-26 07:41:16 +08:00
|
|
|
|
|
|
|
collected = []
|
|
|
|
|
|
|
|
class WarningCollector:
|
2018-08-26 09:15:22 +08:00
|
|
|
def pytest_warning_captured(self, warning_message, when, item):
|
2018-09-05 02:22:07 +08:00
|
|
|
imge_name = item.name if item is not None else ""
|
|
|
|
collected.append((str(warning_message.message), when, imge_name))
|
2018-08-26 07:41:16 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest(plugins=[WarningCollector()])
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
|
|
|
expected = [
|
2018-09-05 02:22:07 +08:00
|
|
|
("config warning", "config", ""),
|
|
|
|
("collect warning", "collect", ""),
|
2018-09-05 04:01:23 +08:00
|
|
|
("setup warning", "runtest", "test_func"),
|
|
|
|
("call warning", "runtest", "test_func"),
|
|
|
|
("teardown warning", "runtest", "test_func"),
|
2018-08-26 07:41:16 +08:00
|
|
|
]
|
|
|
|
assert collected == expected
|
2018-08-26 22:09:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.filterwarnings("always")
|
|
|
|
def test_collection_warnings(testdir):
|
|
|
|
"""
|
2018-09-04 04:52:44 +08:00
|
|
|
Check that we also capture warnings issued during test collection (#3251).
|
2018-08-26 22:09:54 +08:00
|
|
|
"""
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
warnings.warn(UserWarning("collection warning"))
|
|
|
|
|
|
|
|
def test_foo():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
|
2018-10-25 04:33:50 +08:00
|
|
|
" *collection_warnings.py:3: UserWarning: collection warning",
|
|
|
|
' warnings.warn(UserWarning("collection warning"))',
|
2018-08-26 22:09:54 +08:00
|
|
|
"* 1 passed, 1 warnings*",
|
|
|
|
]
|
|
|
|
)
|
2018-09-04 04:52:44 +08:00
|
|
|
|
|
|
|
|
2018-09-05 21:20:25 +08:00
|
|
|
@pytest.mark.filterwarnings("always")
|
|
|
|
def test_mark_regex_escape(testdir):
|
|
|
|
"""@pytest.mark.filterwarnings should not try to escape regex characters (#3936)"""
|
|
|
|
testdir.makepyfile(
|
|
|
|
r"""
|
|
|
|
import pytest, warnings
|
|
|
|
|
|
|
|
@pytest.mark.filterwarnings(r"ignore:some \(warning\)")
|
|
|
|
def test_foo():
|
|
|
|
warnings.warn(UserWarning("some (warning)"))
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
|
|
|
|
|
|
|
|
|
2018-09-04 04:56:18 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
|
|
|
@pytest.mark.parametrize("ignore_pytest_warnings", ["no", "ini", "cmdline"])
|
|
|
|
def test_hide_pytest_internal_warnings(testdir, ignore_pytest_warnings):
|
|
|
|
"""Make sure we can ignore internal pytest warnings using a warnings filter."""
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
warnings.warn(pytest.PytestWarning("some internal warning"))
|
|
|
|
|
|
|
|
def test_bar():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
if ignore_pytest_warnings == "ini":
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
filterwarnings = ignore::pytest.PytestWarning
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
args = (
|
|
|
|
["-W", "ignore::pytest.PytestWarning"]
|
|
|
|
if ignore_pytest_warnings == "cmdline"
|
|
|
|
else []
|
|
|
|
)
|
|
|
|
result = testdir.runpytest(*args)
|
|
|
|
if ignore_pytest_warnings != "no":
|
|
|
|
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
|
|
|
|
else:
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
|
|
|
|
"*test_hide_pytest_internal_warnings.py:4: PytestWarning: some internal warning",
|
|
|
|
"* 1 passed, 1 warnings *",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-09-07 05:02:36 +08:00
|
|
|
@pytest.mark.parametrize("ignore_on_cmdline", [True, False])
|
|
|
|
def test_option_precedence_cmdline_over_ini(testdir, ignore_on_cmdline):
|
|
|
|
"""filters defined in the command-line should take precedence over filters in ini files (#3946)."""
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
filterwarnings = error
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import warnings
|
|
|
|
def test():
|
|
|
|
warnings.warn(UserWarning('hello'))
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
args = ["-W", "ignore"] if ignore_on_cmdline else []
|
|
|
|
result = testdir.runpytest(*args)
|
|
|
|
if ignore_on_cmdline:
|
|
|
|
result.stdout.fnmatch_lines(["* 1 passed in*"])
|
|
|
|
else:
|
|
|
|
result.stdout.fnmatch_lines(["* 1 failed in*"])
|
|
|
|
|
|
|
|
|
|
|
|
def test_option_precedence_mark(testdir):
|
|
|
|
"""Filters defined by marks should always take precedence (#3946)."""
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
filterwarnings = ignore
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest, warnings
|
|
|
|
@pytest.mark.filterwarnings('error')
|
|
|
|
def test():
|
|
|
|
warnings.warn(UserWarning('hello'))
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-W", "ignore")
|
|
|
|
result.stdout.fnmatch_lines(["* 1 failed in*"])
|
|
|
|
|
|
|
|
|
2018-09-04 04:52:44 +08:00
|
|
|
class TestDeprecationWarningsByDefault:
|
|
|
|
"""
|
|
|
|
Note: all pytest runs are executed in a subprocess so we don't inherit warning filters
|
|
|
|
from pytest's own test suite
|
|
|
|
"""
|
|
|
|
|
|
|
|
def create_file(self, testdir, mark=""):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest, warnings
|
|
|
|
|
|
|
|
warnings.warn(DeprecationWarning("collection"))
|
|
|
|
|
|
|
|
{mark}
|
|
|
|
def test_foo():
|
|
|
|
warnings.warn(PendingDeprecationWarning("test run"))
|
|
|
|
""".format(
|
|
|
|
mark=mark
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-09-22 19:27:53 +08:00
|
|
|
@pytest.mark.parametrize("customize_filters", [True, False])
|
|
|
|
def test_shown_by_default(self, testdir, customize_filters):
|
|
|
|
"""Show deprecation warnings by default, even if user has customized the warnings filters (#4013)."""
|
2018-09-04 04:52:44 +08:00
|
|
|
self.create_file(testdir)
|
2018-09-22 19:27:53 +08:00
|
|
|
if customize_filters:
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
filterwarnings =
|
|
|
|
once::UserWarning
|
|
|
|
"""
|
|
|
|
)
|
2018-09-04 04:52:44 +08:00
|
|
|
result = testdir.runpytest_subprocess()
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
|
|
|
|
"*test_shown_by_default.py:3: DeprecationWarning: collection",
|
|
|
|
"*test_shown_by_default.py:7: PendingDeprecationWarning: test run",
|
|
|
|
"* 1 passed, 2 warnings*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_hidden_by_ini(self, testdir):
|
|
|
|
self.create_file(testdir)
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
2018-09-22 19:27:53 +08:00
|
|
|
filterwarnings =
|
|
|
|
ignore::DeprecationWarning
|
|
|
|
ignore::PendingDeprecationWarning
|
2018-09-04 04:52:44 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest_subprocess()
|
|
|
|
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
|
|
|
|
|
|
|
|
def test_hidden_by_mark(self, testdir):
|
|
|
|
"""Should hide the deprecation warning from the function, but the warning during collection should
|
|
|
|
be displayed normally.
|
|
|
|
"""
|
|
|
|
self.create_file(
|
2018-09-22 19:27:53 +08:00
|
|
|
testdir,
|
|
|
|
mark='@pytest.mark.filterwarnings("ignore::PendingDeprecationWarning")',
|
2018-09-04 04:52:44 +08:00
|
|
|
)
|
|
|
|
result = testdir.runpytest_subprocess()
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*== %s ==*" % WARNINGS_SUMMARY_HEADER,
|
|
|
|
"*test_hidden_by_mark.py:3: DeprecationWarning: collection",
|
|
|
|
"* 1 passed, 1 warnings*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_hidden_by_cmdline(self, testdir):
|
|
|
|
self.create_file(testdir)
|
2018-09-22 19:27:53 +08:00
|
|
|
result = testdir.runpytest_subprocess(
|
|
|
|
"-W",
|
|
|
|
"ignore::DeprecationWarning",
|
|
|
|
"-W",
|
|
|
|
"ignore::PendingDeprecationWarning",
|
|
|
|
)
|
2018-09-04 04:52:44 +08:00
|
|
|
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
|
|
|
|
|
|
|
|
def test_hidden_by_system(self, testdir, monkeypatch):
|
|
|
|
self.create_file(testdir)
|
2019-06-03 06:32:00 +08:00
|
|
|
monkeypatch.setenv("PYTHONWARNINGS", "once::UserWarning")
|
2018-09-04 04:52:44 +08:00
|
|
|
result = testdir.runpytest_subprocess()
|
|
|
|
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
|
2018-10-19 06:42:25 +08:00
|
|
|
|
|
|
|
|
2018-11-09 06:14:58 +08:00
|
|
|
@pytest.mark.parametrize("change_default", [None, "ini", "cmdline"])
|
2019-06-30 23:56:27 +08:00
|
|
|
@pytest.mark.skip(
|
|
|
|
reason="This test should be enabled again before pytest 6.0 is released"
|
|
|
|
)
|
2019-06-06 06:02:52 +08:00
|
|
|
def test_deprecation_warning_as_error(testdir, change_default):
|
2019-06-30 23:56:27 +08:00
|
|
|
"""This ensures that PytestDeprecationWarnings raised by pytest are turned into errors.
|
|
|
|
|
|
|
|
This test should be enabled as part of each major release, and skipped again afterwards
|
|
|
|
to ensure our deprecations are turning into warnings as expected.
|
|
|
|
"""
|
2019-06-06 06:02:52 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import warnings, pytest
|
|
|
|
def test():
|
|
|
|
warnings.warn(pytest.PytestDeprecationWarning("some warning"))
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
if change_default == "ini":
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
filterwarnings =
|
|
|
|
ignore::pytest.PytestDeprecationWarning
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
args = (
|
|
|
|
("-Wignore::pytest.PytestDeprecationWarning",)
|
|
|
|
if change_default == "cmdline"
|
|
|
|
else ()
|
|
|
|
)
|
|
|
|
result = testdir.runpytest(*args)
|
|
|
|
if change_default is None:
|
|
|
|
result.stdout.fnmatch_lines(["* 1 failed in *"])
|
|
|
|
else:
|
|
|
|
assert change_default in ("ini", "cmdline")
|
|
|
|
result.stdout.fnmatch_lines(["* 1 passed in *"])
|
|
|
|
|
|
|
|
|
2018-10-15 00:13:00 +08:00
|
|
|
class TestAssertionWarnings:
|
2018-10-15 15:53:27 +08:00
|
|
|
@staticmethod
|
2018-12-06 02:22:44 +08:00
|
|
|
def assert_result_warns(result, msg):
|
2019-04-28 21:38:25 +08:00
|
|
|
result.stdout.fnmatch_lines(["*PytestAssertRewriteWarning: %s*" % msg])
|
2018-10-15 15:53:27 +08:00
|
|
|
|
2018-10-15 00:13:00 +08:00
|
|
|
def test_tuple_warning(self, testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
def test_foo():
|
|
|
|
assert (1,2)
|
|
|
|
"""
|
|
|
|
)
|
2018-10-15 15:53:27 +08:00
|
|
|
result = testdir.runpytest()
|
2018-12-06 02:22:44 +08:00
|
|
|
self.assert_result_warns(
|
|
|
|
result, "assertion is always true, perhaps remove parentheses?"
|
|
|
|
)
|
2018-10-15 00:13:00 +08:00
|
|
|
|
2018-10-15 15:53:27 +08:00
|
|
|
@staticmethod
|
|
|
|
def create_file(testdir, return_none):
|
2018-10-15 00:13:00 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
def foo(return_none):
|
|
|
|
if return_none:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def test_foo():
|
|
|
|
assert foo({return_none})
|
|
|
|
""".format(
|
|
|
|
return_none=return_none
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_none_function_warns(self, testdir):
|
|
|
|
self.create_file(testdir, True)
|
2018-10-15 15:53:27 +08:00
|
|
|
result = testdir.runpytest()
|
2018-12-06 02:22:44 +08:00
|
|
|
self.assert_result_warns(
|
|
|
|
result, 'asserting the value None, please use "assert is None"'
|
|
|
|
)
|
2018-10-15 15:53:27 +08:00
|
|
|
|
|
|
|
def test_assert_is_none_no_warn(self, testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2018-10-25 18:47:30 +08:00
|
|
|
def foo():
|
|
|
|
return None
|
2018-10-15 15:53:27 +08:00
|
|
|
|
|
|
|
def test_foo():
|
2018-10-25 18:47:30 +08:00
|
|
|
assert foo() is None
|
2018-10-15 15:53:27 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
2018-11-05 16:13:37 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed in*"])
|
2018-10-15 00:13:00 +08:00
|
|
|
|
|
|
|
def test_false_function_no_warn(self, testdir):
|
|
|
|
self.create_file(testdir, False)
|
2018-10-15 15:53:27 +08:00
|
|
|
result = testdir.runpytest()
|
2018-11-05 16:13:37 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 failed in*"])
|
2019-01-09 19:31:26 +08:00
|
|
|
|
|
|
|
|
2019-01-10 04:58:51 +08:00
|
|
|
def test_warnings_checker_twice():
|
2019-01-09 19:31:26 +08:00
|
|
|
"""Issue #4617"""
|
2019-01-10 04:58:51 +08:00
|
|
|
expectation = pytest.warns(UserWarning)
|
|
|
|
with expectation:
|
|
|
|
warnings.warn("Message A", UserWarning)
|
|
|
|
with expectation:
|
|
|
|
warnings.warn("Message B", UserWarning)
|
2019-01-23 05:42:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.filterwarnings("always")
|
|
|
|
def test_group_warnings_by_message(testdir):
|
|
|
|
testdir.copy_example("warnings/test_group_warnings_by_message.py")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"test_group_warnings_by_message.py::test_foo[0]",
|
|
|
|
"test_group_warnings_by_message.py::test_foo[1]",
|
|
|
|
"test_group_warnings_by_message.py::test_foo[2]",
|
|
|
|
"test_group_warnings_by_message.py::test_foo[3]",
|
|
|
|
"test_group_warnings_by_message.py::test_foo[4]",
|
|
|
|
"test_group_warnings_by_message.py::test_bar",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
warning_code = 'warnings.warn(UserWarning("foo"))'
|
|
|
|
assert warning_code in result.stdout.str()
|
|
|
|
assert result.stdout.str().count(warning_code) == 1
|
2019-08-12 13:47:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_pytest_configure_warning(testdir, recwarn):
|
|
|
|
"""Issue 5115."""
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
|
|
|
def pytest_configure():
|
|
|
|
import warnings
|
|
|
|
|
|
|
|
warnings.warn("from pytest_configure")
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 5
|
|
|
|
assert "INTERNALERROR" not in result.stderr.str()
|
|
|
|
warning = recwarn.pop()
|
|
|
|
assert str(warning.message) == "from pytest_configure"
|