Skip to content

Commit 25e657b

Browse files
committedJan 14, 2021
Deprecate raising unittest.SkipTest to skip tests during collection
It is not very clear why this code exists -- we are not running any unittest or nose code during collection, and really these frameworks don't have the concept of collection at all, and just raising these exceptions at e.g. the module level would cause an error. So unless I'm missing something, I don't think anyone is using this. Deprecate it so we can eventually clear up this code and keep unittest more tightly restricted to its plugin.
1 parent 42d5545 commit 25e657b

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed
 

‎changelog/8242.deprecation.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Raising :class:`unittest.SkipTest` to skip collection of tests during the
2+
pytest collection phase is deprecated. Use :func:`pytest.skip` instead.
3+
4+
Note: This deprecation only relates to using `unittest.SkipTest` during test
5+
collection. You are probably not doing that. Ordinary usage of
6+
:class:`unittest.SkipTest` / :meth:`unittest.TestCase.skipTest` /
7+
:func:`unittest.skip` in unittest test cases is fully supported.

‎doc/en/deprecations.rst

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ Deprecated Features
1818
Below is a complete list of all pytest features which are considered deprecated. Using those features will issue
1919
:class:`PytestWarning` or subclasses, which can be filtered using :ref:`standard warning filters <warnings>`.
2020

21+
Raising ``unittest.SkipTest`` during collection
22+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23+
24+
.. deprecated:: 6.3
25+
26+
Raising :class:`unittest.SkipTest` to skip collection of tests during the
27+
pytest collection phase is deprecated. Use :func:`pytest.skip` instead.
28+
29+
Note: This deprecation only relates to using `unittest.SkipTest` during test
30+
collection. You are probably not doing that. Ordinary usage of
31+
:class:`unittest.SkipTest` / :meth:`unittest.TestCase.skipTest` /
32+
:func:`unittest.skip` in unittest test cases is fully supported.
33+
34+
2135
The ``--strict`` command-line option
2236
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2337

‎src/_pytest/deprecated.py

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464

6565
PRIVATE = PytestDeprecationWarning("A private pytest class or function was used.")
6666

67+
UNITTEST_SKIP_DURING_COLLECTION = PytestDeprecationWarning(
68+
"Raising unittest.SkipTest to skip tests during collection is deprecated. "
69+
"Use pytest.skip() instead."
70+
)
71+
6772

6873
# You want to make some `__init__` or function "private".
6974
#

‎src/_pytest/runner.py

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import bdb
33
import os
44
import sys
5+
import warnings
56
from typing import Callable
67
from typing import cast
78
from typing import Dict
@@ -27,6 +28,7 @@
2728
from _pytest.compat import final
2829
from _pytest.config.argparsing import Parser
2930
from _pytest.deprecated import check_ispytest
31+
from _pytest.deprecated import UNITTEST_SKIP_DURING_COLLECTION
3032
from _pytest.nodes import Collector
3133
from _pytest.nodes import Item
3234
from _pytest.nodes import Node
@@ -374,6 +376,11 @@ def pytest_make_collect_report(collector: Collector) -> CollectReport:
374376
# Type ignored because unittest is loaded dynamically.
375377
skip_exceptions.append(unittest.SkipTest) # type: ignore
376378
if isinstance(call.excinfo.value, tuple(skip_exceptions)):
379+
if unittest is not None and isinstance(
380+
call.excinfo.value, unittest.SkipTest # type: ignore[attr-defined]
381+
):
382+
warnings.warn(UNITTEST_SKIP_DURING_COLLECTION, stacklevel=2)
383+
377384
outcome = "skipped"
378385
r_ = collector._repr_failure_py(call.excinfo, "line")
379386
assert isinstance(r_, ExceptionChainRepr), repr(r_)

‎testing/deprecated_test.py

+17
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,20 @@ def __init__(self, foo: int, *, _ispytest: bool = False) -> None:
136136

137137
# Doesn't warn.
138138
PrivateInit(10, _ispytest=True)
139+
140+
141+
def test_raising_unittest_skiptest_during_collection_is_deprecated(
142+
pytester: Pytester,
143+
) -> None:
144+
pytester.makepyfile(
145+
"""
146+
import unittest
147+
raise unittest.SkipTest()
148+
"""
149+
)
150+
result = pytester.runpytest()
151+
result.stdout.fnmatch_lines(
152+
[
153+
"*PytestDeprecationWarning: Raising unittest.SkipTest*",
154+
]
155+
)

0 commit comments

Comments
 (0)
Please sign in to comment.