-
-
Notifications
You must be signed in to change notification settings - Fork 97
pytest test sharding #493
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
Merged
Merged
pytest test sharding #493
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e3d2f1f
Initial implementation of test sharding for the pytest runner
vinnybod b9ce906
change reference
vinnybod 7194656
change imports
vinnybod db4eeb5
apply ruff formatting
vinnybod 77ce3d1
code review
vinnybod 5a17cb0
also check total shards > 1
vinnybod b61b9e5
format README
vinnybod 1787c2c
Merge branch 'main' into test-sharding
vinnybod 4e716cd
add :__test__ as a dep in virtual_deps example
vinnybod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| def test_shard_one(): | ||
| assert True | ||
|
|
||
| def test_shard_two(): | ||
| assert True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,7 @@ py_test( | |
| ), | ||
| deps = [ | ||
| requirement("pytest"), | ||
| "__test__", | ||
| ":greet", | ||
| ], | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| load("@aspect_rules_py//py:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "pytest_shard", | ||
| srcs = [ | ||
| "__init__.py", | ||
| ":pytest_shard.py", | ||
| ], | ||
| imports = ["."], | ||
| visibility = ["//visibility:public"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Copyright 2019 Adam Gleave | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # pytest-shard | ||
|
|
||
| This is a fork of [pytest-shard](https://github.com/AdamGleave/pytest-shard) @ [4610a08](https://github.com/AdamGleave/pytest-shard/commit/64610a08dac6b0511b6d51cf895d0e1040d162ad) | ||
|
|
||
| ## Changes | ||
|
|
||
| - The pytest hooks were moved into a class `ShardPlugin`, so that they can be loaded via `pytest.main` | ||
| - The sharding strategy was changed to a simple round-robin strategy | ||
| - The hash-bashed strategy was causing unbalanced or empty shards with small test sets |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| from typing import Iterable, List, Sequence | ||
|
|
||
| from _pytest import nodes # for type checking only | ||
|
alexeagle marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def positive_int(x) -> int: | ||
| x = int(x) | ||
| if x < 0: | ||
| raise ValueError(f"Argument {x} must be positive") | ||
| return x | ||
|
|
||
|
|
||
| def filter_items_by_shard( | ||
| items: Iterable[nodes.Node], shard_id: int, num_shards: int | ||
| ) -> Sequence[nodes.Node]: | ||
| """Computes `items` that should be tested in `shard_id` out of `num_shards` total shards.""" | ||
| shards = [i % num_shards for i in range(len(items))] | ||
|
|
||
| new_items = [] | ||
| for shard, item in zip(shards, items): | ||
| if shard == shard_id: | ||
| new_items.append(item) | ||
| return new_items | ||
|
|
||
|
|
||
| class ShardPlugin: | ||
| @staticmethod | ||
| def pytest_addoption(parser): | ||
| """Add pytest-shard specific configuration parameters.""" | ||
| group = parser.getgroup("shard") | ||
| group.addoption( | ||
| "--shard-id", | ||
| dest="shard_id", | ||
| type=positive_int, | ||
| default=0, | ||
| help="Number of this shard.", | ||
| ) | ||
| group.addoption( | ||
| "--num-shards", | ||
| dest="num_shards", | ||
| type=positive_int, | ||
| default=1, | ||
| help="Total number of shards.", | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def pytest_report_collectionfinish(config, items: Sequence[nodes.Node]) -> str: | ||
| """Log how many and, if verbose, which items are tested in this shard.""" | ||
| msg = f"Running {len(items)} items in this shard" | ||
| if config.option.verbose > 0 and config.getoption("num_shards") > 1: | ||
|
vinnybod marked this conversation as resolved.
|
||
| msg += ": " + ", ".join([item.nodeid for item in items]) | ||
| return msg | ||
|
|
||
| @staticmethod | ||
| def pytest_collection_modifyitems(config, items: List[nodes.Node]): | ||
|
alexeagle marked this conversation as resolved.
|
||
| """Mutate the collection to consist of just items to be tested in this shard.""" | ||
| shard_id = config.getoption("shard_id") | ||
| shard_total = config.getoption("num_shards") | ||
| if shard_id >= shard_total: | ||
| raise ValueError( | ||
| "shard_num = f{shard_num} must be less than shard_total = f{shard_total}" | ||
| ) | ||
|
|
||
| items[:] = filter_items_by_shard(items, shard_id, shard_total) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.