migrates test_warnings.py from testdir to pytester

This commit is contained in:
bot2x 2020-12-06 14:50:33 +05:30 committed by Ran Benita
parent 37b154b1ec
commit 28588bf535
1 changed files with 114 additions and 110 deletions

View File

@ -6,18 +6,18 @@ from typing import Tuple
import pytest import pytest
from _pytest.fixtures import FixtureRequest from _pytest.fixtures import FixtureRequest
from _pytest.pytester import Testdir from _pytest.pytester import Pytester
WARNINGS_SUMMARY_HEADER = "warnings summary" WARNINGS_SUMMARY_HEADER = "warnings summary"
@pytest.fixture @pytest.fixture
def pyfile_with_warnings(testdir: Testdir, request: FixtureRequest) -> str: def pyfile_with_warnings(pytester: Pytester, request: FixtureRequest) -> str:
"""Create a test file which calls a function in a module which generates warnings.""" """Create a test file which calls a function in a module which generates warnings."""
testdir.syspathinsert() pytester.syspathinsert()
test_name = request.function.__name__ test_name = request.function.__name__
module_name = test_name.lstrip("test_") + "_module" module_name = test_name.lstrip("test_") + "_module"
test_file = testdir.makepyfile( test_file = pytester.makepyfile(
""" """
import {module_name} import {module_name}
def test_func(): def test_func():
@ -39,9 +39,9 @@ def pyfile_with_warnings(testdir: Testdir, request: FixtureRequest) -> str:
@pytest.mark.filterwarnings("default") @pytest.mark.filterwarnings("default")
def test_normal_flow(testdir, pyfile_with_warnings): def test_normal_flow(pytester: Pytester, pyfile_with_warnings) -> None:
"""Check that the warnings section is displayed.""" """Check that the warnings section is displayed."""
result = testdir.runpytest(pyfile_with_warnings) result = pytester.runpytest(pyfile_with_warnings)
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*== %s ==*" % WARNINGS_SUMMARY_HEADER,
@ -56,8 +56,8 @@ def test_normal_flow(testdir, pyfile_with_warnings):
@pytest.mark.filterwarnings("always") @pytest.mark.filterwarnings("always")
def test_setup_teardown_warnings(testdir): def test_setup_teardown_warnings(pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import warnings import warnings
import pytest import pytest
@ -72,7 +72,7 @@ def test_setup_teardown_warnings(testdir):
pass pass
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*== %s ==*" % WARNINGS_SUMMARY_HEADER,
@ -86,10 +86,10 @@ def test_setup_teardown_warnings(testdir):
@pytest.mark.parametrize("method", ["cmdline", "ini"]) @pytest.mark.parametrize("method", ["cmdline", "ini"])
def test_as_errors(testdir, pyfile_with_warnings, method): def test_as_errors(pytester: Pytester, pyfile_with_warnings, method) -> None:
args = ("-W", "error") if method == "cmdline" else () args = ("-W", "error") if method == "cmdline" else ()
if method == "ini": if method == "ini":
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
filterwarnings=error filterwarnings=error
@ -97,7 +97,7 @@ def test_as_errors(testdir, pyfile_with_warnings, method):
) )
# Use a subprocess, since changing logging level affects other threads # Use a subprocess, since changing logging level affects other threads
# (xdist). # (xdist).
result = testdir.runpytest_subprocess(*args, pyfile_with_warnings) result = pytester.runpytest_subprocess(*args, pyfile_with_warnings)
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"E UserWarning: user warning", "E UserWarning: user warning",
@ -108,24 +108,24 @@ def test_as_errors(testdir, pyfile_with_warnings, method):
@pytest.mark.parametrize("method", ["cmdline", "ini"]) @pytest.mark.parametrize("method", ["cmdline", "ini"])
def test_ignore(testdir, pyfile_with_warnings, method): def test_ignore(pytester: Pytester, pyfile_with_warnings, method) -> None:
args = ("-W", "ignore") if method == "cmdline" else () args = ("-W", "ignore") if method == "cmdline" else ()
if method == "ini": if method == "ini":
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
filterwarnings= ignore filterwarnings= ignore
""" """
) )
result = testdir.runpytest(*args, pyfile_with_warnings) result = pytester.runpytest(*args, pyfile_with_warnings)
result.stdout.fnmatch_lines(["* 1 passed in *"]) result.stdout.fnmatch_lines(["* 1 passed in *"])
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
@pytest.mark.filterwarnings("always") @pytest.mark.filterwarnings("always")
def test_unicode(testdir): def test_unicode(pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import warnings import warnings
import pytest import pytest
@ -140,7 +140,7 @@ def test_unicode(testdir):
pass pass
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*== %s ==*" % WARNINGS_SUMMARY_HEADER,
@ -150,9 +150,9 @@ def test_unicode(testdir):
) )
def test_works_with_filterwarnings(testdir): def test_works_with_filterwarnings(pytester: Pytester) -> None:
"""Ensure our warnings capture does not mess with pre-installed filters (#2430).""" """Ensure our warnings capture does not mess with pre-installed filters (#2430)."""
testdir.makepyfile( pytester.makepyfile(
""" """
import warnings import warnings
@ -170,22 +170,22 @@ def test_works_with_filterwarnings(testdir):
assert True assert True
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["*== 1 passed in *"]) result.stdout.fnmatch_lines(["*== 1 passed in *"])
@pytest.mark.parametrize("default_config", ["ini", "cmdline"]) @pytest.mark.parametrize("default_config", ["ini", "cmdline"])
def test_filterwarnings_mark(testdir, default_config): def test_filterwarnings_mark(pytester: Pytester, default_config) -> None:
"""Test ``filterwarnings`` mark works and takes precedence over command """Test ``filterwarnings`` mark works and takes precedence over command
line and ini options.""" line and ini options."""
if default_config == "ini": if default_config == "ini":
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
filterwarnings = always filterwarnings = always
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
import warnings import warnings
import pytest import pytest
@ -202,13 +202,13 @@ def test_filterwarnings_mark(testdir, default_config):
warnings.warn(RuntimeWarning()) warnings.warn(RuntimeWarning())
""" """
) )
result = testdir.runpytest("-W always" if default_config == "cmdline" else "") result = pytester.runpytest("-W always" if default_config == "cmdline" else "")
result.stdout.fnmatch_lines(["*= 1 failed, 2 passed, 1 warning in *"]) result.stdout.fnmatch_lines(["*= 1 failed, 2 passed, 1 warning in *"])
def test_non_string_warning_argument(testdir): def test_non_string_warning_argument(pytester: Pytester) -> None:
"""Non-str argument passed to warning breaks pytest (#2956)""" """Non-str argument passed to warning breaks pytest (#2956)"""
testdir.makepyfile( pytester.makepyfile(
"""\ """\
import warnings import warnings
import pytest import pytest
@ -217,13 +217,13 @@ def test_non_string_warning_argument(testdir):
warnings.warn(UserWarning(1, 'foo')) warnings.warn(UserWarning(1, 'foo'))
""" """
) )
result = testdir.runpytest("-W", "always") result = pytester.runpytest("-W", "always")
result.stdout.fnmatch_lines(["*= 1 passed, 1 warning in *"]) result.stdout.fnmatch_lines(["*= 1 passed, 1 warning in *"])
def test_filterwarnings_mark_registration(testdir): def test_filterwarnings_mark_registration(pytester: Pytester) -> None:
"""Ensure filterwarnings mark is registered""" """Ensure filterwarnings mark is registered"""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -232,19 +232,19 @@ def test_filterwarnings_mark_registration(testdir):
pass pass
""" """
) )
result = testdir.runpytest("--strict-markers") result = pytester.runpytest("--strict-markers")
assert result.ret == 0 assert result.ret == 0
@pytest.mark.filterwarnings("always") @pytest.mark.filterwarnings("always")
def test_warning_captured_hook(testdir): def test_warning_captured_hook(pytester: Pytester) -> None:
testdir.makeconftest( pytester.makeconftest(
""" """
def pytest_configure(config): def pytest_configure(config):
config.issue_config_time_warning(UserWarning("config warning"), stacklevel=2) config.issue_config_time_warning(UserWarning("config warning"), stacklevel=2)
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest, warnings import pytest, warnings
@ -268,7 +268,7 @@ def test_warning_captured_hook(testdir):
def pytest_warning_recorded(self, warning_message, when, nodeid, location): def pytest_warning_recorded(self, warning_message, when, nodeid, location):
collected.append((str(warning_message.message), when, nodeid, location)) collected.append((str(warning_message.message), when, nodeid, location))
result = testdir.runpytest(plugins=[WarningCollector()]) result = pytester.runpytest(plugins=[WarningCollector()])
result.stdout.fnmatch_lines(["*1 passed*"]) result.stdout.fnmatch_lines(["*1 passed*"])
expected = [ expected = [
@ -298,9 +298,9 @@ def test_warning_captured_hook(testdir):
@pytest.mark.filterwarnings("always") @pytest.mark.filterwarnings("always")
def test_collection_warnings(testdir): def test_collection_warnings(pytester: Pytester) -> None:
"""Check that we also capture warnings issued during test collection (#3251).""" """Check that we also capture warnings issued during test collection (#3251)."""
testdir.makepyfile( pytester.makepyfile(
""" """
import warnings import warnings
@ -310,7 +310,7 @@ def test_collection_warnings(testdir):
pass pass
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*== %s ==*" % WARNINGS_SUMMARY_HEADER,
@ -322,9 +322,9 @@ def test_collection_warnings(testdir):
@pytest.mark.filterwarnings("always") @pytest.mark.filterwarnings("always")
def test_mark_regex_escape(testdir): def test_mark_regex_escape(pytester: Pytester) -> None:
"""@pytest.mark.filterwarnings should not try to escape regex characters (#3936)""" """@pytest.mark.filterwarnings should not try to escape regex characters (#3936)"""
testdir.makepyfile( pytester.makepyfile(
r""" r"""
import pytest, warnings import pytest, warnings
@ -333,15 +333,17 @@ def test_mark_regex_escape(testdir):
warnings.warn(UserWarning("some (warning)")) warnings.warn(UserWarning("some (warning)"))
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
@pytest.mark.filterwarnings("default") @pytest.mark.filterwarnings("default")
@pytest.mark.parametrize("ignore_pytest_warnings", ["no", "ini", "cmdline"]) @pytest.mark.parametrize("ignore_pytest_warnings", ["no", "ini", "cmdline"])
def test_hide_pytest_internal_warnings(testdir, ignore_pytest_warnings): def test_hide_pytest_internal_warnings(
pytester: Pytester, ignore_pytest_warnings
) -> None:
"""Make sure we can ignore internal pytest warnings using a warnings filter.""" """Make sure we can ignore internal pytest warnings using a warnings filter."""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
import warnings import warnings
@ -353,7 +355,7 @@ def test_hide_pytest_internal_warnings(testdir, ignore_pytest_warnings):
""" """
) )
if ignore_pytest_warnings == "ini": if ignore_pytest_warnings == "ini":
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
filterwarnings = ignore::pytest.PytestWarning filterwarnings = ignore::pytest.PytestWarning
@ -364,7 +366,7 @@ def test_hide_pytest_internal_warnings(testdir, ignore_pytest_warnings):
if ignore_pytest_warnings == "cmdline" if ignore_pytest_warnings == "cmdline"
else [] else []
) )
result = testdir.runpytest(*args) result = pytester.runpytest(*args)
if ignore_pytest_warnings != "no": if ignore_pytest_warnings != "no":
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
else: else:
@ -378,15 +380,17 @@ def test_hide_pytest_internal_warnings(testdir, ignore_pytest_warnings):
@pytest.mark.parametrize("ignore_on_cmdline", [True, False]) @pytest.mark.parametrize("ignore_on_cmdline", [True, False])
def test_option_precedence_cmdline_over_ini(testdir, ignore_on_cmdline): def test_option_precedence_cmdline_over_ini(
pytester: Pytester, ignore_on_cmdline
) -> None:
"""Filters defined in the command-line should take precedence over filters in ini files (#3946).""" """Filters defined in the command-line should take precedence over filters in ini files (#3946)."""
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
filterwarnings = error filterwarnings = error
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
import warnings import warnings
def test(): def test():
@ -394,22 +398,22 @@ def test_option_precedence_cmdline_over_ini(testdir, ignore_on_cmdline):
""" """
) )
args = ["-W", "ignore"] if ignore_on_cmdline else [] args = ["-W", "ignore"] if ignore_on_cmdline else []
result = testdir.runpytest(*args) result = pytester.runpytest(*args)
if ignore_on_cmdline: if ignore_on_cmdline:
result.stdout.fnmatch_lines(["* 1 passed in*"]) result.stdout.fnmatch_lines(["* 1 passed in*"])
else: else:
result.stdout.fnmatch_lines(["* 1 failed in*"]) result.stdout.fnmatch_lines(["* 1 failed in*"])
def test_option_precedence_mark(testdir): def test_option_precedence_mark(pytester: Pytester) -> None:
"""Filters defined by marks should always take precedence (#3946).""" """Filters defined by marks should always take precedence (#3946)."""
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
filterwarnings = ignore filterwarnings = ignore
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest, warnings import pytest, warnings
@pytest.mark.filterwarnings('error') @pytest.mark.filterwarnings('error')
@ -417,7 +421,7 @@ def test_option_precedence_mark(testdir):
warnings.warn(UserWarning('hello')) warnings.warn(UserWarning('hello'))
""" """
) )
result = testdir.runpytest("-W", "ignore") result = pytester.runpytest("-W", "ignore")
result.stdout.fnmatch_lines(["* 1 failed in*"]) result.stdout.fnmatch_lines(["* 1 failed in*"])
@ -427,8 +431,8 @@ class TestDeprecationWarningsByDefault:
from pytest's own test suite from pytest's own test suite
""" """
def create_file(self, testdir, mark=""): def create_file(self, pytester: Pytester, mark="") -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest, warnings import pytest, warnings
@ -443,18 +447,18 @@ class TestDeprecationWarningsByDefault:
) )
@pytest.mark.parametrize("customize_filters", [True, False]) @pytest.mark.parametrize("customize_filters", [True, False])
def test_shown_by_default(self, testdir, customize_filters): def test_shown_by_default(self, pytester: Pytester, customize_filters) -> None:
"""Show deprecation warnings by default, even if user has customized the warnings filters (#4013).""" """Show deprecation warnings by default, even if user has customized the warnings filters (#4013)."""
self.create_file(testdir) self.create_file(pytester)
if customize_filters: if customize_filters:
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
filterwarnings = filterwarnings =
once::UserWarning once::UserWarning
""" """
) )
result = testdir.runpytest_subprocess() result = pytester.runpytest_subprocess()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*== %s ==*" % WARNINGS_SUMMARY_HEADER,
@ -464,9 +468,9 @@ class TestDeprecationWarningsByDefault:
] ]
) )
def test_hidden_by_ini(self, testdir): def test_hidden_by_ini(self, pytester: Pytester) -> None:
self.create_file(testdir) self.create_file(pytester)
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
filterwarnings = filterwarnings =
@ -474,18 +478,18 @@ class TestDeprecationWarningsByDefault:
ignore::PendingDeprecationWarning ignore::PendingDeprecationWarning
""" """
) )
result = testdir.runpytest_subprocess() result = pytester.runpytest_subprocess()
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
def test_hidden_by_mark(self, testdir): def test_hidden_by_mark(self, pytester: Pytester) -> None:
"""Should hide the deprecation warning from the function, but the warning during collection should """Should hide the deprecation warning from the function, but the warning during collection should
be displayed normally. be displayed normally.
""" """
self.create_file( self.create_file(
testdir, pytester,
mark='@pytest.mark.filterwarnings("ignore::PendingDeprecationWarning")', mark='@pytest.mark.filterwarnings("ignore::PendingDeprecationWarning")',
) )
result = testdir.runpytest_subprocess() result = pytester.runpytest_subprocess()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*== %s ==*" % WARNINGS_SUMMARY_HEADER,
@ -494,9 +498,9 @@ class TestDeprecationWarningsByDefault:
] ]
) )
def test_hidden_by_cmdline(self, testdir): def test_hidden_by_cmdline(self, pytester: Pytester) -> None:
self.create_file(testdir) self.create_file(pytester)
result = testdir.runpytest_subprocess( result = pytester.runpytest_subprocess(
"-W", "-W",
"ignore::DeprecationWarning", "ignore::DeprecationWarning",
"-W", "-W",
@ -504,10 +508,10 @@ class TestDeprecationWarningsByDefault:
) )
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
def test_hidden_by_system(self, testdir, monkeypatch): def test_hidden_by_system(self, pytester: Pytester, monkeypatch) -> None:
self.create_file(testdir) self.create_file(pytester)
monkeypatch.setenv("PYTHONWARNINGS", "once::UserWarning") monkeypatch.setenv("PYTHONWARNINGS", "once::UserWarning")
result = testdir.runpytest_subprocess() result = pytester.runpytest_subprocess()
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str() assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
@ -515,13 +519,13 @@ class TestDeprecationWarningsByDefault:
@pytest.mark.skip( @pytest.mark.skip(
reason="This test should be enabled again before pytest 7.0 is released" reason="This test should be enabled again before pytest 7.0 is released"
) )
def test_deprecation_warning_as_error(testdir, change_default): def test_deprecation_warning_as_error(pytester: Pytester, change_default) -> None:
"""This ensures that PytestDeprecationWarnings raised by pytest are turned into errors. """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 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. to ensure our deprecations are turning into warnings as expected.
""" """
testdir.makepyfile( pytester.makepyfile(
""" """
import warnings, pytest import warnings, pytest
def test(): def test():
@ -529,7 +533,7 @@ def test_deprecation_warning_as_error(testdir, change_default):
""" """
) )
if change_default == "ini": if change_default == "ini":
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
filterwarnings = filterwarnings =
@ -542,7 +546,7 @@ def test_deprecation_warning_as_error(testdir, change_default):
if change_default == "cmdline" if change_default == "cmdline"
else () else ()
) )
result = testdir.runpytest(*args) result = pytester.runpytest(*args)
if change_default is None: if change_default is None:
result.stdout.fnmatch_lines(["* 1 failed in *"]) result.stdout.fnmatch_lines(["* 1 failed in *"])
else: else:
@ -552,23 +556,23 @@ def test_deprecation_warning_as_error(testdir, change_default):
class TestAssertionWarnings: class TestAssertionWarnings:
@staticmethod @staticmethod
def assert_result_warns(result, msg): def assert_result_warns(result, msg) -> None:
result.stdout.fnmatch_lines(["*PytestAssertRewriteWarning: %s*" % msg]) result.stdout.fnmatch_lines(["*PytestAssertRewriteWarning: %s*" % msg])
def test_tuple_warning(self, testdir): def test_tuple_warning(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
"""\ """\
def test_foo(): def test_foo():
assert (1,2) assert (1,2)
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
self.assert_result_warns( self.assert_result_warns(
result, "assertion is always true, perhaps remove parentheses?" result, "assertion is always true, perhaps remove parentheses?"
) )
def test_warnings_checker_twice(): def test_warnings_checker_twice() -> None:
"""Issue #4617""" """Issue #4617"""
expectation = pytest.warns(UserWarning) expectation = pytest.warns(UserWarning)
with expectation: with expectation:
@ -579,9 +583,9 @@ def test_warnings_checker_twice():
@pytest.mark.filterwarnings("ignore::pytest.PytestExperimentalApiWarning") @pytest.mark.filterwarnings("ignore::pytest.PytestExperimentalApiWarning")
@pytest.mark.filterwarnings("always") @pytest.mark.filterwarnings("always")
def test_group_warnings_by_message(testdir): def test_group_warnings_by_message(pytester: Pytester) -> None:
testdir.copy_example("warnings/test_group_warnings_by_message.py") pytester.copy_example("warnings/test_group_warnings_by_message.py")
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*== %s ==*" % WARNINGS_SUMMARY_HEADER,
@ -611,10 +615,10 @@ def test_group_warnings_by_message(testdir):
@pytest.mark.filterwarnings("ignore::pytest.PytestExperimentalApiWarning") @pytest.mark.filterwarnings("ignore::pytest.PytestExperimentalApiWarning")
@pytest.mark.filterwarnings("always") @pytest.mark.filterwarnings("always")
def test_group_warnings_by_message_summary(testdir): def test_group_warnings_by_message_summary(pytester: Pytester) -> None:
testdir.copy_example("warnings/test_group_warnings_by_message_summary") pytester.copy_example("warnings/test_group_warnings_by_message_summary")
testdir.syspathinsert() pytester.syspathinsert()
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*== %s ==*" % WARNINGS_SUMMARY_HEADER, "*== %s ==*" % WARNINGS_SUMMARY_HEADER,
@ -634,9 +638,9 @@ def test_group_warnings_by_message_summary(testdir):
) )
def test_pytest_configure_warning(testdir, recwarn): def test_pytest_configure_warning(pytester: Pytester, recwarn) -> None:
"""Issue 5115.""" """Issue 5115."""
testdir.makeconftest( pytester.makeconftest(
""" """
def pytest_configure(): def pytest_configure():
import warnings import warnings
@ -645,7 +649,7 @@ def test_pytest_configure_warning(testdir, recwarn):
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
assert result.ret == 5 assert result.ret == 5
assert "INTERNALERROR" not in result.stderr.str() assert "INTERNALERROR" not in result.stderr.str()
warning = recwarn.pop() warning = recwarn.pop()
@ -654,7 +658,7 @@ def test_pytest_configure_warning(testdir, recwarn):
class TestStackLevel: class TestStackLevel:
@pytest.fixture @pytest.fixture
def capwarn(self, testdir): def capwarn(self, pytester: Pytester):
class CapturedWarnings: class CapturedWarnings:
captured: List[ captured: List[
Tuple[warnings.WarningMessage, Optional[Tuple[str, int, str]]] Tuple[warnings.WarningMessage, Optional[Tuple[str, int, str]]]
@ -664,16 +668,16 @@ class TestStackLevel:
def pytest_warning_recorded(cls, warning_message, when, nodeid, location): def pytest_warning_recorded(cls, warning_message, when, nodeid, location):
cls.captured.append((warning_message, location)) cls.captured.append((warning_message, location))
testdir.plugins = [CapturedWarnings()] pytester.plugins = [CapturedWarnings()]
return CapturedWarnings return CapturedWarnings
def test_issue4445_rewrite(self, testdir, capwarn): def test_issue4445_rewrite(self, pytester: Pytester, capwarn) -> None:
"""#4445: Make sure the warning points to a reasonable location """#4445: Make sure the warning points to a reasonable location
See origin of _issue_warning_captured at: _pytest.assertion.rewrite.py:241 See origin of _issue_warning_captured at: _pytest.assertion.rewrite.py:241
""" """
testdir.makepyfile(some_mod="") pytester.makepyfile(some_mod="")
conftest = testdir.makeconftest( conftest = pytester.makeconftest(
""" """
import some_mod import some_mod
import pytest import pytest
@ -681,7 +685,7 @@ class TestStackLevel:
pytest.register_assert_rewrite("some_mod") pytest.register_assert_rewrite("some_mod")
""" """
) )
testdir.parseconfig() pytester.parseconfig()
# with stacklevel=5 the warning originates from register_assert_rewrite # with stacklevel=5 the warning originates from register_assert_rewrite
# function in the created conftest.py # function in the created conftest.py
@ -694,16 +698,16 @@ class TestStackLevel:
assert func == "<module>" # the above conftest.py assert func == "<module>" # the above conftest.py
assert lineno == 4 assert lineno == 4
def test_issue4445_preparse(self, testdir, capwarn): def test_issue4445_preparse(self, pytester: Pytester, capwarn) -> None:
"""#4445: Make sure the warning points to a reasonable location """#4445: Make sure the warning points to a reasonable location
See origin of _issue_warning_captured at: _pytest.config.__init__.py:910 See origin of _issue_warning_captured at: _pytest.config.__init__.py:910
""" """
testdir.makeconftest( pytester.makeconftest(
""" """
import nothing import nothing
""" """
) )
testdir.parseconfig("--help") pytester.parseconfig("--help")
# with stacklevel=2 the warning should originate from config._preparse and is # with stacklevel=2 the warning should originate from config._preparse and is
# thrown by an errorneous conftest.py # thrown by an errorneous conftest.py
@ -716,29 +720,29 @@ class TestStackLevel:
assert func == "_preparse" assert func == "_preparse"
@pytest.mark.filterwarnings("default") @pytest.mark.filterwarnings("default")
def test_conftest_warning_captured(self, testdir: Testdir) -> None: def test_conftest_warning_captured(self, pytester: Pytester) -> None:
"""Warnings raised during importing of conftest.py files is captured (#2891).""" """Warnings raised during importing of conftest.py files is captured (#2891)."""
testdir.makeconftest( pytester.makeconftest(
""" """
import warnings import warnings
warnings.warn(UserWarning("my custom warning")) warnings.warn(UserWarning("my custom warning"))
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["conftest.py:2", "*UserWarning: my custom warning*"] ["conftest.py:2", "*UserWarning: my custom warning*"]
) )
def test_issue4445_import_plugin(self, testdir, capwarn): def test_issue4445_import_plugin(self, pytester: Pytester, capwarn) -> None:
"""#4445: Make sure the warning points to a reasonable location""" """#4445: Make sure the warning points to a reasonable location"""
testdir.makepyfile( pytester.makepyfile(
some_plugin=""" some_plugin="""
import pytest import pytest
pytest.skip("thing", allow_module_level=True) pytest.skip("thing", allow_module_level=True)
""" """
) )
testdir.syspathinsert() pytester.syspathinsert()
testdir.parseconfig("-p", "some_plugin") pytester.parseconfig("-p", "some_plugin")
# with stacklevel=2 the warning should originate from # with stacklevel=2 the warning should originate from
# config.PytestPluginManager.import_plugin is thrown by a skipped plugin # config.PytestPluginManager.import_plugin is thrown by a skipped plugin
@ -751,11 +755,11 @@ class TestStackLevel:
assert f"config{os.sep}__init__.py" in file assert f"config{os.sep}__init__.py" in file
assert func == "_warn_about_skipped_plugins" assert func == "_warn_about_skipped_plugins"
def test_issue4445_issue5928_mark_generator(self, testdir): def test_issue4445_issue5928_mark_generator(self, pytester: Pytester) -> None:
"""#4445 and #5928: Make sure the warning from an unknown mark points to """#4445 and #5928: Make sure the warning from an unknown mark points to
the test file where this mark is used. the test file where this mark is used.
""" """
testfile = testdir.makepyfile( testfile = pytester.makepyfile(
""" """
import pytest import pytest
@ -764,7 +768,7 @@ class TestStackLevel:
pass pass
""" """
) )
result = testdir.runpytest_subprocess() result = pytester.runpytest_subprocess()
# with stacklevel=2 the warning should originate from the above created test file # with stacklevel=2 the warning should originate from the above created test file
result.stdout.fnmatch_lines_random( result.stdout.fnmatch_lines_random(
[ [