fierce-greece-10087
08/05/2025, 12:18 PMimport pytest
def pytest_generate_tests(metafunc):
data = zip(range(5), range(5)) # imagine this to be very complex
metafunc.parametrize("x, y", [pytest.param(x, y, id=f"test_{x}_{y}") for x, y in data])
def test_my_stuff(x: int, y: int):
assert x <= y # each such test takes about 1 minute to complete
If I run this with pants
, it will run all generated tests in a single process serially. Can I somehow convince pants
to run the generated test cases in parallel?elegant-florist-94385
08/05/2025, 12:26 PM#test_file_A.py
def pytest_generate_tests(metafunc):
data = zip(range(2), range(5))
...
#test_file_B.py
def pytest_generate_tests(metafunc):
data = zip(range(3, 4), range(5))
...
etc. This would get pants to run each file as a separate process (you get some separation, but not so fine-grained that you lose performance due to excessive sandbox setup).
3. Test sharding. Usually this is used more for splitting your tests across CI machines, so I'm not sure if you can use it to parallelize on one machine, but a starting point is: https://www.pantsbuild.org/prerelease/reference/goals/test#shardfierce-greece-10087
08/05/2025, 2:06 PMpython_tests
, we'd have some custom_python_tests
thing, which would take care of that? Would that be possible?elegant-florist-94385
08/05/2025, 2:13 PMparametrize
feature with the extra_env_vars
field on your python_tests
, and then you provide a list of different env var values to be used with the test file. Then instead of having multiple files, you can have 1 file that controls the pytest_generate_tests
mechanism with the env var values. Each parametrized variant will get cached independently, and you have full control over how you want to break down the vars into the individual testselegant-florist-94385
08/05/2025, 2:55 PMfierce-greece-10087
08/06/2025, 9:42 AMelegant-florist-94385
08/06/2025, 10:56 AMpython_test
targets, so that they can be cached independently. I would guess something like this should work well:
# BUILD
test_options = {f"params_{x}_{y}": ("PARAM_X={x}", "PARAM_Y={y})" for x in range(5) for y in range(5)
python_test(
source="test_file.py",
extra_env_vars=parametrize(test_options),
)
# test_file.py
import os
def test_my_stuff():
x = os.getenv("PARAM_X")
y = os.getenv("PARAM_Y")
assert x <= y
elegant-florist-94385
08/06/2025, 11:08 AMpython_image
to create a docker_image
target that pre-fills some of the arguments so that I can make all my images use the same base image). Your case is essentially wanting to have a lot of python_test
targets that that have some common behavior (eg. the same source file), so my first thought was a macro. In your particular case though, parametrize
is a tool already made for covering this case, when you don't need to do a lot of customization to your targets.elegant-florist-94385
08/06/2025, 11:09 AMextra_env_vars
field on my tests) to run all of my tests in multiple configurations of feature flags.fierce-greece-10087
08/11/2025, 8:33 AMelegant-florist-94385
08/11/2025, 9:57 AM