From 3a03f110132a29690a0ffc1f550ac1207b9a615d Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 14 Feb 2018 09:49:27 +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`. 1. https://github.com/MobileDynasty/pytest-env 2. https://docs.pytest.org/en/latest/customize.html#confval-testpaths --- pytest.ini | 11 +++++++++++ requirements_for_test.txt | 1 + 2 files changed, 12 insertions(+) create mode 100644 pytest.ini diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 000000000..6a7eed81e --- /dev/null +++ b/pytest.ini @@ -0,0 +1,11 @@ +[pytest] +testpaths = tests +env = + NOTIFY_ENVIRONMENT=test + ADMIN_CLIENT_SECRET=dev-notify-secret-key + API_HOST_NAME=test + DANGEROUS_SALT=dev-notify-salt + SECRET_KEY=dev-notify-secret-key + DESKPRO_API_HOST=test + DESKPRO_API_KEY=test + STATSD_PREFIX=stats-prefix diff --git a/requirements_for_test.txt b/requirements_for_test.txt index f4ba9ba05..ebd78061a 100644 --- a/requirements_for_test.txt +++ b/requirements_for_test.txt @@ -1,5 +1,6 @@ -r requirements.txt 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 513c64be6438550319497e6aec79f64a5f6389b0 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 14 Feb 2018 09:50:18 +0000 Subject: [PATCH 2/2] Remove environment_test.sh These config variables are now set in `pytest.ini` instead. --- environment_test.sh | 8 -------- scripts/run_tests.sh | 4 ---- 2 files changed, 12 deletions(-) delete mode 100644 environment_test.sh diff --git a/environment_test.sh b/environment_test.sh deleted file mode 100644 index 18f0add28..000000000 --- a/environment_test.sh +++ /dev/null @@ -1,8 +0,0 @@ -export NOTIFY_ENVIRONMENT='test' -export ADMIN_CLIENT_SECRET='dev-notify-secret-key' -export API_HOST_NAME='' -export DANGEROUS_SALT='dev-notify-salt' -export SECRET_KEY='dev-notify-secret-key' -export DESKPRO_API_HOST="" -export DESKPRO_API_KEY="" -export STATSD_PREFIX="stats-prefix" diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh index d8d6b5a39..a6a09ef94 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