diff --git a/changelog/8242.bugfix.rst b/changelog/8242.bugfix.rst new file mode 100644 index 00000000000..eaa46142eea --- /dev/null +++ b/changelog/8242.bugfix.rst @@ -0,0 +1,3 @@ +The deprecation of raising :class:`unittest.SkipTest` to skip collection of +tests during the pytest collection phase is reverted - this is now a supported +feature again. diff --git a/doc/en/changelog.rst b/doc/en/changelog.rst index 1acdad366da..0ad4f8938ac 100644 --- a/doc/en/changelog.rst +++ b/doc/en/changelog.rst @@ -211,6 +211,8 @@ Deprecations :class:`unittest.SkipTest` / :meth:`unittest.TestCase.skipTest` / :func:`unittest.skip` in unittest test cases is fully supported. + NOTE: This deprecation has been reverted in pytest 7.1.0. + - `#8315 `_: Several behaviors of :meth:`Parser.addoption ` are now scheduled for removal in pytest 8 (deprecated since pytest 2.4.0): diff --git a/doc/en/deprecations.rst b/doc/en/deprecations.rst index 4eaa80e2f27..1210947159b 100644 --- a/doc/en/deprecations.rst +++ b/doc/en/deprecations.rst @@ -241,19 +241,6 @@ scheduled for removal in pytest 8 (deprecated since pytest 2.4.0): - ``parser.addoption(..., type="int/string/float/complex")`` - use ``type=int`` etc. instead. -Raising ``unittest.SkipTest`` during collection -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. deprecated:: 7.0 - -Raising :class:`unittest.SkipTest` to skip collection of tests during the -pytest collection phase is deprecated. Use :func:`pytest.skip` instead. - -Note: This deprecation only relates to using `unittest.SkipTest` during test -collection. You are probably not doing that. Ordinary usage of -:class:`unittest.SkipTest` / :meth:`unittest.TestCase.skipTest` / -:func:`unittest.skip` in unittest test cases is fully supported. - Using ``pytest.warns(None)`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index 4534fbcab82..f2d79760ae7 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -47,11 +47,6 @@ # This deprecation is never really meant to be removed. PRIVATE = PytestDeprecationWarning("A private pytest class or function was used.") -UNITTEST_SKIP_DURING_COLLECTION = PytestRemovedIn8Warning( - "Raising unittest.SkipTest to skip tests during collection is deprecated. " - "Use pytest.skip() instead." -) - ARGUMENT_PERCENT_DEFAULT = PytestRemovedIn8Warning( 'pytest now uses argparse. "%default" should be changed to "%(default)s"', ) diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index e43dd2dc818..df6eecdb129 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -2,7 +2,6 @@ import bdb import os import sys -import warnings from typing import Callable from typing import cast from typing import Dict @@ -28,7 +27,6 @@ from _pytest.compat import final from _pytest.config.argparsing import Parser from _pytest.deprecated import check_ispytest -from _pytest.deprecated import UNITTEST_SKIP_DURING_COLLECTION from _pytest.nodes import Collector from _pytest.nodes import Item from _pytest.nodes import Node @@ -379,11 +377,6 @@ def pytest_make_collect_report(collector: Collector) -> CollectReport: # Type ignored because unittest is loaded dynamically. skip_exceptions.append(unittest.SkipTest) # type: ignore if isinstance(call.excinfo.value, tuple(skip_exceptions)): - if unittest is not None and isinstance( - call.excinfo.value, unittest.SkipTest # type: ignore[attr-defined] - ): - warnings.warn(UNITTEST_SKIP_DURING_COLLECTION, stacklevel=2) - outcome = "skipped" r_ = collector._repr_failure_py(call.excinfo, "line") assert isinstance(r_, ExceptionChainRepr), repr(r_) diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index 4104d19db9a..db649841abe 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -86,23 +86,6 @@ def __init__(self, foo: int, *, _ispytest: bool = False) -> None: PrivateInit(10, _ispytest=True) -def test_raising_unittest_skiptest_during_collection_is_deprecated( - pytester: Pytester, -) -> None: - pytester.makepyfile( - """ - import unittest - raise unittest.SkipTest() - """ - ) - result = pytester.runpytest() - result.stdout.fnmatch_lines( - [ - "*PytestRemovedIn8Warning: Raising unittest.SkipTest*", - ] - ) - - @pytest.mark.parametrize("hooktype", ["hook", "ihook"]) def test_hookproxy_warnings_for_pathlib(tmp_path, hooktype, request): path = legacy_path(tmp_path) diff --git a/testing/test_nose.py b/testing/test_nose.py index 1ded8854bb7..cab5a81a2b9 100644 --- a/testing/test_nose.py +++ b/testing/test_nose.py @@ -345,7 +345,7 @@ def test_failing(): """ ) result = pytester.runpytest(p) - result.assert_outcomes(skipped=1, warnings=1) + result.assert_outcomes(skipped=1, warnings=0) def test_SkipTest_in_test(pytester: Pytester) -> None: diff --git a/testing/test_unittest.py b/testing/test_unittest.py index 1601086d5b2..fb312814542 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -1498,3 +1498,30 @@ def test_it(self): assert passed == 1 assert failed == 1 assert reprec.ret == 1 + + +def test_raising_unittest_skiptest_during_collection( + pytester: Pytester, +) -> None: + pytester.makepyfile( + """ + import unittest + + class TestIt(unittest.TestCase): + def test_it(self): pass + def test_it2(self): pass + + raise unittest.SkipTest() + + class TestIt2(unittest.TestCase): + def test_it(self): pass + def test_it2(self): pass + """ + ) + reprec = pytester.inline_run() + passed, skipped, failed = reprec.countoutcomes() + assert passed == 0 + # Unittest reports one fake test for a skipped module. + assert skipped == 1 + assert failed == 0 + assert reprec.ret == ExitCode.NO_TESTS_COLLECTED