Hook to handle --shard_tests command line option. If set, this "deselects" a subset of tests, by hashing (using crc32()) their id into buckets.
(items, config, session)
| 613 | |
| 614 | @pytest.hookimpl(trylast=True) |
| 615 | def pytest_collection_modifyitems(items, config, session): |
| 616 | """Hook to handle --shard_tests command line option. |
| 617 | |
| 618 | If set, this "deselects" a subset of tests, by hashing (using crc32()) |
| 619 | their id into buckets. |
| 620 | """ |
| 621 | if not config.option.shard_tests: |
| 622 | return |
| 623 | |
| 624 | num_items = len(items) |
| 625 | this_shard, num_shards = list(map(int, config.option.shard_tests.split("/"))) |
| 626 | assert 0 <= this_shard <= num_shards |
| 627 | if this_shard == num_shards: |
| 628 | this_shard = 0 |
| 629 | |
| 630 | items_selected, items_deselected = [], [] |
| 631 | for i in items: |
| 632 | if crc32(i.nodeid.encode('utf-8')) % num_shards == this_shard: |
| 633 | items_selected.append(i) |
| 634 | else: |
| 635 | items_deselected.append(i) |
| 636 | config.hook.pytest_deselected(items=items_deselected) |
| 637 | |
| 638 | # We must modify the items list in place for it to take effect. |
| 639 | items[:] = items_selected |
| 640 | |
| 641 | logging.info( |
| 642 | "pytest shard selection enabled %s. Of %d items, selected %d items by hash.", |
| 643 | config.option.shard_tests, num_items, len(items)) |
| 644 | |
| 645 | |
| 646 | @pytest.hookimpl(trylast=True) |