CloverHealth / pytest-pgsql

Clean PostgreSQL Databases for Your Tests
http://pytest-pgsql.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
65 stars 4 forks source link

Live database support #13

Open UriCW opened 4 years ago

UriCW commented 4 years ago

To the best of my understanding, this "magically" uses the Postgresql executable to create a separate database. This works great for testing on my machine, but when I want to run these test on my CI environment (using gitlab's ci/cd toolchain) I run into a problem.

I either need to install postgresql on the docker image the tests run on or use a pre installed postgresql image. further when I try these approaches I run into other problems, for example it doesn't like that the tests are running as root.

Ideally, I could still have a way to use a live database, which you can setup and demolish using "services": postgres:latest in the .gitlab-ci.yml file as part of the normal CI pipeline.

For the time being, I will probably add a fixture to conftest.py that checks some environment variables and either returns a real postgresql db context or a pytest-psql postgresql_db context depending on the environment.

Would be cool if i could just set some options to pass a real database connection details instead.

Apologies if this is already possible I couldn't find out how.

ash211 commented 4 years ago

I was very interested in this also. I think I have it working with this conftest.py setting:

import pytest
import os

if "TEST_POSTGRESQL_URI" in os.environ:
    @pytest.fixture(scope='session')
    def database_uri():
        return os.getenv("TEST_POSTGRESQL_URI")

Setting that environment variable to postgresql://postgres@127.0.0.1:5432/test seems to use the existing database as expected, vs creating a new one.

Does this work for you too?

jannismain commented 4 years ago

@ash211 Thank you very much for your comment :-) This helped me move forward using an existing database for testing, while I'm waiting on input on #20

nayyarv commented 4 years ago

@ash211 I can also can say this worked well for me and is worth official support as a PR.

Within gitlab's services infra: https://docs.gitlab.com/ee/ci/services/postgres.html, I found setting the hostname to postgres like to postgresql://postgres@postgres:5432/testwas also good.