Pytest Xdist parallel execution, prevent recreating the database

3.3k views Asked by At

I'm trying to speed up the Selenium tests in my python Django web application by using parallel execution of 4 threads (-n=4)

Of the first 4 tests, 3 give the following error:

[test setup] [Test Error Output]
Got an error creating the test database: (1007, "Can't create database 'test1database'; database exists")

I get that I have to specify setup to be run once before parallel test execution, to prevent this multiple attempts at creating the database, but how would I enforce this in pytest xdist configuration?

2

There are 2 answers

1
xverges On BEST ANSWER

You can probably have a different database for each one of the threads. The worker_id fixture allows you to do that https://github.com/pytest-dev/pytest-xdist#identifying-the-worker-process-during-a-test

@pytest.fixture()
def test_database(worker_id):
    return CreateDatabase("test{}database".format(worker_id))

Update

This github issue comment shows a solution to the OP's original problem. It also creates N databases, using a shared template. This brings the interesting twist of synchronizing the access to a shared resource in fixture.

0
Samarth On

You can use this if you won't be stuck in any problem in the rest of your code:

CREATE DATABASE IF NOT EXISTS test1database;