Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run pytest test suite against multiple database versions

Tags:

python

pytest

I build an application that uses a database in the backend. For integration tests, I start the database in Docker and run a test suite with pytest.

I use a session scoped fixture with autouse=True to start the Docker container:

@pytest.fixture(scope='session', autouse=True)
    def run_database():                     
        # setup code skipped ...

        # start container with docker-py
        container.start()

        # yield container to run tests
        yield container

        # stop container afterwards
        container.stop()

I pass the database connection to the test functions with another session scoped fixture:

@pytest.fixture(scope='session')
def connection():
    return Connection(...)

Now I can run a test function:

def test_something(connection):
    result = connection.run(...)
    assert result == 'abc'

However, I would like to run my test functions against multiple different versions of the database.

I could run multiple Docker containers in the run_database() fixture. How can I parametrize my test functions so that they run for two different connection() fixtures?

like image 793
Martin Preusse Avatar asked May 20 '26 11:05

Martin Preusse


1 Answers

The answer by @Guy works!

I found another solution for the problem. It is possible to parametrize a fixture. Every test function that uses the fixture will run multiple times: https://docs.pytest.org/en/latest/fixture.html#parametrizing-fixtures

Thus, I parametrized the connection() function:

@pytest.fixture(scope='session', params=['url_1', 'url_2'])
def connection(request):
    yield Connection(url=request.param)

Now every test function that uses the connection fixture runs twice. The advantage is that you do not have to change/adapt/mark the existing test functions.

like image 193
Martin Preusse Avatar answered May 23 '26 00:05

Martin Preusse