Skip to content

Commit

Permalink
Super-simple implementation of pytest contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 9, 2019
1 parent 4082617 commit 6c94171
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/pytest_cov/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def pytest_addoption(parser):
'Default: False')
group.addoption('--cov-branch', action='store_true', default=None,
help='Enable branch coverage.')
group.addoption('--cov-context', action='store_true', default=None,
help='Enable dynamic context collection for each test.')


def _prepare_cov_source(cov_source):
Expand Down Expand Up @@ -282,16 +284,25 @@ def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write(message, **markup)

def pytest_runtest_setup(self, item):
if self.options.cov_context:
context = "{item.nodeid}|setup".format(item=item)
self.cov_controller.cov.switch_context(context)
if os.getpid() != self.pid:
# test is run in another process than session, run
# coverage manually
embed.init()

def pytest_runtest_teardown(self, item):
if self.options.cov_context:
context = "{item.nodeid}|teardown".format(item=item)
self.cov_controller.cov.switch_context(context)
embed.cleanup()

@compat.hookwrapper
def pytest_runtest_call(self, item):
if self.options.cov_context:
context = "{item.nodeid}|call".format(item=item)
self.cov_controller.cov.switch_context(context)
if (item.get_closest_marker('no_cover')
or 'no_cover' in getattr(item, 'fixturenames', ())):
self.cov_controller.pause()
Expand Down

0 comments on commit 6c94171

Please sign in to comment.