diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2021-02-02 10:53:27 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-02 10:53:27 -0500 |
commit | b7160a896252bb92ffe921a37e3cde98c5cb78b9 (patch) | |
tree | a0edae6ca0996217b9f0fa3213f824eab8b1cbe1 /tests/mixins.py | |
parent | 35d91c7aa186a84d0edb333bad60b520f3b1d719 (diff) | |
parent | 80c021d9174e7ae3e5183f1902903fb90a891246 (diff) | |
download | python-coveragepy-git-b7160a896252bb92ffe921a37e3cde98c5cb78b9.tar.gz |
Merge pull request #1113 from nedbat/nedbat/capsys
More unittest removal
Diffstat (limited to 'tests/mixins.py')
-rw-r--r-- | tests/mixins.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/mixins.py b/tests/mixins.py new file mode 100644 index 00000000..ab0623c0 --- /dev/null +++ b/tests/mixins.py @@ -0,0 +1,70 @@ +# 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 + +import pytest + +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', (), {}) + + +class StdStreamCapturingMixin: + """ + Adapter from the pytest capsys fixture to more convenient methods. + + This doesn't also output to the real stdout, so we probably want to move + to "real" capsys when we can use fixtures in test methods. + + Once you've used one of these methods, the capturing is reset, so another + invocation will only return the delta. + + """ + @pytest.fixture(autouse=True) + def _capcapsys(self, capsys): + """Grab the fixture so our methods can use it.""" + self.capsys = capsys + + def stdouterr(self): + """Returns (out, err), two strings for stdout and stderr.""" + return self.capsys.readouterr() + + def stdout(self): + """Returns a string, the captured stdout.""" + return self.capsys.readouterr().out + + def stderr(self): + """Returns a string, the captured stderr.""" + return self.capsys.readouterr().err |