def track_it(testdir, func): testmon = Testmon(project_dirs=[testdir.tmpdir.strpath], testmon_labels=set()) testmon_data = TestmonData(testdir.tmpdir.strpath) _result, coverage_data = testmon.track_dependencies(func, 'testnode') testmon_data.set_dependencies('testnode', coverage_data, testdir.tmpdir.strpath) return testmon_data.node_data['testnode']
class TestmonDeselect(object): def __init__(self, config, testmon_data): self.testmon_data = testmon_data self.testmon = Testmon(config.project_dirs, testmon_labels=testmon_options(config) ) self.testmon_save = True self.config = config self.lastfailed = self.testmon_data.lastfailed def pytest_report_header(self, config): changed_files = ",".join([os.path.relpath(path, config.rootdir.strpath) for path in self.testmon_data.modules_cache]) if changed_files=='' or len(changed_files)>100: changed_files = len(self.testmon_data.modules_cache) active_message = "testmon={}, changed files: {}, skipping collection of {} items".format(config.getoption('testmon'), changed_files, sum(self.testmon_data.unaffected_paths.values())) if self.testmon_data.variant: return active_message + ", run variant: {}".format(self.testmon_data.variant) else: return active_message + "." def pytest_collection_modifyitems(self, session, config, items): selected, deselected = [], [] self.testmon_data.collect_garbage(allnodeids=[item.nodeid for item in items]) for item in items: if item.nodeid in self.lastfailed or self.testmon_data.test_should_run(item.nodeid): selected.append(item) else: deselected.append(item) items[:] = selected if deselected: config.hook.pytest_deselected(items=deselected) def pytest_runtest_protocol(self, __multicall__, item, nextitem): if self.config.getoption('testmon') == u'readonly': return __multicall__.execute() result, coverage_data = self.testmon.track_dependencies(__multicall__.execute, item.nodeid) self.testmon_data.set_dependencies(item.nodeid, coverage_data, item.config.rootdir.strpath) return result def pytest_runtest_logreport(self, report): if report.failed and "xfail" not in report.keywords: if report.nodeid not in self.lastfailed: self.lastfailed.append(report.nodeid) elif not report.failed: if report.when == "call": try: if report.nodeid in self.lastfailed: self.lastfailed.remove(report.nodeid) except KeyError: pass def pytest_ignore_collect(self, path, config): strpath = path.strpath if strpath in self.testmon_data.unaffected_paths: config.hook.pytest_deselected(items=['1'] * self.testmon_data.unaffected_paths[strpath]) return True def pytest_internalerror(self, excrepr, excinfo): self.testmon_save = False def pytest_keyboard_interrupt(self, excinfo): self.testmon_save = False def pytest_sessionfinish(self, session): if self.testmon_save: self.testmon_data.write_data() self.testmon.close()