summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-03-11 15:25:11 -0500
committerNed Batchelder <ned@nedbatchelder.com>2021-03-11 15:25:11 -0500
commit1bdcc691f5127edf9f197f8b509a2eeff51edcd6 (patch)
tree60fdc179154ae813e53abc0493b9b01eb9e933ab
parentd2985974a0fba46df7552a9958c7f9ef34f75868 (diff)
downloadpython-coveragepy-git-nedbat/remove-unittest-testcase.tar.gz
test: simplify how StopEverything is converted to skipnedbat/remove-unittest-testcase
The auto-decorating metaclass was interfering with parameterized methods on Python 2.7. But we don't need it anymore anyway, since pytest will let us hook to deal with the exception in a simpler way.
-rw-r--r--tests/conftest.py9
-rw-r--r--tests/coveragetest.py6
-rw-r--r--tests/mixins.py28
-rw-r--r--tests/test_api.py4
-rw-r--r--tests/test_testing.py29
5 files changed, 12 insertions, 64 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 81ec9f77..11d7aece 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -14,6 +14,7 @@ import warnings
import pytest
from coverage import env
+from coverage.misc import StopEverything
# Pytest will rewrite assertions in test modules, but not elsewhere.
@@ -92,3 +93,11 @@ def fix_xdist_sys_path():
del os.environ['PYTHONPATH']
except KeyError:
pass
+
+
+@pytest.hookimpl(hookwrapper=True)
+def pytest_runtest_call(item):
+ """Convert StopEverything into skipped tests."""
+ outcome = yield
+ if outcome.excinfo and issubclass(outcome.excinfo[0], StopEverything):
+ pytest.skip("Skipping {} for StopEverything: {}".format(item.nodeid, outcome.excinfo[1]))
diff --git a/tests/coveragetest.py b/tests/coveragetest.py
index c52892b5..f08de798 100644
--- a/tests/coveragetest.py
+++ b/tests/coveragetest.py
@@ -23,10 +23,7 @@ from coverage.cmdline import CoverageScript
from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal
from tests.helpers import run_command, SuperModuleCleaner
-from tests.mixins import (
- StdStreamCapturingMixin, StopEverythingMixin,
- TempDirMixin, PytestBase,
-)
+from tests.mixins import StdStreamCapturingMixin, TempDirMixin, PytestBase
# Status returns for the command line.
@@ -39,7 +36,6 @@ TESTS_DIR = os.path.dirname(__file__)
class CoverageTest(
StdStreamCapturingMixin,
TempDirMixin,
- StopEverythingMixin,
PytestBase,
):
"""A base class for coverage.py test cases."""
diff --git a/tests/mixins.py b/tests/mixins.py
index 4954a6a5..e8068be4 100644
--- a/tests/mixins.py
+++ b/tests/mixins.py
@@ -7,17 +7,14 @@ Test class mixins
Some of these are transitional while working toward pure-pytest style.
"""
-import functools
import os
import os.path
import sys
-import types
import textwrap
import pytest
from coverage import env
-from coverage.misc import StopEverything
class PytestBase(object):
@@ -146,31 +143,6 @@ class TempDirMixin(object):
return filename
-def convert_skip_exceptions(method):
- """A decorator for test methods to convert StopEverything to skips."""
- @functools.wraps(method)
- def _wrapper(*args, **kwargs):
- try:
- result = method(*args, **kwargs)
- except StopEverything:
- pytest.skip("StopEverything!")
- return result
- return _wrapper
-
-
-class SkipConvertingMetaclass(type):
- """Decorate all test methods to convert StopEverything to skips."""
- def __new__(cls, name, bases, attrs):
- for attr_name, attr_value in attrs.items():
- if attr_name.startswith('test_') and isinstance(attr_value, types.FunctionType):
- attrs[attr_name] = convert_skip_exceptions(attr_value)
-
- return super(SkipConvertingMetaclass, cls).__new__(cls, name, bases, attrs)
-
-
-StopEverythingMixin = SkipConvertingMetaclass('StopEverythingMixin', (), {})
-
-
class StdStreamCapturingMixin:
"""
Adapter from the pytest capsys fixture to more convenient methods.
diff --git a/tests/test_api.py b/tests/test_api.py
index a865c24c..3a5c86ac 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -22,7 +22,7 @@ from coverage.data import line_counts
from coverage.files import abs_file, relative_filename
from coverage.misc import CoverageException
-from tests.coveragetest import CoverageTest, StopEverythingMixin, TESTS_DIR, UsingModulesMixin
+from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
from tests.helpers import assert_count_equal
@@ -789,7 +789,7 @@ class NamespaceModuleTest(UsingModulesMixin, CoverageTest):
cov.report()
-class IncludeOmitTestsMixin(UsingModulesMixin, StopEverythingMixin):
+class IncludeOmitTestsMixin(UsingModulesMixin, CoverageTest):
"""Test methods for coverage methods taking include and omit."""
# We don't write any source files, but the data file will collide with
diff --git a/tests/test_testing.py b/tests/test_testing.py
index ad26bada..558c846e 100644
--- a/tests/test_testing.py
+++ b/tests/test_testing.py
@@ -14,14 +14,12 @@ import pytest
import coverage
from coverage import tomlconfig
from coverage.files import actual_path
-from coverage.misc import StopEverything
from tests.coveragetest import CoverageTest
from tests.helpers import (
arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal,
CheckUniqueFilenames, re_lines, re_line, without_module,
)
-from tests.mixins import convert_skip_exceptions
def test_xdist_sys_path_nuttiness_is_fixed():
@@ -320,33 +318,6 @@ class ReLinesTest(CoverageTest):
re_line(text, pat)
-def test_convert_skip_exceptions():
- # pytest doesn't expose the exception raised by pytest.skip, so let's
- # make one to get the class.
- try:
- pytest.skip("Just to get the exception")
- except BaseException as exc:
- pytest_Skipped = type(exc)
-
- @convert_skip_exceptions
- def some_method(ret=None, exc=None):
- """Be like a test case."""
- if exc:
- raise exc("yikes!")
- return ret
-
- # Normal flow is normal.
- assert some_method(ret=[17, 23]) == [17, 23]
-
- # Exceptions are raised normally.
- with pytest.raises(ValueError):
- some_method(exc=ValueError)
-
- # But a StopEverything becomes a skip.
- with pytest.raises(pytest_Skipped):
- some_method(exc=StopEverything)
-
-
def _same_python_executable(e1, e2):
"""Determine if `e1` and `e2` refer to the same Python executable.