<#21933 Patterns for Python integration tests (e.g...
# github-notifications
c
#21933 Patterns for Python integration tests (e.g. w/ Postgres database) New discussion created by peter-woyzbun I have some Django applications I am looking to onboard to a monorepo that uses Pants. These applications depend on Postgres-specific database features, e.g. triggers, and so tests require a running Postgres database (vs. just using SQLite). Ideally these tests could run within Pants/hermetically - i.e. without having to run something like
docker-compose
locally before running
pants test ::
(and equivalent in GitHub workflows/CI) to make a Postgres database available. Does this seem generally possible? Is there any official stance on support (or planned support) for patterns like this? My first thought was that Pants has support for running Docker images, and for running Python tests, so maybe a plugin would work. The idea would be to have something like:
Copy code
python_tests(
    dependencies = [
        ":test_utils",
    ],
    extra_env_vars = [
        "DJANGO_SETTINGS_MODULE=tests.django_settings",
        "DATABASE": "my-database"
    ],
    docker_services=[
        {"image": "<registry>/postgres:15.8", "docker_env": {"POSTGRES_DB": "my-database"}, "run_args": "...", "port": 5432}
    ]
)
with the complications being that: 1. Port mappings from the host to the running container would likely need to be dynamic, to prevent port clashes between parallel test runs from multiple
python_tests
targets requiring
docker_services
(e.g. if I have two
python_tests
targets, both requiring a Postgres database, we might need two databases running in parallel). 2. The Docker services would need to be running before tests start to run, and then torn down afterwards. Any guidance or suggestions on how to support these types of integration tests would be appreciated. pantsbuild/pants