Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Super-simple implementation of pytest contexts #283

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think plugin should call cov directly. I think this is something that cov_controller should manage

Copy link
Member

@graingert graingert Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eg pass options.cov_context here:

self.cov_controller = controller_cls(
self.options.cov_source,
self.options.cov_report,
self.options.cov_config,
self.options.cov_append,
self.options.cov_branch,
config,
nodeid
)

then run self.cov_controller.switch_context(item.nodeid, "setup")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I should close this PR: it will eventually be folded into #319 (where I could use some help understanding why tests are failing, actually... :) )

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