summaryrefslogtreecommitdiff
path: root/tests/test_api.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2019-06-23 07:14:16 -0400
committerNed Batchelder <ned@nedbatchelder.com>2019-06-25 09:54:41 -0400
commite9e703c7885840412295a0aaf3c84b5244483881 (patch)
treeab63e85a914e00207b2e3ae0614c11ea766d7813 /tests/test_api.py
parente126d4720a4abad33a772734e0a0b40403b974d2 (diff)
downloadpython-coveragepy-git-e9e703c7885840412295a0aaf3c84b5244483881.tar.gz
New API: Coverage.current() returns the latest started instance.
Diffstat (limited to 'tests/test_api.py')
-rw-r--r--tests/test_api.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/test_api.py b/tests/test_api.py
index a034c828..920cd9ad 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -614,6 +614,37 @@ class ApiTest(CoverageTest):
cov.switch_context("test3")
+class CurrentInstanceTest(CoverageTest):
+ """Tests of Coverage.current()."""
+
+ def assert_current_is_none(self, current):
+ """Assert that a current we expect to be None is correct."""
+ # During meta-coverage, the None answers will be wrong because the
+ # overall coverage measurement will still be on the current-stack.
+ # Since we know they will be wrong, and we have non-meta test runs
+ # also, don't assert them.
+ if not env.METACOV:
+ assert current is None
+
+ def test_current(self):
+ cur0 = coverage.Coverage.current()
+ self.assert_current_is_none(cur0)
+ # Making an instance doesn't make it current.
+ cov = coverage.Coverage()
+ cur1 = coverage.Coverage.current()
+ self.assert_current_is_none(cur1)
+ assert cur0 is cur1
+ # Starting the instance makes it current.
+ cov.start()
+ cur2 = coverage.Coverage.current()
+ assert cur2 is cov
+ # Stopping the instance makes current None again.
+ cov.stop()
+ cur3 = coverage.Coverage.current()
+ self.assert_current_is_none(cur3)
+ assert cur0 is cur3
+
+
class NamespaceModuleTest(UsingModulesMixin, CoverageTest):
"""Test PEP-420 namespace modules."""