diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2017-01-26 07:36:07 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2017-01-26 07:36:07 -0500 |
commit | 72fb471371f1d8e2c79d3c7f682628f5d6d1a28d (patch) | |
tree | ac228ec3a763ffbd644adaf574eee053054d1f92 /tests | |
parent | 2d124b93708636179f5c611eb698ac68901c5931 (diff) | |
download | python-coveragepy-git-72fb471371f1d8e2c79d3c7f682628f5d6d1a28d.tar.gz |
Move the decorator out of the metaclass, so we can test it
Diffstat (limited to 'tests')
-rw-r--r-- | tests/coveragetest.py | 25 | ||||
-rw-r--r-- | tests/test_testing.py | 25 |
2 files changed, 36 insertions, 14 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py index bd5cf9af..306df3b8 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -36,26 +36,27 @@ from tests.helpers import run_command OK, ERR = 0, 1 +def convert_skip_exceptions(method): + """A decorator for test methods to convert StopEverything to SkipTest.""" + def wrapper(*args, **kwargs): + """Run the test method, and convert exceptions.""" + 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__(mcs, 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] = mcs.convert_skip_exceptions(attr_value) + attrs[attr_name] = convert_skip_exceptions(attr_value) return super(SkipConvertingMetaclass, mcs).__new__(mcs, name, bases, attrs) - @classmethod - def convert_skip_exceptions(mcs, method): - """The decorator that wraps test methods.""" - def wrapper(*args, **kwargs): - """Run the test method, and convert StopEverything to SkipTest.""" - try: - result = method(*args, **kwargs) - except StopEverything: - raise unittest.SkipTest("StopEverything!") - return result - return wrapper CoverageTestMethodsMixin = SkipConvertingMetaclass('CoverageTestMethodsMixin', (), {}) diff --git a/tests/test_testing.py b/tests/test_testing.py index a7f60b31..9776acb4 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -11,10 +11,11 @@ import sys import pytest import coverage -from coverage.backunittest import TestCase +from coverage.backunittest import TestCase, unittest from coverage.files import actual_path +from coverage.misc import StopEverything -from tests.coveragetest import CoverageTest +from tests.coveragetest import CoverageTest, convert_skip_exceptions from tests.helpers import CheckUniqueFilenames, re_lines, re_line @@ -203,6 +204,26 @@ def test_re_line_bad(text, pat): re_line(text, pat) +def test_convert_skip_exceptions(): + @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 SkipTest. + with pytest.raises(unittest.SkipTest): + some_method(exc=StopEverything) + + def _same_python_executable(e1, e2): """Determine if `e1` and `e2` refer to the same Python executable. |