Skip to content

feat: implement pytest_test rule#3931

Open
rickeylev wants to merge 2 commits into
bazel-contrib:mainfrom
rickeylev:implement-pytest-main-gen
Open

feat: implement pytest_test rule#3931
rickeylev wants to merge 2 commits into
bazel-contrib:mainfrom
rickeylev:implement-pytest-main-gen

Conversation

@rickeylev

Copy link
Copy Markdown
Collaborator

This implements a pytest_test rule based upon the pytest_bazel library.

The core implementation of this is fairly simple: a file is generated that calls
pytest.main() with the srcs to test. This generated file is the file that is
run by py_test.

DO NOT MERGE: Needs discussion. This was quickly thrown together. Haven't put any thought into how to handle conf.py or other pytest flags/settings. Works in the simple example, but haven't vetted it much.

@rickeylev
rickeylev requested a review from aignas as a code owner July 16, 2026 20:21
@rickeylev rickeylev added the do not merge Tag that prevents merging label Jul 16, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new pytest_test rule to support running pytest tests under Bazel, along with corresponding dependency updates and integration tests. Feedback on the changes highlights several critical issues: calling pytest_bazel.main in the bootstrap template will cause an AttributeError as pytest.main should be called instead; the config_settings attribute is unsupported in pytest_test and will cause a loading-phase error; the "pypi" repository is not exported by dev_pip in MODULE.bazel; list concatenation with srcs in pytest_test.bzl will fail if srcs is a select(); template expansion can be simplified by avoiding computed_substitutions; and the bzl_library target for pytest_test is missing its srcs attribute.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread tests/support/pytest_test/pytest_bootstrap_template.py
Comment thread tests/pytest_test/BUILD.bazel
Comment thread MODULE.bazel
py_test(
name = name,
main = main_file,
srcs = [bootstrap_target] + srcs,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

If srcs is a select(), concatenating it as [bootstrap_target] + srcs will fail with a Starlark evaluation error because list-plus-select concatenation is not supported (only select-plus-list is supported). Reversing the order to srcs + [bootstrap_target] resolves this and makes the rule compatible with select()s.

Suggested change
srcs = [bootstrap_target] + srcs,
srcs = srcs + [bootstrap_target],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The bootstrap is not needed, we can just use the main_module that already exists in the py_test rule.

Comment on lines +57 to +71
computed_subs = ctx.actions.template_dict()

computed_subs.add_joined(
"%TEST_FILES%",
depset(ctx.files.srcs),
join_with = "\n",
map_each = _map_file,
)

ctx.actions.expand_template(
output = output,
template = ctx.file._bootstrap_template,
substitutions = {},
computed_substitutions = computed_subs,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since ctx.files.srcs is already a flat list of files available at analysis time, using computed_substitutions and template_dict is unnecessary and adds extra complexity. We can simplify this by using standard substitutions with a simple string join, which is also more compatible with older Bazel versions.

    ctx.actions.expand_template(
        output = output,
        template = ctx.file._bootstrap_template,
        substitutions = {
            "%TEST_FILES%": "\n".join([f.short_path for f in ctx.files.srcs]),
        },
    )

Comment thread tests/support/pytest_test/BUILD.bazel

@aignas aignas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I need a little bit of time to think how:

  • We can handle sharding related optional dependencies.
  • We can handle other features that may require extra plugins.

Since pytest_bazel is its own repo, either we have to migrate the code to rules_python or migrate the rule imple to pytest_bazel and then proxy it in rules_python. Let me think about it a little.

Other ideas/thoughts here are welcome.

py_test(
name = name,
main = main_file,
srcs = [bootstrap_target] + srcs,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The bootstrap is not needed, we can just use the main_module that already exists in the py_test rule.

@rickeylev

Copy link
Copy Markdown
Collaborator Author

The bootstrap is not needed, we can just use the main_module that already exists in the py_test rule.

How so? I originally tried that, but it didn't work. I didn't see how it could work in practice -- it would have to somehow auto discover the subset of files to test, but it has no info about what that subset is.

Implements a new  rule to support running Python tests with pytest.
Exposes default pytest targets and includes basic tests.
@rickeylev
rickeylev force-pushed the implement-pytest-main-gen branch from b925c46 to 0dba5a1 Compare July 17, 2026 20:05
@rickeylev

Copy link
Copy Markdown
Collaborator Author

I need a little bit of time to think

That's OK with me, but: How about I move it to tests/support for now?

I'd like to port the tests for e.g. the release tool to pytest, but I'm somewhat blocked on having a functional rule for it. This would also help give us some ideas of what basic usage looks like.

@aignas

aignas commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

+1 on having it in support for some time.

@aignas

aignas commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

How so? I originally tried that, but it didn't work. I didn't see how it could work in practice -- it would have to somehow auto discover the subset of files to test, but it has no info about what that subset is.

It will discover all of the tests that are available in a particular sandbox. I remember those working at some point. And it should ignore any tests in the external dependencies.

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

Labels

do not merge Tag that prevents merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants