pytest-modal 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. pytest_modal-0.0.1/.gitignore +5 -0
  2. pytest_modal-0.0.1/CHANGELOG.md +38 -0
  3. pytest_modal-0.0.1/LICENSE +202 -0
  4. pytest_modal-0.0.1/PKG-INFO +302 -0
  5. pytest_modal-0.0.1/README.md +274 -0
  6. pytest_modal-0.0.1/example/conftest.py +10 -0
  7. pytest_modal-0.0.1/example/test_mixed.py +23 -0
  8. pytest_modal-0.0.1/example/test_wait_a.py +8 -0
  9. pytest_modal-0.0.1/example/test_wait_b.py +8 -0
  10. pytest_modal-0.0.1/example/test_wait_c.py +8 -0
  11. pytest_modal-0.0.1/example/test_wait_d.py +8 -0
  12. pytest_modal-0.0.1/pyproject.toml +50 -0
  13. pytest_modal-0.0.1/src/pytest_modal/__init__.py +0 -0
  14. pytest_modal-0.0.1/src/pytest_modal/_common.py +107 -0
  15. pytest_modal-0.0.1/src/pytest_modal/backends.py +301 -0
  16. pytest_modal-0.0.1/src/pytest_modal/hookspecs.py +11 -0
  17. pytest_modal-0.0.1/src/pytest_modal/plan.py +63 -0
  18. pytest_modal-0.0.1/src/pytest_modal/plugin.py +561 -0
  19. pytest_modal-0.0.1/src/pytest_modal/worker.py +128 -0
  20. pytest_modal-0.0.1/tests/conftest.py +11 -0
  21. pytest_modal-0.0.1/tests/live/conftest.py +51 -0
  22. pytest_modal-0.0.1/tests/live/test_fork.py +90 -0
  23. pytest_modal-0.0.1/tests/live/test_lifetime.py +63 -0
  24. pytest_modal-0.0.1/tests/live/test_upload.py +81 -0
  25. pytest_modal-0.0.1/tests/test_backends.py +156 -0
  26. pytest_modal-0.0.1/tests/test_collect.py +101 -0
  27. pytest_modal-0.0.1/tests/test_coverage.py +54 -0
  28. pytest_modal-0.0.1/tests/test_failures.py +258 -0
  29. pytest_modal-0.0.1/tests/test_fixtures.py +80 -0
  30. pytest_modal-0.0.1/tests/test_image.py +43 -0
  31. pytest_modal-0.0.1/tests/test_plan.py +76 -0
  32. pytest_modal-0.0.1/tests/test_plugin.py +220 -0
  33. pytest_modal-0.0.1/tests/test_weights.py +52 -0
@@ -0,0 +1,5 @@
1
+ .venv/
2
+ __pycache__/
3
+ .pytest_cache/
4
+ .ruff_cache/
5
+ dist/
@@ -0,0 +1,38 @@
1
+ # Changelog
2
+
3
+ What changed in each release of pytest-modal, newest first. While the version starts with 0, any release
4
+ may change options and behaviour.
5
+
6
+ ## 0.0.1
7
+
8
+ The first public alpha.
9
+
10
+ - `pytest --modal` splits a session's tests into shards, runs each shard in a Modal sandbox and reports
11
+ through pytest's own hooks, so the terminal output, the exit code, `-k`, `-m`, `-x`, `--lf`,
12
+ `--junitxml`, `--cov` and the warnings summary behave as in a plain run.
13
+ - The sandboxes get the project by upload, with a default image that mirrors the packages of the local
14
+ environment, from a snapshot of the sandbox pytest runs in, or from a sandbox or image named by id
15
+ (`--modal-from`).
16
+ - `--modal-shards=auto` sizes the run from the time each test took before; `--modal-group-by=test`
17
+ splits inside files; `modal_isolate` and `xdist_group` keep tests together. The times are kept in
18
+ `.pytest_cache` and in a Modal Dict, for machines that keep no cache between runs; there they are named
19
+ after the repository the project was cloned from (`modal_project` names them otherwise).
20
+ - A number given to `--modal-shards` is used as given, up to a sandbox for each test; auto asks for at
21
+ most 1,024 (`--modal-max-shards`) and fills a shard to no less than one costs to start. Shards start 128
22
+ at a time, the heaviest first. One that Modal has no room for waits for another of the run to finish,
23
+ and one far slower to start than the rest is replaced.
24
+ - With `-s`, what a test wrote is shown when the test finishes. `--pdb` and `--trace` are refused.
25
+ - `--junitxml`, `--basetemp` and `--cov-report` name files on the machine pytest was started on, and are
26
+ honoured there, once.
27
+ - The summary gives the run in a line and, when one file took most of it, names the file and the option
28
+ that splits it.
29
+ - A run stops with a usage error, before collecting, when it has no Modal credentials, and when the shards
30
+ run another pytest version than the controller.
31
+ - Each run is one app, whose id is printed when it starts; its sandboxes are tagged and its disk snapshot
32
+ is deleted when it ends.
33
+ - `--modal-collect=remote` lists the tests where they run, so the controller imports no test code.
34
+ - The controller signs in before it imports the project and keeps the token out of the shards.
35
+ - A sandbox that dies fails the test it was running and the rest of its shard runs elsewhere; Modal stops
36
+ the sandboxes of a controller that died.
37
+ - `--modal-backend=local` runs the shards as local subprocesses, with no Modal account.
38
+ - Requires Python 3.10 to 3.15, pytest 8.0 or later and modal 1.6.2 or later.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,302 @@
1
+ Metadata-Version: 2.5
2
+ Name: pytest-modal
3
+ Version: 0.0.1
4
+ Summary: Run a pytest session's tests across Modal sandboxes
5
+ Project-URL: Homepage, https://github.com/wikipika-modal/pytest-modal
6
+ Project-URL: Documentation, https://github.com/wikipika-modal/pytest-modal#readme
7
+ Project-URL: Changelog, https://github.com/wikipika-modal/pytest-modal/blob/main/CHANGELOG.md
8
+ Project-URL: Issues, https://github.com/wikipika-modal/pytest-modal/issues
9
+ Author: Modal Labs
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: distributed,modal,parallel,pytest,sandbox,testing
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Framework :: Pytest
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Programming Language :: Python :: 3.15
23
+ Classifier: Topic :: Software Development :: Testing
24
+ Requires-Python: >=3.10
25
+ Requires-Dist: modal>=1.6.2
26
+ Requires-Dist: pytest>=8
27
+ Description-Content-Type: text/markdown
28
+
29
+ # pytest-modal
30
+
31
+ Run a pytest session's tests across [Modal](https://modal.com) sandboxes by adding `--modal` to the
32
+ command you already run. An alpha: it works end to end, and its options and behaviour may still change.
33
+
34
+ ## Quickstart
35
+
36
+ You need Python 3.10 or later, a project whose tests run with `pytest`, and a
37
+ [Modal account](https://modal.com/signup).
38
+
39
+ ```
40
+ pip install pytest-modal # into the environment you run pytest from
41
+ modal setup # once: opens a browser and stores a token on this machine
42
+ pytest --modal
43
+ ```
44
+
45
+ The first run builds an image holding the packages of that environment, then starts the sandboxes and
46
+ reports each test as it finishes. For a project of 20 one-second tests in four files:
47
+
48
+ ```
49
+ $ pytest --modal
50
+ ============================= test session starts ==============================
51
+ platform linux -- Python 3.12.16, pytest-9.2.0, pluggy-1.7.0
52
+ modal: modal backend
53
+ rootdir: /home/you/project
54
+ plugins: modal-0.0.1, anyio-4.16.0
55
+ collected 20 items
56
+ modal: preparing the image the shards start from
57
+ modal: app ap-...
58
+ .................... [100%]
59
+ modal: 4 shards, 7.1s wall, 20.0s of tests, start 5.7s, shards up in 1.7s (slowest 1.8s), app ap-...
60
+ modal: test_b.py took 5.0s of the 7.1s; --modal-group-by=test would split its tests over sandboxes
61
+ ============================= 20 passed in 13.03s ==============================
62
+ ```
63
+
64
+ The last two `modal:` lines are the run in brief: four sandboxes ran 20.0 s of tests in 7.1 s, after 5.7 s
65
+ spent on the image (20.8 s for an environment none of whose packages Modal had seen), and one file of
66
+ five tests is as long as the whole run. The image is kept, so the next run of the same project ends with
67
+ `20 passed in 7.74s`, where plain `pytest` takes 20.03 s. Sandboxes are billed as any other use of Modal
68
+ is.
69
+
70
+ To try it without an account, `pytest --modal --modal-backend=local` shards over local subprocesses
71
+ instead. The same command takes these options, which the sections below explain:
72
+
73
+ ```
74
+ pytest --modal # as many sandboxes as shorten the run
75
+ pytest --modal --modal-shards=8
76
+ pytest --modal --modal-group-by=test # split inside files too
77
+ pytest --modal --modal-collect=remote # this process imports no test code
78
+ pytest --modal --modal-backend=local # subprocesses instead of sandboxes, needs no Modal account
79
+ ```
80
+
81
+ The terminal output, the exit code, `-k`, `-m`, `-x`, `--lf`, `--junitxml`, `--cov` and the warnings
82
+ summary behave as in a plain run.
83
+
84
+ ## How it works
85
+
86
+ 1. The pytest process you start is the controller. It lists the tests and runs none.
87
+ 2. It splits the tests into shards. A file stays in one shard unless `--modal-group-by=test`; tests sharing
88
+ an `xdist_group` mark stay together; a shard runs its tests in the order a plain run would.
89
+ 3. Each shard is one sandbox. It runs pytest on its tests and writes every report to stdout as a JSON line.
90
+ 4. The controller feeds those reports to pytest's own report hooks, so the terminal reporter, junitxml,
91
+ pytest-cov and other reporting plugins see them as if the tests had run in the controller.
92
+
93
+ ### Where the sandboxes get the project
94
+
95
+ - **Upload** (the default outside Modal): an image plus the project directory at the same absolute path.
96
+ The default image is `debian_slim` with this Python minor version and the packages of the environment
97
+ pytest runs in, at the same versions; a package installed from a path (`pip install -e .`) goes up as
98
+ it is installed. Return another image from `pytest_modal_image` when that does not fit: system
99
+ packages, a private index, packages that have no Linux build.
100
+ - **Disk** (the default inside a Modal sandbox): a snapshot of the sandbox the controller runs in. A runner
101
+ sandbox installs and builds as usual, then `pytest --modal` fans the tests out to copies of its disk, with
102
+ the same paths, working directory and environment, less the variables starting with `MODAL_`.
103
+ - **A sandbox or image named by id** (`--modal-from=sb-...` or `im-...`): the controller runs somewhere
104
+ else, snapshots that sandbox (or takes that image) and starts the shards from it, in the directory given
105
+ by `--modal-workdir`. Collection is remote, the sandbox needs pytest and neither this plugin nor a Modal
106
+ token, and the controller needs none of the project: `pip install pytest-modal` and the same pytest
107
+ version are enough.
108
+
109
+ `--modal-from=upload|disk|ID` overrides the choice.
110
+
111
+ ### How many sandboxes
112
+
113
+ No number of sandboxes finishes before the heaviest group (a file, by default) does. `--modal-shards=auto`
114
+ fills shards to half that group's weight, and to no less than a shard was measured to cost besides its
115
+ tests (a sandbox and an interpreter to start, files to collect), up to `--modal-max-shards`. A number given
116
+ to `--modal-shards` is used as given, however large; shards start 128 at a time, the heaviest first, and
117
+ the summary line says how long the last one waited. Weights are the wall time each test cost its shard in
118
+ earlier runs; a test never seen weighs the median. They are kept in `.pytest_cache` and, for machines that
119
+ keep no cache between runs, in a Modal Dict named `pytest-modal` in the environment the run uses. The entry
120
+ is named after where the repository was cloned from (`github.com/you/project`, without credentials), so
121
+ checkouts on different machines share it; outside a repository, after the root directory, and
122
+ `modal_project` names it otherwise. A run of the whole suite replaces what was kept; a run of part of it
123
+ adds to it.
124
+
125
+ ### What a run leaves on Modal
126
+
127
+ - One app per run, named `pytest-modal` (`--modal-app`). It is ephemeral, which is what lets Modal stop the
128
+ sandboxes of a controller that died, and like the app of a `modal run` it stays listed as stopped. Its
129
+ id is printed when the app starts (`modal: app ap-...`) and again in the summary line;
130
+ `modal app stop ap-...` ends a run from elsewhere.
131
+ - Sandboxes tagged `pytest-modal=<app name>` and `pytest-modal-shard=<modalN or collect>`, besides the tags
132
+ your `pytest_modal_sandbox` hook returns: `modal.Sandbox.list(app_id="ap-...", tags={"pytest-modal-shard":
133
+ "modal3"})` is the sandbox of one shard.
134
+ - The disk snapshot of a run is deleted when the run ends, and expires after `modal_timeout` if the
135
+ controller died first.
136
+ - The Dict of weights; an entry expires after a week without use.
137
+
138
+ ## Credentials
139
+
140
+ The controller is the only process that talks to Modal. It needs to create an app, build an image and
141
+ create sandboxes: a Contributor in one environment.
142
+
143
+ - **Your machine**: the profile `modal token new` wrote.
144
+ - **CI**: `MODAL_TOKEN_ID` and `MODAL_TOKEN_SECRET` in the job's environment, and `MODAL_ENVIRONMENT`. Use
145
+ a [service user](https://modal.com/docs/guide/service-users) that is Contributor in an environment kept
146
+ for test runs, created with an expiry if it should not live long
147
+ (`modal workspace service-users create NAME --expires-in 90d`), and not a person's token.
148
+ - **Inside a Modal sandbox**: a sandbox has no Modal identity of its own. Either pass the two variables to
149
+ the `pytest --modal` command alone (`sandbox.exec(..., secrets=[...])`), or keep the token out of the
150
+ sandbox altogether: run `pytest --modal --modal-from=<sandbox id>` from the process that created it.
151
+
152
+ The controller signs in before it imports any of the project, then removes the token from its own
153
+ environment. Shards never receive it, so tests cannot read it. Code that runs in the same sandbox as the
154
+ controller (a `conftest.py` it imports during local collection, a build step that left a process behind) is
155
+ not kept from it by this: for code you do not trust, use `--modal-from=<sandbox id>`.
156
+
157
+ ## Listing the tests
158
+
159
+ - `--modal-collect=local` (default): the controller collects as pytest always does, so the tests' imports
160
+ must work where it runs.
161
+ - `--modal-collect=remote`: the interpreter that runs the tests lists them, in a sandbox, or in a
162
+ subprocess while the disk snapshot is taken when the shards start from this disk. The controller loads no
163
+ `conftest.py` and imports no test module, and can be a different interpreter from the one in the shards
164
+ (`modal_python`). Options a `conftest.py` defines go through `--modal-args="--runslow"`.
165
+
166
+ ## Configuration
167
+
168
+ Options: `--modal-shards`, `--modal-max-shards` (1024, or 64 local processes), `--modal-group-by=file|test`,
169
+ `--modal-collect=local|remote`, `--modal-from=auto|upload|disk|ID`, `--modal-workdir`, `--modal-retries` (2),
170
+ `--modal-args`, `--modal-backend=modal|local`, `--modal-app`.
171
+
172
+ Ini values:
173
+
174
+ | Name | Meaning |
175
+ | --- | --- |
176
+ | `modal_isolate` | Each line: patterns for test files that run together in a sandbox of their own |
177
+ | `modal_python` | Interpreter the shards run |
178
+ | `modal_project` | Name the weights are kept under on Modal |
179
+ | `modal_conf` | File with the hooks below (`modal_conf.py`) |
180
+ | `modal_timeout` | Seconds a sandbox may live (3600) |
181
+ | `modal_start_timeout` | Seconds a shard may take to start before it is replaced (300); see below |
182
+
183
+ `modal_isolate` is for order dependencies a suite has and does not declare: files that pass only in a
184
+ process that also loaded another file go on one line, and a file that fails after certain others goes on a
185
+ line by itself.
186
+
187
+ Hooks go in `modal_conf.py` next to the pytest configuration. Only the controller loads it, so it can
188
+ import `modal`:
189
+
190
+ ```python
191
+ import modal
192
+
193
+
194
+ def pytest_modal_image(config): # upload only
195
+ return modal.Image.debian_slim(python_version="3.12").pip_install("pytest==9.2.0", "numpy")
196
+
197
+
198
+ def pytest_modal_sandbox(config): # keyword arguments for modal.Sandbox.create
199
+ return {"cpu": (0.125, 8), "gpu": "T4"}
200
+ ```
201
+
202
+ ## What a shard changes for the tests
203
+
204
+ - Session-scoped fixtures set up and tear down once per sandbox. Module-scoped fixtures run once per file
205
+ while files stay whole. `PYTEST_MODAL_SHARD` and `PYTEST_MODAL_SHARDS` tell a fixture where it is.
206
+ - Coverage: with `--cov`, each shard's data comes back and pytest-cov combines it; the report and
207
+ `--cov-fail-under` use the combined result.
208
+ - A file skipped as a whole and warnings raised in a sandbox are reported once, as in a plain run.
209
+
210
+ ## When something goes wrong
211
+
212
+ | Event | Result |
213
+ | --- | --- |
214
+ | A sandbox dies while running a test | That test fails with the sandbox's last output; the rest of the shard runs in a new sandbox |
215
+ | A sandbox dies or fails to start before running anything | The shard is retried, `--modal-retries` times |
216
+ | Modal has no room for another sandbox (a limit on how many the workspace runs) | The shard waits, using no retry, and starts when a sandbox of the run ends |
217
+ | A sandbox does not start within `modal_start_timeout`, or its first start takes ten times as long as most of the run's and more than 15 s | It is replaced |
218
+ | A shard does not collect a test the controller listed | That test is an error |
219
+ | The controller is interrupted or terminated | It stops its sandboxes |
220
+ | The controller is killed outright | Modal stops the run's app and its sandboxes when the heartbeat lapses (about 3 minutes) |
221
+ | No Modal credentials, Modal cannot be reached, or the options do not fit | A usage error before anything is collected, no traceback |
222
+ | The shards run another pytest version than the controller | A usage error naming both |
223
+ | A shard cannot import a file the controller listed | That file's tests are errors; the shard runs its others |
224
+
225
+ ## Measured
226
+
227
+ `pytest example --modal`: 28 tests holding 24.2 s of test time, from a cloud VM, one run each.
228
+
229
+ | Shards | Wall |
230
+ | --- | --- |
231
+ | 5 (one per file), image not yet built | 14.3 s |
232
+ | 5 (one per file) | 7.8 s |
233
+ | 25 (`--modal-group-by=test`) | 4.7 s |
234
+
235
+ [mpmath](https://github.com/mpmath/mpmath)'s suite (2,765 tests with its doctests; pure Python; needs
236
+ hypothesis, pexpect, ipython and pytest-timeout), with no configuration for this plugin. Plain pytest takes
237
+ 560 s on the same cloud VM and 576 s in a sandbox. One run each:
238
+
239
+ | Where the shards get the project | Run | Shards | Wall |
240
+ | --- | --- | --- | --- |
241
+ | Upload | first: no weights, image built (11 s) | 4 | 210 s |
242
+ | Upload | later: kept weights | 9 | 156 s |
243
+ | Upload | `--modal-group-by=test` | 42 | 40 s |
244
+ | Disk, pytest inside a runner sandbox | first | 4 | 232 s |
245
+ | Disk | later | 8 | 192 s |
246
+ | Disk | `--modal-group-by=test` | 28 | 61 s |
247
+ | `--modal-from=<sandbox id>`, pytest outside it | `--modal-group-by=test` | 64 | 44 s |
248
+
249
+ In each row every test has the outcome it has in a plain run (the junit files compared test by test), also
250
+ with `--modal-collect=remote`, `--cov`, `-k`, `-x`, `--lf` and a failing test. With files kept whole the
251
+ run is as long as the suite's longest file, 136 s here, which is why `--modal-group-by=test` is four times
252
+ faster. Coverage came to the same 21,845 statements; 5 lines differed, all reached only by tests that draw
253
+ unseeded random numbers.
254
+
255
+ The same suite with `--modal-group-by=test` and the shard count given, up to a sandbox for each test. One
256
+ test of 44 s bounds every one of these runs (31 to 53 s), so the columns are what the count changes. Upload;
257
+ a disk snapshot gives much the same.
258
+
259
+ | Shards | A shard is up in (median) | The last one is up after | Controller CPU | Sandbox-seconds |
260
+ | --- | --- | --- | --- | --- |
261
+ | 64 | 1.3 s | 4.3 s | 2.9 s | 1,158 |
262
+ | 128 | 1.3 s | 2.7 s | 3.4 s | 1,424 |
263
+ | 256 | 1.4 s | 3.7 s | 4.7 s | 2,001 |
264
+ | 512 | 1.6 s | 5.2 s | 6.8 s | 2,603 |
265
+ | 1,024 | 1.8 s | 13.9 s | 12.0 s | 4,643 |
266
+ | 2,765 (one per test) | 2.0 s | 30.4 s | 30.8 s | 9,699 |
267
+
268
+ Every run reports what a plain run does, test by test, with no shard restarted and nothing refused by
269
+ Modal. The controller starts about 80 shards a second on one core, so past a few hundred the later ones
270
+ wait for their turn. A shard costs 3 to 5 s besides its tests, which is why sandbox-seconds grow with the
271
+ count. The controller keeps a thread and about 0.4 MB for each shard: 1.1 GB at 2,765.
272
+
273
+ pybind11's pytest suite (1,138 to 1,483 tests on each of six interpreters, C++ modules built in a runner
274
+ sandbox, shards from its disk, `--modal-collect=remote`, default shard count): every row reports the same
275
+ counts as a plain run. With a warm build the six rows finish in 15 to 21 s; a row's pytest step takes 5 to
276
+ 19 s where a plain run takes 14 to 129 s.
277
+
278
+ ## Development
279
+
280
+ - `pyproject.toml` registers `pytest_modal.plugin` under the `pytest11` entry point.
281
+ - `src/pytest_modal/plugin.py`: the controller. `worker.py`: the hooks loaded in each shard, needing only
282
+ pytest. `plan.py`: the shard plan. `backends.py`: sandboxes or subprocesses. `hookspecs.py`: the hooks.
283
+ - `uv run pytest` drives real pytest runs through `pytester` with the local backend.
284
+ - `PYTEST_MODAL_LIVE=1 uv run pytest tests/live` runs against Modal with the active profile: upload,
285
+ disk snapshots on both sandbox runtimes, a lost sandbox and a killed controller. The disk tests pass the
286
+ profile's token to the one `pytest` command inside their runner sandbox.
287
+
288
+ ## Not done
289
+
290
+ - With `-s`, what a test wrote is shown when that test finishes, not while it runs. `--pdb` and `--trace`
291
+ are refused: there is no terminal where the test runs.
292
+ - The controller is one process with a thread per shard and starts about 80 shards a second. Starting a
293
+ thousand in a second or two would take sandboxes driven from several processes.
294
+ - On a first run, with no weights, the auto shard count counts tests: a file holding half the tests gets
295
+ half the weight whatever it costs, and with `--modal-group-by=test` it asks for a sandbox for each test,
296
+ up to `--modal-max-shards`. Later runs plan from what the tests took.
297
+ - A shard collects the whole of each file it has a test from.
298
+ - With `--modal-from=ID` the controller has none of the project, so `--cov` reports need the sources at the
299
+ same paths where it runs, and options a plugin of the project defines go through `--modal-args`.
300
+ - The default image cannot mirror what an index does not have for Linux; such environments need the
301
+ `pytest_modal_image` hook.
302
+ - The controller is tested on Linux and on macOS (arm64), not on Windows; sandboxes are Linux.