Merge pull request #1201 from alphagov/parallel-db-tests

run tests in multiple threads at once
This commit is contained in:
Leo Hemsted
2017-08-30 09:42:39 +01:00
committed by GitHub
3 changed files with 35 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ pycodestyle==2.3.1
pytest==3.2.1
pytest-mock==1.6.2
pytest-cov==2.5.1
pytest-xdist==1.20.0
coveralls==1.2.0
moto==1.1.1
freezegun==0.3.9

View File

@@ -30,5 +30,6 @@ fi
pycodestyle .
display_result $? 1 "Code style check"
py.test --cov=app --cov-report=term-missing tests/ --junitxml=test_results.xml
# run with four concurrent threads
py.test --cov=app --cov-report=term-missing tests/ --junitxml=test_results.xml -n 4
display_result $? 2 "Unit tests"

View File

@@ -1,12 +1,13 @@
from contextlib import contextmanager
import os
import boto3
import pytest
from alembic.command import upgrade
from alembic.config import Config
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
import boto3
import pytest
import sqlalchemy
from app import create_app, db
@@ -45,9 +46,36 @@ def client(notify_api):
yield client
def create_test_db(database_uri):
# get the
db_uri_parts = database_uri.split('/')
postgres_db_uri = '/'.join(db_uri_parts[:-1] + ['postgres'])
postgres_db = sqlalchemy.create_engine(
postgres_db_uri,
echo=False,
isolation_level='AUTOCOMMIT',
client_encoding='utf8'
)
try:
result = postgres_db.execute(sqlalchemy.sql.text('CREATE DATABASE {}'.format(db_uri_parts[-1])))
result.close()
except sqlalchemy.exc.ProgrammingError:
# database "test_notification_api_master" already exists
pass
finally:
postgres_db.dispose()
@pytest.fixture(scope='session')
def notify_db(notify_api):
assert db.engine.url.database != 'notification_api', 'dont run tests against main db'
def notify_db(notify_api, worker_id):
assert 'test_notification_api' in db.engine.url.database, 'dont run tests against main db'
# create a database for this worker thread -
from flask import current_app
current_app.config['SQLALCHEMY_DATABASE_URI'] += '_{}'.format(worker_id)
create_test_db(current_app.config['SQLALCHEMY_DATABASE_URI'])
Migrate(notify_api, db)
Manager(db, MigrateCommand)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))