Skip to content

Commit 91caf14

Browse files
committed
Tests: limit live API integration tests in Travis runs
To conserve our ESP test accounts' send quotas, don't run the live API integration tests 13 times in every Travis run. Instead, just run them twice, on a representative set of Python/Django combinations: * Once on Python 2.7 (currently with Django 1.8) * Once on Python 3.x (currently 3.5 with Django 1.9) (Prep for running weekly tests on Travis cron.) The *non*-integration tests still run on all combos. * Introduce RUN_LIVE_TESTS environment var to control whether live API integration test cases should run. Default True, except in Travis-CI runs default False. * Enable RUN_LIVE_TESTS in .travis.yml matrix for the Python/Django combos listed above. (cherry picked from commit 0c5911c)
1 parent 2778a43 commit 91caf14

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ matrix:
44
include:
55
# Anymail supports the same python versions as Django, excluding Python 3.2, but adding pypy.
66
# https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django
7+
8+
# Live API integration tests are only run on a few, representative Python/Django version
9+
# cominbations, to avoid rapidly consuming the testing accounts' entire send allotments.
10+
711
# Django 1.8: Python 2.7, 3.2 (until the end of 2016), 3.3, 3.4, 3.5
8-
- { env: DJANGO=django==1.8, python: 2.7 }
12+
- { env: DJANGO=django==1.8 RUN_LIVE_TESTS=true, python: 2.7 }
913
- { env: DJANGO=django==1.8, python: 3.3 }
1014
- { env: DJANGO=django==1.8, python: 3.4 }
1115
- { env: DJANGO=django==1.8, python: 3.5 }
1216
- { env: DJANGO=django==1.8, python: pypy }
1317
# Django 1.9: Python 2.7, 3.4, 3.5
1418
- { env: DJANGO=django==1.9, python: 2.7 }
1519
- { env: DJANGO=django==1.9, python: 3.4 }
16-
- { env: DJANGO=django==1.9, python: 3.5 }
20+
- { env: DJANGO=django==1.9 RUN_LIVE_TESTS=true, python: 3.5 }
1721
- { env: DJANGO=django==1.9, python: pypy }
1822
# Django 1.10 (prerelease): Python 2.7, 3.4, 3.5
1923
- { env: DJANGO="--pre django", python: 2.7 }

tests/test_mailgun_integration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
from anymail.exceptions import AnymailAPIError
1414
from anymail.message import AnymailMessage
1515

16-
from .utils import AnymailTestMixin, sample_image_path
16+
from .utils import AnymailTestMixin, sample_image_path, RUN_LIVE_TESTS
1717

1818
MAILGUN_TEST_API_KEY = os.getenv('MAILGUN_TEST_API_KEY')
1919
MAILGUN_TEST_DOMAIN = os.getenv('MAILGUN_TEST_DOMAIN')
2020

2121

22+
@unittest.skipUnless(RUN_LIVE_TESTS, "RUN_LIVE_TESTS disabled in this environment")
2223
@unittest.skipUnless(MAILGUN_TEST_API_KEY and MAILGUN_TEST_DOMAIN,
2324
"Set MAILGUN_TEST_API_KEY and MAILGUN_TEST_DOMAIN environment variables "
2425
"to run Mailgun integration tests")

tests/test_mandrill_integration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
from anymail.exceptions import AnymailAPIError, AnymailRecipientsRefused
1111
from anymail.message import AnymailMessage
1212

13-
from .utils import AnymailTestMixin, sample_image_path
13+
from .utils import AnymailTestMixin, sample_image_path, RUN_LIVE_TESTS
1414

1515
MANDRILL_TEST_API_KEY = os.getenv('MANDRILL_TEST_API_KEY')
1616

1717

18+
@unittest.skipUnless(RUN_LIVE_TESTS, "RUN_LIVE_TESTS disabled in this environment")
1819
@unittest.skipUnless(MANDRILL_TEST_API_KEY,
1920
"Set MANDRILL_TEST_API_KEY environment variable to run integration tests")
2021
@override_settings(MANDRILL_API_KEY=MANDRILL_TEST_API_KEY,

tests/test_postmark_integration.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from __future__ import unicode_literals
22

3+
import unittest
4+
35
from django.test import SimpleTestCase
46
from django.test.utils import override_settings
57

68
from anymail.exceptions import AnymailAPIError
79
from anymail.message import AnymailMessage
810

9-
from .utils import AnymailTestMixin, sample_image_path
11+
from .utils import AnymailTestMixin, sample_image_path, RUN_LIVE_TESTS
1012

1113

14+
@unittest.skipUnless(RUN_LIVE_TESTS, "RUN_LIVE_TESTS disabled in this environment")
1215
@override_settings(ANYMAIL_POSTMARK_SERVER_TOKEN="POSTMARK_API_TEST",
1316
EMAIL_BACKEND="anymail.backends.postmark.PostmarkBackend")
1417
class PostmarkBackendIntegrationTests(SimpleTestCase, AnymailTestMixin):

tests/test_sendgrid_integration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from anymail.exceptions import AnymailAPIError
1212
from anymail.message import AnymailMessage
1313

14-
from .utils import AnymailTestMixin, sample_image_path
14+
from .utils import AnymailTestMixin, sample_image_path, RUN_LIVE_TESTS
1515

1616
# For API_KEY auth tests:
1717
SENDGRID_TEST_API_KEY = os.getenv('SENDGRID_TEST_API_KEY')
@@ -21,6 +21,7 @@
2121
SENDGRID_TEST_PASSWORD = os.getenv('SENDGRID_TEST_PASSWORD')
2222

2323

24+
@unittest.skipUnless(RUN_LIVE_TESTS, "RUN_LIVE_TESTS disabled in this environment")
2425
@unittest.skipUnless(SENDGRID_TEST_API_KEY,
2526
"Set SENDGRID_TEST_API_KEY environment variable "
2627
"to run SendGrid integration tests")
@@ -118,6 +119,7 @@ def test_invalid_api_key(self):
118119
self.assertIn("authorization grant is invalid", str(err))
119120

120121

122+
@unittest.skipUnless(RUN_LIVE_TESTS, "RUN_LIVE_TESTS disabled in this environment")
121123
@unittest.skipUnless(SENDGRID_TEST_USERNAME and SENDGRID_TEST_PASSWORD,
122124
"Set SENDGRID_TEST_USERNAME and SENDGRID_TEST_PASSWORD"
123125
"environment variables to run SendGrid integration tests")

tests/test_sparkpost_integration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
from anymail.exceptions import AnymailAPIError
1111
from anymail.message import AnymailMessage
1212

13-
from .utils import AnymailTestMixin, sample_image_path
13+
from .utils import AnymailTestMixin, sample_image_path, RUN_LIVE_TESTS
1414

1515
SPARKPOST_TEST_API_KEY = os.getenv('SPARKPOST_TEST_API_KEY')
1616

1717

18+
@unittest.skipUnless(RUN_LIVE_TESTS, "RUN_LIVE_TESTS disabled in this environment")
1819
@unittest.skipUnless(SPARKPOST_TEST_API_KEY,
1920
"Set SPARKPOST_TEST_API_KEY environment variable "
2021
"to run SparkPost integration tests")

tests/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Anymail test utils
22
import sys
3+
from distutils.util import strtobool
34

45
import os
56
import re
@@ -10,6 +11,25 @@
1011
from django.test import Client
1112

1213

14+
def envbool(var, default=False):
15+
"""Returns value of environment variable var as a bool, or default if not set.
16+
17+
Converts `'true'` to `True`, and `'false'` to `False`.
18+
See :func:`~distutils.util.strtobool` for full list of allowable values.
19+
"""
20+
val = os.getenv(var, None)
21+
if val is None:
22+
return default
23+
else:
24+
return strtobool(val)
25+
26+
27+
# RUN_LIVE_TESTS: whether to run live API integration tests.
28+
# True by default, except in CONTINUOUS_INTEGRATION job.
29+
# (See comments and overrides in .travis.yml.)
30+
RUN_LIVE_TESTS = envbool('RUN_LIVE_TESTS', default=not envbool('CONTINUOUS_INTEGRATION'))
31+
32+
1333
def decode_att(att):
1434
"""Returns the original data from base64-encoded attachment content"""
1535
return b64decode(att.encode('ascii'))

0 commit comments

Comments
 (0)