Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions .github/workflows/superset-python-unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,27 @@ jobs:
needs: changes
if: needs.changes.outputs.python == 'true'
runs-on: ubuntu-26.04
# The full suite takes about 42 minutes on two-worker runners. Leave
# headroom for setup, scoped coverage gates, uploads, and runner variance.
timeout-minutes: 60
# Leave time for setup, one shard of the suite, the scoped coverage gates
# (shard 1 only), and uploads.
timeout-minutes: 45
permissions:
# Declaring any permission drops the defaults, and checkout and `$/` actions
# need read access wherever the repository is not public.
contents: read
id-token: write
strategy:
# Let every shard finish so one failure doesn't hide the others.
fail-fast: false
matrix:
# Just "current" here, on both push and PR — the "next" Python minor is
# exercised separately by the nightly canary (python-nightly-canary.yml)
# so cross-version breaks don't cost a runner on every push/PR.
python-version: ["current"]
# The suite is split by test file across these runners (see
# tests/pytest_shard.py): on a 2-core runner pytest-xdist only gets two
# workers, and the unsharded run had grown to ~40 minutes against this
# job's timeout. Keep NUM_SHARDS below in step with this list.
shard: [1, 2, 3]
env:
PYTHONPATH: ${{ github.workspace }}
steps:
Expand All @@ -72,6 +79,7 @@ jobs:
- name: Install HiveServer2 transport test dependencies
run: uv pip install --system -e ".[hive,impala]"
- name: SDK unit tests
if: matrix.shard == 1
env:
PYTEST_DISABLE_PLUGIN_AUTOLOAD: "1"
PYTHONPATH: ${{ github.workspace }}/superset-core/src
Expand Down Expand Up @@ -99,8 +107,10 @@ jobs:
env:
SUPERSET_TESTENV: true
SUPERSET_SECRET_KEY: not-a-secret
NUM_SHARDS: 3
SHARD_ID: ${{ matrix.shard }}
run: |
pytest -n auto --dist loadfile --durations=0 --durations-min=0.5 --cov-report= --cov=superset ./tests/common ./tests/unit_tests --cache-clear --maxfail=50 --junit-xml=test-results/junit-unit.xml
pytest -p tests.pytest_shard --num-shards "$NUM_SHARDS" --shard-id "$SHARD_ID" -n auto --dist loadfile --durations=0 --durations-min=0.5 --cov-report= --cov=superset ./tests/common ./tests/unit_tests --cache-clear --maxfail=50 --junit-xml=test-results/junit-unit.xml
# COVERAGE_FILE keeps these scoped gates off the default .coverage that
# the step above wrote. pytest-cov starts a fresh data file per run, so
# without it the last gate replaces the full-suite data and the report
Expand All @@ -111,6 +121,7 @@ jobs:
# runner whose other cores sit idle. pytest-cov combines the
# per-worker data before --cov-fail-under is evaluated.
- name: Python 100% coverage unit tests
if: matrix.shard == 1
env:
SUPERSET_TESTENV: true
SUPERSET_SECRET_KEY: not-a-secret
Expand All @@ -134,7 +145,7 @@ jobs:
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: junit-results-${{ matrix.python-version }}
name: junit-results-${{ matrix.python-version }}-${{ matrix.shard }}
path: test-results/
retention-days: 7

Expand Down
72 changes: 72 additions & 0 deletions tests/pytest_shard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Split one pytest run into deterministic shards, one test file per shard.

Load with ``-p tests.pytest_shard`` and pass ``--num-shards N --shard-id K``
(``K`` counts from 1). Each test file lands on exactly one shard, chosen by a
stable hash of its path, so module-scoped fixtures behave as in a full run
and every pytest-xdist worker computes the same selection. The shards of one
commit together run each collected test exactly once.
"""

from __future__ import annotations

import zlib

import pytest


def pytest_addoption(parser: pytest.Parser) -> None:
group = parser.getgroup("shard")
group.addoption(
"--num-shards",
type=int,
default=1,
help="Number of shards the run is split into (default: 1, no split).",
)
group.addoption(
"--shard-id",
type=int,
default=1,
help="Which shard to run, from 1 to --num-shards.",
)


def shard_of(nodeid: str, num_shards: int) -> int:
"""Return the 1-based shard for a test, keyed on its file path."""
path = nodeid.split("::", 1)[0]
return zlib.crc32(path.encode("utf-8")) % num_shards + 1


def pytest_collection_modifyitems(
config: pytest.Config, items: list[pytest.Item]
) -> None:
num_shards: int = config.getoption("num_shards")
shard_id: int = config.getoption("shard_id")
if num_shards < 1 or not 1 <= shard_id <= num_shards:
raise pytest.UsageError(
f"--shard-id must be between 1 and --num-shards ({num_shards}); "
f"got {shard_id}"
)
if num_shards == 1:
return
selected = [item for item in items if shard_of(item.nodeid, num_shards) == shard_id]
deselected = [
item for item in items if shard_of(item.nodeid, num_shards) != shard_id
]
items[:] = selected
config.hook.pytest_deselected(items=deselected)
32 changes: 32 additions & 0 deletions tests/unit_tests/pytest_shard_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from tests.pytest_shard import shard_of


def test_shard_of_keeps_a_file_on_one_shard() -> None:
"""Every test in a file lands on the same shard, so module fixtures hold."""
shards = {
shard_of(f"tests/unit_tests/foo_test.py::{name}", 3)
for name in ("test_a", "TestBar::test_b", "test_c[param]")
}
assert len(shards) == 1


def test_shard_of_covers_every_shard_within_range() -> None:
"""Shard ids stay in 1..N, and a realistic set of files reaches all of them."""
files = [f"tests/unit_tests/module_{i}_test.py" for i in range(200)]
assert {shard_of(path, 3) for path in files} == {1, 2, 3}
Loading