Skip to content

Commit

Permalink
feat: Test sharding support (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
amartani committed Nov 9, 2023
1 parent 983d131 commit 7c07b39
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ py_pytest_test(
)
```

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

## Disclaimer

I rely on this myself and will accept issue reports and contributions, however this only has a simple smoke test and isn't
Expand Down
2 changes: 2 additions & 0 deletions e2e/smoke/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Enable after bazel 5.4.0 is removed from e2e tests to verify correct implementation of sharding.
# test --incompatible_check_sharding_support
11 changes: 11 additions & 0 deletions e2e/smoke/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ py_pytest_test(
],
)

py_pytest_test(
name = "test_sharded",
size = "small",
srcs = ["test_sharded.py"],
shard_count = 2,
deps = [
requirement("pytest"),
requirement("pytest-shard"),
],
)

build_test(
name = "smoke_test",
targets = [
Expand Down
1 change: 1 addition & 0 deletions e2e/smoke/requirements.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest==7.3.2
pytest-shard==0.1.2
6 changes: 6 additions & 0 deletions e2e/smoke/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pluggy==1.2.0 \
pytest==7.3.2 \
--hash=sha256:cdcbd012c9312258922f8cd3f1b62a6580fdced17db6014896053d47cddf9295 \
--hash=sha256:ee990a3cc55ba808b80795a79944756f315c67c12b56abd3ac993a7b8c17030b
# via
# -r requirements.in
# pytest-shard
pytest-shard==0.1.2 \
--hash=sha256:407a1df385cebe1feb9b4d2e7eeee8b044f8a24f0919421233159a17c59be2b9 \
--hash=sha256:b86a967fbfd1c8e50295095ccda031b7e890862ee06531d5142844f4c1d1cd67
# via -r requirements.in
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
Expand Down
9 changes: 9 additions & 0 deletions e2e/smoke/test_sharded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os


def test_shard_0():
assert os.environ["TEST_SHARD_INDEX"] == "0"


def test_shard_1():
assert os.environ["TEST_SHARD_INDEX"] == "1"
7 changes: 7 additions & 0 deletions python_pytest/pytest_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
if os.environ.get("XML_OUTPUT_FILE"):
pytest_args.append("--junitxml={xml_output_file}".format(xml_output_file=os.environ.get("XML_OUTPUT_FILE")))

# Handle test sharding - requires pytest-shard plugin.
if os.environ.get("TEST_SHARD_INDEX") and os.environ.get("TEST_TOTAL_SHARDS"):
pytest_args.append("--shard-id={shard_id}".format(shard_id=os.environ.get("TEST_SHARD_INDEX")))
pytest_args.append("--num-shards={num_shards}".format(num_shards=os.environ.get("TEST_TOTAL_SHARDS")))
if os.environ.get("TEST_SHARD_STATUS_FILE"):
open(os.environ["TEST_SHARD_STATUS_FILE"], "a").close()

# Handle plugins that generate reports - if they are provided with relative paths (via args),
# re-write it under bazel's test undeclared outputs dir.
if os.environ.get("TEST_UNDECLARED_OUTPUTS_DIR"):
Expand Down

0 comments on commit 7c07b39

Please sign in to comment.