Skip to content

Commit 7c07b39

Browse files
authored
feat: Test sharding support (#9)
1 parent 983d131 commit 7c07b39

File tree

7 files changed

+39
-0
lines changed

7 files changed

+39
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ py_pytest_test(
3434
)
3535
```
3636

37+
To use [test sharding](https://bazel.build/reference/test-encyclopedia#test-sharding), also add `requirement("pytest-shard")`
38+
to `deps`.
39+
3740
## Disclaimer
3841

3942
I rely on this myself and will accept issue reports and contributions, however this only has a simple smoke test and isn't

e2e/smoke/.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Enable after bazel 5.4.0 is removed from e2e tests to verify correct implementation of sharding.
2+
# test --incompatible_check_sharding_support

e2e/smoke/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ py_pytest_test(
1515
],
1616
)
1717

18+
py_pytest_test(
19+
name = "test_sharded",
20+
size = "small",
21+
srcs = ["test_sharded.py"],
22+
shard_count = 2,
23+
deps = [
24+
requirement("pytest"),
25+
requirement("pytest-shard"),
26+
],
27+
)
28+
1829
build_test(
1930
name = "smoke_test",
2031
targets = [

e2e/smoke/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest==7.3.2
2+
pytest-shard==0.1.2

e2e/smoke/requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ pluggy==1.2.0 \
2323
pytest==7.3.2 \
2424
--hash=sha256:cdcbd012c9312258922f8cd3f1b62a6580fdced17db6014896053d47cddf9295 \
2525
--hash=sha256:ee990a3cc55ba808b80795a79944756f315c67c12b56abd3ac993a7b8c17030b
26+
# via
27+
# -r requirements.in
28+
# pytest-shard
29+
pytest-shard==0.1.2 \
30+
--hash=sha256:407a1df385cebe1feb9b4d2e7eeee8b044f8a24f0919421233159a17c59be2b9 \
31+
--hash=sha256:b86a967fbfd1c8e50295095ccda031b7e890862ee06531d5142844f4c1d1cd67
2632
# via -r requirements.in
2733
tomli==2.0.1 \
2834
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \

e2e/smoke/test_sharded.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os
2+
3+
4+
def test_shard_0():
5+
assert os.environ["TEST_SHARD_INDEX"] == "0"
6+
7+
8+
def test_shard_1():
9+
assert os.environ["TEST_SHARD_INDEX"] == "1"

python_pytest/pytest_shim.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
if os.environ.get("XML_OUTPUT_FILE"):
1717
pytest_args.append("--junitxml={xml_output_file}".format(xml_output_file=os.environ.get("XML_OUTPUT_FILE")))
1818

19+
# Handle test sharding - requires pytest-shard plugin.
20+
if os.environ.get("TEST_SHARD_INDEX") and os.environ.get("TEST_TOTAL_SHARDS"):
21+
pytest_args.append("--shard-id={shard_id}".format(shard_id=os.environ.get("TEST_SHARD_INDEX")))
22+
pytest_args.append("--num-shards={num_shards}".format(num_shards=os.environ.get("TEST_TOTAL_SHARDS")))
23+
if os.environ.get("TEST_SHARD_STATUS_FILE"):
24+
open(os.environ["TEST_SHARD_STATUS_FILE"], "a").close()
25+
1926
# Handle plugins that generate reports - if they are provided with relative paths (via args),
2027
# re-write it under bazel's test undeclared outputs dir.
2128
if os.environ.get("TEST_UNDECLARED_OUTPUTS_DIR"):

0 commit comments

Comments
 (0)