[TE-5134] Add --tag-filters option to filter tests by markers#426
[TE-5134] Add --tag-filters option to filter tests by markers#426
Conversation
There was a problem hiding this comment.
Clarifying comments to not confuse filter_tests with tag filtering. This class is actually fetching test files, not really filtering down the tests/files to execute.
508fb29 to
f5ea4ff
Compare
Add a new --tag-filters CLI flag that allows filtering tests by execution tags. This is useful for selectively running subsets of tests. Currently only Pytest is supported via markers [1]. When specified, the tag filter is passed to pytest via the -m flag (e.g., pytest -m "test_execution"). We will replace the default marker filter with a custom filter option implemented in the Python test collector [2] Changes: - Add --tag-filters flag and BUILDKITE_TEST_ENGINE_TAG_FILTERS env var - Add TagFilters field to Config struct - Update Pytest runner to use -m flag when tag filters are specified. (This will be replaced with a custom filter) - Add test coverage for marker-based filtering for pytest Example usage: bktec run --tag-filters "slow" BUILDKITE_TEST_ENGINE_TAG_FILTERS="not integration" bktec run [1] https://docs.pytest.org/en/7.1.x/example/markers.html [2] buildkite/test-collector-python#83
…w or skipped files
f5ea4ff to
6e8faf5
Compare
…ecution_tag markers
Replace pytest's built-in `-m` marker flag with the `--tag-filters` flag
from the Python Test Collector plugin (v1.2.0+). This change enables
proper key-value tag filtering for test execution.
Update test data markers from `@pytest.mark.test_execution("key", "value")`
to `@pytest.mark.execution_tag("key", "value")` to match the plugin's
expected format. Add documentation explaining the --tag-filters feature
and its requirements.
…thon_test_collector changes The uv tool is required to install branch versions of the python_test_collector for local testing of the --tag-filters functionality. This allows developers to test unreleased collector changes by installing from a local path with `uv pip install -e /path/to/python_test_collector` in a uv-managed virtual environment, providing a consistent and reproducible testing workflow. Quick cheat sheet: uv venv source .venv/bin/activate uv pip install -e /path/to/python_test_collector go test ...
541d1a2 to
2a5ab29
Compare
…tag-filters support
2a5ab29 to
1a7169a
Compare
|
The build will pass when we release a new version of the Python Test Collector buildkite/test-collector-python#83 |
| } | ||
| } | ||
|
|
||
| if c.TagFilters != "" && c.TestRunner != "pytest" { |
There was a problem hiding this comment.
Just a note, I have a wip branch to implement a SupportedFeatures() interface on each runner, e.g.:
func (p Pytest) SupportedFeatures() SupportedFeatures {
return SupportedFeatures{
SplitByFile: true,
SplitByExample: false,
FilterTestFiles: true,
AutoRetry: true,
Mute: true,
Skip: false,
}
}Main motivations being that a) this (anti) pattern of switching by runner name is starting to proliferate, b) to give us a way to support config validation like you have implemented here, and c) to give us a way to build the feature support matrix in the README.md from the code base.
Long winded way of saying this is 👍 but I may sweep back to update it some time soonish 😅
| TestCommand: "rspec", | ||
| }, | ||
| }) | ||
|
|
There was a problem hiding this comment.
🤔 why these whitespace removals?
There was a problem hiding this comment.
It seems the linter in my editor is doing this! I'll see if I can track down which one
There was a problem hiding this comment.
gofumpt maybe? If so we should discuss whether or not we want gofumpt on this project and reformat everything wholesale in one commit if we do.
There was a problem hiding this comment.
Yep it was gofumpt it was the default formatter in my editor config.
We had an internal discussion about using gofumpt wholesale across all BK Go projects last year and the consensus was mostly in agreement.
https://buildkite-corp.slack.com/archives/C05Q8CCJEUC/p1763597169525229
| // getPythonPackageVersion retrieves the version of a Python package using importlib.metadata. | ||
| // The pkgName should use hyphens (e.g., "buildkite-test-collector") as that's the package name in metadata. | ||
| func getPythonPackageVersion(pkgName string) (string, error) { | ||
| pythonCmd := exec.Command("python", "-c", "import importlib.metadata, sys; print(importlib.metadata.version(sys.argv[1]))", pkgName) |
There was a problem hiding this comment.
Just a note. I think this will be OK but there may be configurations where somehow the test suite runs in a different venv to the environment bktec is running in and this check fails.
I think it's unlikely, but it would be a blocker and we would need to cut a new bktec release to fix. So we could maybe consider logging an error rather than failing outright here.
There was a problem hiding this comment.
Logging an error and ignoring the --filter-tags option seems like the safer option. I'll merge this as is and look at refactoring the code in a separate PR.
There was a problem hiding this comment.
|
I've added a commit to bump the version of the Python Test Collector. And as expected, the CI build now passes. :) |
| // that are slow or contain skipped tests. These files are then split into examples | ||
| // The remaining files are sent as is. | ||
| // | ||
| // If tag filtering is enabled, all files are split into examples to support filtering. |
Description
Add a new --tag-filters CLI flag that allows filtering tests by execution
tags. This is useful for selectively running subsets of tests based on
key-value tag expressions.
Only Pytest is currently supported, using the
--tag-filtersflagprovided by the Python Test Collector plugin (v1.2.0+) [1]. Tests must be
decorated with
@pytest.mark.execution_tag("key", "value")markers to befiltered by tags.
Tag filtering only works when server-side test filtering is disabled (i.e.,
when all files are returned), ensuring that all tests are available for
tag-based selection.
Example usage:
bktec run --tag-filters "speed:slow"
BUILDKITE_TEST_ENGINE_TAG_FILTERS="speed:fast" bktec run
Please don't confuse tag filtering with the existing filtering
functionality which relies on server-side 'filtering' to return a list of
slow files.
Context
[1] buildkite/test-collector-python#83
https://linear.app/buildkite/issue/TE-5134/add-tag-filters-option-to-bktec
#418
Changes