Skip to content

Commit c271d3f

Browse files
authored
refactor(tools): migrate private release tool tests to pytest and pytest-mock (#3937)
Migrates release tool unit tests under `tests/tools/private/release/` to Pytest and Pytest-mock (`mocker` fixture). - Added `pytest-mock` dependency to `docs/pyproject.toml`, updated `docs/uv.lock` & `docs/requirements.txt`, and added `@pypi//pytest_mock` to release test helper `deps`. - Converted release test targets from standard unittest to pytest fixtures. - Refactored tests to use the standard `mocker` fixture from `pytest-mock`.
1 parent f0badb1 commit c271d3f

27 files changed

Lines changed: 3260 additions & 3528 deletions

AGENTS.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ because it interferes with code review comments.
4242
Follow the advice in `CONTRIBUTING.md` for PR descriptions. PR descriptions
4343
become the commit message upon merge.
4444

45+
### Python pytest conventions
46+
47+
* When registering pytest fixtures from helper modules in test files, use
48+
`pytest_plugins = ["<module_path>"]`.
49+
* Name fixture functions with a `fixture_` prefix (e.g. `def fixture_foo():`),
50+
and pass the public fixture name using the `name` parameter in
51+
`@pytest.fixture(name="foo")`.
52+
4553
### Starlark style
4654

4755
For doc strings, using triple quoted strings when the doc string is more than
@@ -98,7 +106,6 @@ def foo_test_suite(name):
98106
test_suite(name=name, tests=_tests)
99107
```
100108

101-
102109
#### Repository rules
103110

104111
The function argument `rctx` is a hint that the function is a repository rule,
@@ -157,7 +164,6 @@ This repository contains 3 Bazel bzlmod modules.
157164

158165
`tests/support/` contains utility code and helpers for testing.
159166

160-
161167
`python/config_settings/BUILD.bazel` contains build flags that are part of the
162168
public API. DO NOT add, remove, or modify these build flags unless specifically
163169
instructed to.

docs/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ dependencies = [
1919
"markupsafe",
2020
"pytest",
2121
"pytest-bazel",
22+
"pytest-mock",
2223
]

docs/requirements.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ myst-parser==3.0.1 ; python_full_version < '3.10' \
309309
--hash=sha256:6457aaa33a5d474aca678b8ead9b3dc298e89c68e67012e73146ea6fd54babf1 \
310310
--hash=sha256:88f0cb406cb363b077d176b51c476f62d60604d68a8dcdf4832e080441301a87
311311
# via rules-python-docs (docs/pyproject.toml)
312-
myst-parser==5.1.0 ; python_full_version == '3.10.*' \
313-
--hash=sha256:9c91c52b3cdb4d94a6506e4fab4e2f296c7623a0da0dcbe6de1565c3dad67a8a \
314-
--hash=sha256:ab69322dc6719dcc7f296479dbb70181b66df6ed315064f92dbc85c0e1bf2f02
312+
myst-parser==4.0.1 ; python_full_version == '3.10.*' \
313+
--hash=sha256:5cfea715e4f3574138aecbf7d54132296bfd72bb614d31168f48c477a830a7c4 \
314+
--hash=sha256:9134e88959ec3b5780aedf8a99680ea242869d012e8821db3126d427edc9c95d
315315
# via rules-python-docs (docs/pyproject.toml)
316316
myst-parser==5.1.0 ; python_full_version >= '3.11' \
317317
--hash=sha256:9c91c52b3cdb4d94a6506e4fab4e2f296c7623a0da0dcbe6de1565c3dad67a8a \
@@ -352,15 +352,21 @@ pytest==8.4.2 ; python_full_version < '3.10' \
352352
# via
353353
# rules-python-docs (docs/pyproject.toml)
354354
# pytest-bazel
355+
# pytest-mock
355356
pytest==9.1.1 ; python_full_version >= '3.10' \
356357
--hash=sha256:1088fbde8f2b49d95a549a195707afa7a76a3ce9bcadc26b6d71f0ffda5fe313 \
357358
--hash=sha256:37a86b45efb9a47a61a36449063e8e18d0cab3161329fc099eb21783169c4f0c
358359
# via
359360
# rules-python-docs (docs/pyproject.toml)
360361
# pytest-bazel
362+
# pytest-mock
361363
pytest-bazel==0.1.6 \
362364
--hash=sha256:a29e80e1d67c3db801bdd4d0b6b742f2bfb48cd6841caa33401458e5c4e29c21
363365
# via rules-python-docs (docs/pyproject.toml)
366+
pytest-mock==3.15.1 \
367+
--hash=sha256:0a25e2eb88fe5168d535041d09a4529a188176ae608a6d249ee65abc0949630d \
368+
--hash=sha256:1849a238f6f396da19762269de72cb1814ab44416fa73a8686deac10b0d87a0f
369+
# via rules-python-docs (docs/pyproject.toml)
364370
pyyaml==6.0.3 \
365371
--hash=sha256:00c4bdeba853cc34e7dd471f16b4114f4162dc03e6b7afcc2128711f0eca823c \
366372
--hash=sha256:0150219816b6a1fa26fb4699fb7daa9caf09eb1999f3b70fb6e786805e80375a \

docs/uv.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tools/private/release/BUILD.bazel

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,172 @@
1-
load("@rules_python//python:defs.bzl", "py_library", "py_test")
1+
load("//python:py_library.bzl", "py_library")
2+
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD")
3+
load("//tests/support/pytest_test:pytest_test.bzl", "pytest_test")
24

35
py_library(
46
name = "release_test_helper",
57
srcs = ["release_test_helper.py"],
8+
target_compatible_with = SUPPORTS_BZLMOD,
69
deps = [
710
"//tools/private/release:mock_gh",
811
"//tools/private/release:release_lib",
12+
"@pypi//pytest_mock",
913
],
1014
)
1115

12-
py_test(
16+
pytest_test(
1317
name = "add_backports_test",
1418
srcs = ["add_backports_test.py"],
19+
target_compatible_with = SUPPORTS_BZLMOD,
1520
deps = [
1621
":release_test_helper",
1722
"//tools/private/release:release_lib",
1823
],
1924
)
2025

21-
py_test(
26+
pytest_test(
2227
name = "changelog_news_test",
2328
srcs = ["changelog_news_test.py"],
29+
target_compatible_with = SUPPORTS_BZLMOD,
2430
deps = [
2531
":release_test_helper",
2632
"//tools/private/release:release_lib",
2733
],
2834
)
2935

30-
py_test(
36+
pytest_test(
3137
name = "complete_sync_changelog_test",
3238
srcs = ["complete_sync_changelog_test.py"],
39+
target_compatible_with = SUPPORTS_BZLMOD,
3340
deps = [
3441
":release_test_helper",
3542
"//tools/private/release:release_lib",
3643
],
3744
)
3845

39-
py_test(
40-
name = "create_rc_test",
41-
srcs = ["create_rc_test.py"],
46+
pytest_test(
47+
name = "create_release_branch_test",
48+
srcs = ["create_release_branch_test.py"],
49+
target_compatible_with = SUPPORTS_BZLMOD,
4250
deps = [
4351
":release_test_helper",
4452
"//tools/private/release:release_lib",
4553
],
4654
)
4755

48-
py_test(
49-
name = "create_release_branch_test",
50-
srcs = ["create_release_branch_test.py"],
56+
pytest_test(
57+
name = "git_test",
58+
srcs = ["git_test.py"],
59+
target_compatible_with = SUPPORTS_BZLMOD,
5160
deps = [
5261
":release_test_helper",
5362
"//tools/private/release:release_lib",
5463
],
5564
)
5665

57-
py_test(
58-
name = "gh_test",
59-
srcs = ["gh_test.py"],
66+
pytest_test(
67+
name = "on_pr_merged_test",
68+
srcs = ["on_pr_merged_test.py"],
69+
target_compatible_with = SUPPORTS_BZLMOD,
6070
deps = [
71+
":release_test_helper",
6172
"//tools/private/release:release_lib",
6273
],
6374
)
6475

65-
py_test(
66-
name = "git_test",
67-
srcs = ["git_test.py"],
76+
pytest_test(
77+
name = "promote_test",
78+
srcs = ["promote_test.py"],
79+
target_compatible_with = SUPPORTS_BZLMOD,
6880
deps = [
81+
":release_test_helper",
6982
"//tools/private/release:release_lib",
7083
],
7184
)
7285

73-
py_test(
74-
name = "on_pr_merged_test",
75-
srcs = ["on_pr_merged_test.py"],
86+
pytest_test(
87+
name = "release_issue_test",
88+
srcs = ["release_issue_test.py"],
89+
target_compatible_with = SUPPORTS_BZLMOD,
7690
deps = [
7791
":release_test_helper",
7892
"//tools/private/release:release_lib",
7993
],
8094
)
8195

82-
py_test(
83-
name = "prepare_test",
84-
srcs = ["prepare_test.py"],
96+
pytest_test(
97+
name = "release_test",
98+
srcs = ["release_test.py"],
99+
target_compatible_with = SUPPORTS_BZLMOD,
85100
deps = [
86101
":release_test_helper",
87102
"//tools/private/release:release_lib",
88103
],
89104
)
90105

91-
py_test(
92-
name = "process_backports_test",
93-
srcs = ["process_backports_test.py"],
106+
pytest_test(
107+
name = "backport_create_releases_test",
108+
srcs = ["backport_create_releases_test.py"],
109+
target_compatible_with = SUPPORTS_BZLMOD,
94110
deps = [
95111
":release_test_helper",
96112
"//tools/private/release:release_lib",
97113
],
98114
)
99115

100-
py_test(
101-
name = "promote_test",
102-
srcs = ["promote_test.py"],
116+
pytest_test(
117+
name = "prepare_test",
118+
srcs = ["prepare_test.py"],
119+
target_compatible_with = SUPPORTS_BZLMOD,
103120
deps = [
104121
":release_test_helper",
105122
"//tools/private/release:release_lib",
106123
],
107124
)
108125

109-
py_test(
110-
name = "release_issue_test",
111-
srcs = ["release_issue_test.py"],
126+
pytest_test(
127+
name = "gh_test",
128+
srcs = ["gh_test.py"],
129+
target_compatible_with = SUPPORTS_BZLMOD,
112130
deps = [
131+
":release_test_helper",
113132
"//tools/private/release:release_lib",
114133
],
115134
)
116135

117-
py_test(
118-
name = "release_test",
119-
srcs = ["release_test.py"],
136+
pytest_test(
137+
name = "backport_prepare_test",
138+
srcs = ["backport_prepare_test.py"],
139+
target_compatible_with = SUPPORTS_BZLMOD,
120140
deps = [
141+
":release_test_helper",
121142
"//tools/private/release:release_lib",
122143
],
123144
)
124145

125-
py_test(
126-
name = "utils_test",
127-
srcs = ["utils_test.py"],
146+
pytest_test(
147+
name = "process_backports_test",
148+
srcs = ["process_backports_test.py"],
149+
target_compatible_with = SUPPORTS_BZLMOD,
128150
deps = [
129151
":release_test_helper",
130152
"//tools/private/release:release_lib",
131-
"@dev_pip//packaging",
132153
],
133154
)
134155

135-
py_test(
136-
name = "backport_prepare_test",
137-
srcs = ["backport_prepare_test.py"],
156+
pytest_test(
157+
name = "create_rc_test",
158+
srcs = ["create_rc_test.py"],
159+
target_compatible_with = SUPPORTS_BZLMOD,
138160
deps = [
139161
":release_test_helper",
140162
"//tools/private/release:release_lib",
141163
],
142164
)
143165

144-
py_test(
145-
name = "backport_create_releases_test",
146-
srcs = ["backport_create_releases_test.py"],
166+
pytest_test(
167+
name = "utils_test",
168+
srcs = ["utils_test.py"],
169+
target_compatible_with = SUPPORTS_BZLMOD,
147170
deps = [
148171
":release_test_helper",
149172
"//tools/private/release:release_lib",

0 commit comments

Comments
 (0)