summaryrefslogtreecommitdiff
path: root/tests/mixins.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-02-02 09:04:58 -0500
committerNed Batchelder <ned@nedbatchelder.com>2021-02-02 09:04:58 -0500
commit9012623110f49635fff61d19a4f5bb779de91b99 (patch)
tree8b540954cbe2b6446f991d30dfc268fc6c039531 /tests/mixins.py
parent53354d78837de6b94833052ff1ca3d2797e7549e (diff)
downloadpython-coveragepy-git-9012623110f49635fff61d19a4f5bb779de91b99.tar.gz
refactor: move test mixins to their own file
Diffstat (limited to 'tests/mixins.py')
-rw-r--r--tests/mixins.py39
1 files changed, 39 insertions, 0 deletions
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', (), {})