From d36b742e14338c371f35fa21acc5bf3b7cb2efa3 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 14 Feb 2018 11:19:11 +0000 Subject: [PATCH 1/2] Automatically set environment vars before tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sometimes you just wanna run some tests directly using the `pytest` command. But you’re in a new shell, and have forgotten to do `source environment_test.sh`. The screen fills with red, and your day just got a little bit worse. This commit will stop this from ever happening again, by making the setting environment variables part of running Pytest. It does this with a plugin called pytest-env[1]. pytest.ini is the standard way of configuring pytest. Creating this file where it didn’t exist before changes the behaviour of pytest, in that it will now look for tests in the same directory as the file, rather than defaulting to the `tests/` directory. So we also have to explicitly configure pytest[2] to tell it that it should only look in this directory. Otherwise it gets lost in the weeds of `node_modules`. This also changes the way that `SQLALCHEMY_DATABASE_URI` is overriden to the convention used by this plugin. 1. https://github.com/MobileDynasty/pytest-env 2. https://docs.pytest.org/en/latest/customize.html#confval-testpaths fixup! Remove environment_test.sh --- Makefile | 2 +- pytest.ini | 20 ++++++++++++++++++++ requirements_for_test.txt | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 pytest.ini diff --git a/Makefile b/Makefile index b96a292a2..ebf56145a 100644 --- a/Makefile +++ b/Makefile @@ -128,7 +128,7 @@ test-with-docker: prepare-docker-build-image create-docker-test-db ## Run tests --link "${DOCKER_CONTAINER_PREFIX}-db:postgres" \ -e UID=$(shell id -u) \ -e GID=$(shell id -g) \ - -e TEST_DATABASE=postgresql://postgres:postgres@postgres/test_notification_api \ + -e SQLALCHEMY_DATABASE_URI=postgresql://postgres:postgres@postgres/test_notification_api \ -e GIT_COMMIT=${GIT_COMMIT} \ -e BUILD_NUMBER=${BUILD_NUMBER} \ -e BUILD_URL=${BUILD_URL} \ diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 000000000..a76815501 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,20 @@ +[pytest] +testpaths = tests +env = + D:SQLALCHEMY_DATABASE_URI=postgresql://@localhost/test_notification_api + SECRET_KEY=secret-key + DANGEROUS_SALT=dangerous-salt + NOTIFY_ENVIRONMENT=test + ADMIN_CLIENT_SECRET=dev-notify-secret-key + ADMIN_BASE_URL=http://localhost:6012 + FROM_NUMBER=from_number + MMG_URL=https://api.mmg.co.uk/json/api.php + MMG_API_KEY=mmg-secret-key + LOADTESTING_API_KEY=loadtesting + FIRETEXT_API_KEY=Firetext + STATSD_PREFIX=stats-prefix + NOTIFICATION_QUEUE_PREFIX=testing + REDIS_URL=redis://localhost:6379/0 + FLASK_APP=application.py + FLASK_DEBUG=1 + WERKZEUG_DEBUG_PIN=off diff --git a/requirements_for_test.txt b/requirements_for_test.txt index 71a8c290d..1e62201eb 100644 --- a/requirements_for_test.txt +++ b/requirements_for_test.txt @@ -1,6 +1,7 @@ -r requirements.txt flake8==3.5.0 pytest==3.4.0 +pytest-env==0.6.2 pytest-mock==1.6.3 pytest-cov==2.5.1 pytest-xdist==1.22.0 From 845aad1183d9f4ab9b94a7981d9e018228ddc5a2 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 14 Feb 2018 12:18:46 +0000 Subject: [PATCH 2/2] Remove environment_test.sh These config variables are now set in `pytest.ini` instead. --- environment_test.sh | 17 ----------------- scripts/run_tests.sh | 4 ---- 2 files changed, 21 deletions(-) delete mode 100644 environment_test.sh diff --git a/environment_test.sh b/environment_test.sh deleted file mode 100644 index f165e3334..000000000 --- a/environment_test.sh +++ /dev/null @@ -1,17 +0,0 @@ -export SQLALCHEMY_DATABASE_URI=${TEST_DATABASE:='postgresql://localhost/test_notification_api'} -export SECRET_KEY='secret-key' -export DANGEROUS_SALT='dangerous-salt' -export NOTIFY_ENVIRONMENT='test' -export ADMIN_CLIENT_SECRET='dev-notify-secret-key' -export ADMIN_BASE_URL='http://localhost:6012' -export FROM_NUMBER='from_number' -export MMG_URL="https://api.mmg.co.uk/json/api.php" -export MMG_API_KEY='mmg-secret-key' -export LOADTESTING_API_KEY="loadtesting" -export FIRETEXT_API_KEY="Firetext" -export STATSD_PREFIX="stats-prefix" -export NOTIFICATION_QUEUE_PREFIX='testing' -export REDIS_URL="redis://localhost:6379/0" -export FLASK_APP=application.py -export FLASK_DEBUG=1 -export WERKZEUG_DEBUG_PIN=off \ No newline at end of file diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh index 8f5c41561..f1668e697 100755 --- a/scripts/run_tests.sh +++ b/scripts/run_tests.sh @@ -5,12 +5,8 @@ # NOTE: This script expects to be run from the project root with # ./scripts/run_tests.sh -# Use default environment vars for localhost if not already set - set -o pipefail -source environment_test.sh - function display_result { RESULT=$1 EXIT_STATUS=$2