diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2021-02-02 09:04:58 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2021-02-02 09:04:58 -0500 |
commit | 9012623110f49635fff61d19a4f5bb779de91b99 (patch) | |
tree | 8b540954cbe2b6446f991d30dfc268fc6c039531 | |
parent | 53354d78837de6b94833052ff1ca3d2797e7549e (diff) | |
download | python-coveragepy-git-9012623110f49635fff61d19a4f5bb779de91b99.tar.gz |
refactor: move test mixins to their own file
-rw-r--r-- | tests/coveragetest.py | 32 | ||||
-rw-r--r-- | tests/mixins.py | 39 | ||||
-rw-r--r-- | tests/test_api.py | 4 | ||||
-rw-r--r-- | tests/test_testing.py | 3 |
4 files changed, 46 insertions, 32 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py index c4a46da9..eeabfb46 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -6,7 +6,6 @@ import contextlib import datetime import difflib -import functools import glob import os import os.path @@ -14,20 +13,19 @@ import random import re import shlex import sys -import types import pytest from unittest_mixins import EnvironmentAwareMixin, StdStreamCapturingMixin, TempDirMixin import coverage from coverage import env -from coverage.backunittest import TestCase, unittest +from coverage.backunittest import TestCase from coverage.backward import StringIO, import_local_file, string_class, shlex_quote from coverage.cmdline import CoverageScript -from coverage.misc import StopEverything from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs from tests.helpers import run_command, SuperModuleCleaner +from tests.mixins import StopEverythingMixin # Status returns for the command line. @@ -37,35 +35,11 @@ OK, ERR = 0, 1 TESTS_DIR = os.path.dirname(__file__) -def convert_skip_exceptions(method): - """A decorator for test methods to convert StopEverything to SkipTest.""" - @functools.wraps(method) - def _wrapper(*args, **kwargs): - try: - result = method(*args, **kwargs) - except StopEverything: - raise unittest.SkipTest("StopEverything!") - return result - return _wrapper - - -class SkipConvertingMetaclass(type): - """Decorate all test methods to convert StopEverything to SkipTest.""" - 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) - - -CoverageTestMethodsMixin = SkipConvertingMetaclass('CoverageTestMethodsMixin', (), {}) - class CoverageTest( EnvironmentAwareMixin, StdStreamCapturingMixin, TempDirMixin, - CoverageTestMethodsMixin, + StopEverythingMixin, TestCase, ): """A base class for coverage.py test cases.""" diff --git a/tests/mixins.py b/tests/mixins.py new file mode 100644 index 00000000..97ca093c --- /dev/null +++ b/tests/mixins.py @@ -0,0 +1,39 @@ +# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 +# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt + +""" +Test class mixins + +Some of these are transitional while working toward pure-pytest style. +""" + +import functools +import types + +from coverage.backunittest import unittest +from coverage.misc import StopEverything + + +def convert_skip_exceptions(method): + """A decorator for test methods to convert StopEverything to SkipTest.""" + @functools.wraps(method) + def _wrapper(*args, **kwargs): + try: + result = method(*args, **kwargs) + except StopEverything: + raise unittest.SkipTest("StopEverything!") + return result + return _wrapper + + +class SkipConvertingMetaclass(type): + """Decorate all test methods to convert StopEverything to SkipTest.""" + 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', (), {}) diff --git a/tests/test_api.py b/tests/test_api.py index bce431f3..6c322795 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, CoverageTestMethodsMixin, TESTS_DIR, UsingModulesMixin +from tests.coveragetest import CoverageTest, StopEverythingMixin, TESTS_DIR, UsingModulesMixin class ApiTest(CoverageTest): @@ -794,7 +794,7 @@ class NamespaceModuleTest(UsingModulesMixin, CoverageTest): cov.report() -class IncludeOmitTestsMixin(UsingModulesMixin, CoverageTestMethodsMixin): +class IncludeOmitTestsMixin(UsingModulesMixin, StopEverythingMixin): """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 21e09dcc..b6ffe1a0 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -17,11 +17,12 @@ from coverage.backunittest import TestCase, unittest from coverage.files import actual_path from coverage.misc import StopEverything -from tests.coveragetest import CoverageTest, convert_skip_exceptions +from tests.coveragetest import CoverageTest from tests.helpers import ( arcs_to_arcz_repr, arcz_to_arcs, CheckUniqueFilenames, re_lines, re_line, without_module, ) +from tests.mixins import convert_skip_exceptions def test_xdist_sys_path_nuttiness_is_fixed(): |