diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2019-04-26 13:57:49 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-04-26 13:57:49 -0400 |
commit | 792e2fccac3afc83bf46c65ceabd2b0ab7e692ab (patch) | |
tree | 9037ee3b0f86ae74f3dd0fdc0d542f48b196b49c | |
parent | 59208c20ee0d312bc0da16e8d143ed8b70ec5079 (diff) | |
download | python-coveragepy-git-792e2fccac3afc83bf46c65ceabd2b0ab7e692ab.tar.gz |
Old-style classes can report the test_function name. #797
-rw-r--r-- | CHANGES.rst | 5 | ||||
-rw-r--r-- | coverage/context.py | 2 | ||||
-rw-r--r-- | tests/test_context.py | 18 |
3 files changed, 24 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index cae6e2d7..dbb9f74c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -19,6 +19,10 @@ Unreleased - Dynamic contexts can now be determined by third-party code in a plugin. Thanks, Justas Sadzevičius. +- the ``dynamic_context = test_function`` setting now works with Python 2 + old-style classes, though it only reports the method name, not the class it + was defined on. Closes `issue 797`_. + - ``fail_under`` values more than 100 are reported as errors. Thanks to Mike Fiedler for closing `issue 746`_. @@ -46,6 +50,7 @@ Unreleased .. _issue 746: https://github.com/nedbat/coveragepy/issues/746 .. _issue 748: https://github.com/nedbat/coveragepy/issues/748 .. _issue 761: https://github.com/nedbat/coveragepy/issues/761 +.. _issue 797: https://github.com/nedbat/coveragepy/issues/797 .. _changes_50a4: diff --git a/coverage/context.py b/coverage/context.py index 22c0e531..13800337 100644 --- a/coverage/context.py +++ b/coverage/context.py @@ -65,7 +65,7 @@ def qualname_from_frame(frame): if hasattr(func, '__qualname__'): qname = func.__qualname__ else: - for cls in self.__class__.__mro__: + for cls in getattr(self.__class__, '__mro__', ()): f = cls.__dict__.get(fname, None) if f is None: continue diff --git a/tests/test_context.py b/tests/test_context.py index 1b50d30d..ddbf59a3 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -228,6 +228,18 @@ def fake_out(self): # pylint: disable=missing-docstring, unu def patch_meth(self): # pylint: disable=missing-docstring, unused-argument return get_qualname() +# pylint: disable=missing-docstring + +class OldStyle: # pylint: disable=old-style-class + def meth(self): + return get_qualname() + +class OldChild(OldStyle): + pass + +# pylint: enable=missing-docstring + + class QualnameTest(CoverageTest): """Tests of qualname_from_frame.""" @@ -262,3 +274,9 @@ class QualnameTest(CoverageTest): c = Child() c.meth = patch_meth self.assertEqual(c.meth(c), "patch_meth") + + def test_oldstyle(self): + if not env.PY2: + self.skipTest("Old-style classes are only in Python 2") + self.assertEqual(OldStyle().meth(), "meth") + self.assertEqual(OldChild().meth(), "meth") |