Skip to content

[TE-5134] Add --tag-filters option to filter tests by markers#426

Merged
gchan merged 9 commits intomainfrom
te-5134-add-tag-filters-option-to-bktec
Jan 27, 2026
Merged

[TE-5134] Add --tag-filters option to filter tests by markers#426
gchan merged 9 commits intomainfrom
te-5134-add-tag-filters-option-to-bktec

Conversation

@gchan
Copy link
Contributor

@gchan gchan commented Jan 23, 2026

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-filters flag
provided by the Python Test Collector plugin (v1.2.0+) [1]. Tests must be
decorated with @pytest.mark.execution_tag("key", "value") markers to be
filtered 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

  • Add --tag-filters flag and BUILDKITE_TEST_ENGINE_TAG_FILTERS env var
  • Add TagFilters field to Config struct
  • Update Pytest runner to pass tag filters to the Python Test Collector plugin via --tag-filters flag
  • Add version check to ensure buildkite-test-collector v1.2.0+ is installed when using tag filters
  • Add validation to ensure tag filtering only works with pytest
  • Ensure all pytest files are available for tag filtering (not just slow/skipped files)
  • Add comprehensive integration tests for --tag-filters option with pytest markers
  • Add uv to .tool-versions for local testing of unreleased python_test_collector changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@gchan gchan force-pushed the te-5134-add-tag-filters-option-to-bktec branch 2 times, most recently from 508fb29 to f5ea4ff Compare January 23, 2026 04:27
gchan added 4 commits January 26, 2026 10:47
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
@gchan gchan force-pushed the te-5134-add-tag-filters-option-to-bktec branch from f5ea4ff to 6e8faf5 Compare January 25, 2026 22:46
gchan added 3 commits January 26, 2026 14:33
…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 ...
@gchan gchan force-pushed the te-5134-add-tag-filters-option-to-bktec branch from 2a5ab29 to 1a7169a Compare January 26, 2026 04:08
@gchan
Copy link
Contributor Author

gchan commented Jan 26, 2026

The build will pass when we release a new version of the Python Test Collector buildkite/test-collector-python#83

@gchan gchan marked this pull request as ready for review January 26, 2026 04:10
@gchan gchan requested a review from a team as a code owner January 26, 2026 04:10
}
}

if c.TagFilters != "" && c.TestRunner != "pytest" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!

TestCommand: "rspec",
},
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 why these whitespace removals?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the linter in my editor is doing this! I'll see if I can track down which one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gchan
Copy link
Contributor Author

gchan commented Jan 27, 2026

I've added a commit to bump the version of the Python Test Collector. And as expected, the CI build now passes. :)

@gchan gchan merged commit 65b5995 into main Jan 27, 2026
1 check passed
@gchan gchan deleted the te-5134-add-tag-filters-option-to-bktec branch January 27, 2026 01:18
// 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants