From 4ee0b2567aa25eedf83e983a7939ecb190a523ae Mon Sep 17 00:00:00 2001 From: Pablo Marcos Date: Thu, 5 Jan 2023 17:16:03 +0100 Subject: [PATCH] Sanitize test names created by pytest-flakefinder Remove the `[N]` suffix to consider all tests the same so that testmon works well with test names generated by flakefinder. --- testmon/pytest_testmon.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/testmon/pytest_testmon.py b/testmon/pytest_testmon.py index ffb395a..f31b29b 100644 --- a/testmon/pytest_testmon.py +++ b/testmon/pytest_testmon.py @@ -386,9 +386,18 @@ def __init__(self, config, testmon_data): file for file in testmon_data.stable_files if file not in failing_files ] self.deselected_nodes = [ - node for node in testmon_data.stable_nodeids if node not in failing_nodes + self.sanitize_flake_finder_name(node) for node in testmon_data.stable_nodeids if node not in failing_nodes ] + def sanitize_flake_finder_name(self, name): + # Tests generated by pytest-flakefinder end with an [N] suffix. e.g. test_foo[0], + # test_foo[12], etc. Since pytest-flakefinder is hooking to pytest the first one + # (exactly the opposite of testmon), we need consider all those tests the same one to be + # able to deselect them. + start_bracket_pos = name.rfind('[') + new_name = name if start_bracket_pos < 0 else name[0:start_bracket_pos] + return new_name + def pytest_ignore_collect(self, path, config): strpath = os.path.relpath(path.strpath, config.rootdir.strpath) if strpath in self.deselected_files and self.config.testmon_config[2]: @@ -400,7 +409,7 @@ def pytest_collection_modifyitems(self, session, config, items): selected = [] deselected = [] for item in items: - if item.nodeid in self.deselected_nodes: + if self.sanitize_flake_finder_name(item.nodeid) in self.deselected_nodes: deselected.append(item) else: selected.append(item)