From 2a0fab8d16a57a7dc318ca81b106ef930e38d1fc Mon Sep 17 00:00:00 2001 From: Eric Lunderberg Date: Mon, 16 May 2022 16:25:53 -0500 Subject: [PATCH] [CI] Added message if test is running on another shard --- conftest.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/conftest.py b/conftest.py index 9768b6cc528d..3c04f0680a11 100644 --- a/conftest.py +++ b/conftest.py @@ -58,9 +58,9 @@ } -def should_run(nodeid: str, num_shards: int, shard_index: int) -> bool: +def find_shard_index(nodeid: str, num_shards: int) -> int: """ - Return true if this test should run on this shard + Return the index of the shard that should run this test """ for prefix, target_shard_idx in FIXED_ALLOCATION_PREFIXES.items(): if nodeid.startswith(prefix): @@ -68,7 +68,7 @@ def should_run(nodeid: str, num_shards: int, shard_index: int) -> bool: raise RuntimeError( f"Cannot collect sharded tests, {nodeid} has hardcoded shard index {target_shard_idx} among only {num_shards} shards" ) - return target_shard_idx == shard_index + return target_shard_idx if nodeid in HARDCODED_ALLOCATIONS: hash = HARDCODED_ALLOCATIONS[nodeid] @@ -76,7 +76,7 @@ def should_run(nodeid: str, num_shards: int, shard_index: int) -> bool: hash = hashlib.md5(nodeid.encode()) hash = int(hash.hexdigest(), 16) - return hash % num_shards == shard_index + return hash % num_shards def pytest_collection_modifyitems(config, items): @@ -89,5 +89,10 @@ def pytest_collection_modifyitems(config, items): print(f"Marking tests for shard {shard_index} of {num_shards}") for item in items: - if not should_run(item.nodeid, num_shards=num_shards, shard_index=shard_index): - item.add_marker(pytest.mark.skip()) + item_shard_index = find_shard_index(item.nodeid, num_shards=num_shards) + item.add_marker( + pytest.mark.skipif( + item_shard_index != shard_index, + reason=f"Test running on shard {item_shard_index} of {num_shards}", + ) + )