mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-08 02:38:26 -04:00
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:
@@ -3,6 +3,7 @@ pycodestyle==2.3.1
|
|||||||
pytest==3.2.1
|
pytest==3.2.1
|
||||||
pytest-mock==1.6.2
|
pytest-mock==1.6.2
|
||||||
pytest-cov==2.5.1
|
pytest-cov==2.5.1
|
||||||
|
pytest-xdist==1.20.0
|
||||||
coveralls==1.2.0
|
coveralls==1.2.0
|
||||||
moto==1.1.1
|
moto==1.1.1
|
||||||
freezegun==0.3.9
|
freezegun==0.3.9
|
||||||
|
|||||||
@@ -30,5 +30,6 @@ fi
|
|||||||
pycodestyle .
|
pycodestyle .
|
||||||
display_result $? 1 "Code style check"
|
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"
|
display_result $? 2 "Unit tests"
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import boto3
|
|
||||||
import pytest
|
|
||||||
from alembic.command import upgrade
|
from alembic.command import upgrade
|
||||||
from alembic.config import Config
|
from alembic.config import Config
|
||||||
from flask_migrate import Migrate, MigrateCommand
|
from flask_migrate import Migrate, MigrateCommand
|
||||||
from flask_script import Manager
|
from flask_script import Manager
|
||||||
|
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
|
||||||
|
import boto3
|
||||||
|
import psycopg2
|
||||||
|
import pytest
|
||||||
|
|
||||||
from app import create_app, db
|
from app import create_app, db
|
||||||
|
|
||||||
@@ -45,9 +47,31 @@ def client(notify_api):
|
|||||||
yield client
|
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')
|
@pytest.fixture(scope='session')
|
||||||
def notify_db(notify_api):
|
def notify_db(notify_api, worker_id):
|
||||||
assert db.engine.url.database != 'notification_api', 'dont run tests against main db'
|
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)
|
Migrate(notify_api, db)
|
||||||
Manager(db, MigrateCommand)
|
Manager(db, MigrateCommand)
|
||||||
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
||||||
|
|||||||
Reference in New Issue
Block a user