run tests in multiple threads at once

previously we didn't do this because the tests all used the same DB
(test_notifications_api), however @minglis shared a snippet that simply
creates one test db per thread.
This commit is contained in:
Leo Hemsted
2017-08-23 14:46:40 +01:00
parent 4f0443041d
commit 3d4dbaa632
3 changed files with 31 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 six concurrent threads
py.test --cov=app --cov-report=term-missing tests/ --junitxml=test_results.xml -n 6
display_result $? 2 "Unit tests"

View File

@@ -1,12 +1,14 @@
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
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import boto3
import psycopg2
import pytest
from app import create_app, db
@@ -45,9 +47,31 @@ def client(notify_api):
yield client
def create_test_db(database_uri):
database_name = database_uri.split('/')[-1]
system_db = psycopg2.connect(dbname='postgres')
system_db.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = system_db.cursor()
try:
cursor.execute('CREATE DATABASE {}'.format(database_name))
except psycopg2.ProgrammingError:
# database "test_notification_api_x..." already exists
pass
finally:
cursor.close()
system_db.close()
@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__))