My Python project is deployed as a container, and also has unit tests. For Docker best-practice, the final image is supposed to exclude any dependencies that are only used for testing (like pytest for example). How should I exclude test dependencies while still running the tests?
One issue is that if I build my test environment independently from my deployment image, then those two environments may end up containing different versions of sub-dependencies. (Hypothetically if my project uses rasterio, and rasterio and pytest both use attrs, then installing pytest could alter which version of attrs is installed by the package manager. This suggests a bug could appear in the deployment image, without being present in the test environment.) If test dependencies are included in the final image, then the CI can be set up with successive steps like docker-compose build, docker-compose up -d and docker-compose exec myimage pytest. If testing dependencies are not present in the final container environment, how should the CI workflow be arranged? (E.g., if I used a multi-stage docker build and ran unit tests before the second stage, would I need to manually maintain a list of sub-dependencies to carry into the final stage?)