pdm-project / pdm-example-monorepo

An example of a monorepo managed by PDM
The Unlicense
30 stars 2 forks source link

How to invoke `pytest` from subprojects? #4

Open mttbernardini opened 1 month ago

mttbernardini commented 1 month ago

Assuming we use a shared venv from the top-folder where pdm.lock is located, how would you setup tests with pytest?

My understanding is that each subproject would have its own unit tests under <subproj>/tests and pytest configuration be in <subproj>/pyproject.toml.

Then, what would be the idiomatic way to invoke pytest?

Ideas?

sinogermany commented 1 month ago

@mttbernardini In your case, try

pdm run pytest -p <subproj>

I personally use a shell script, under ./bin/pdm.sh

ls packages | xargs -I % sh -c "pdm ${@} -p packages/%"

Then all you need to do is: ./bin/pdm.sh run pytest

mttbernardini commented 4 weeks ago

Hey @sinogermany, thanks for your suggestion. I tried as you mentioned, however running pdm with the -p switch fails to satisfy my premise:

Assuming we use a shared venv from the top-folder where pdm.lock is located

Running pdm run -p <subproj> pytest (note: params order matters, otherwise -p gets parsed by pytest rather than pdm) tells pdm to create a venv under <subproj>/.venv rather than using the top-level one (tested using v2.15.2).

So mostly my question boils down to: how to run tools through pdm with their CWD being <subproj>, but having pdm use the top-level .venv?

PS: I discovered you can also do pdm run pytest <subproj>/ and that will filter test discovery to the given directory only, however it will still use the top-level pyproject.toml or pytest.ini for configuration rather than <subproj>/pytest.ini. That may be an acceptable tradeoff if there's nothing to override, and that's the direction I'm following at the moment.