Skip to content

Commit 59cfb4d

Browse files
committed
Switch test execution method
Switch to the test execution suggested by Django 1.9 docs.
1 parent 3499da9 commit 59cfb4d

File tree

7 files changed

+62
-58
lines changed

7 files changed

+62
-58
lines changed

runtests.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Based on:
4+
# https://docs.djangoproject.com/en/1.9/topics/testing/advanced
5+
# Using the Django test runner to test reusable applications
6+
7+
import os
8+
import logging
9+
import sys
10+
11+
import django
12+
from django.conf import settings
13+
from django.test.utils import get_runner
14+
15+
if __name__ == "__main__":
16+
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
17+
django.setup()
18+
19+
# Log memcache errors to console
20+
from django_pylibmc.memcached import log
21+
handler = logging.StreamHandler()
22+
handler.setLevel(logging.DEBUG)
23+
log.addHandler(handler)
24+
25+
# Test that the cache is working at all
26+
from django.core.cache import cache
27+
assert cache
28+
test_val = 'The test passed'
29+
assert cache.set('test', test_val), "Could not set cache value"
30+
cache_val = cache.get('test')
31+
assert cache_val == test_val, "Could not get from cache"
32+
33+
# Ignore memcache errors during tests
34+
handler.setLevel(logging.CRITICAL)
35+
36+
# Run the tests
37+
TestRunner = get_runner(settings)
38+
test_runner = TestRunner()
39+
failures = test_runner.run_tests(["tests"])
40+
sys.exit(bool(failures))

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Tests for django-pylibmc

tests/app/__init__.py

Whitespace-only changes.
File renamed without changes.

tests/settings.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
}
88

99
INSTALLED_APPS = (
10-
'app',
10+
'tests',
1111
)
1212

1313
CACHES = {
@@ -44,3 +44,19 @@
4444
'django.contrib.messages.middleware.MessageMiddleware',
4545
'django.middleware.clickjacking.XFrameOptionsMiddleware',
4646
)
47+
48+
LOGGING = {
49+
'version': 1,
50+
'disable_existing_loggers': False,
51+
'handlers': {
52+
'console': {
53+
'class': 'logging.StreamHandler',
54+
},
55+
},
56+
'loggers': {
57+
'django': {
58+
'handlers': ['console'],
59+
'level': 'CRITICAL'
60+
}
61+
}
62+
}

tests/tests.py

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,9 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
import os
4-
import logging
5-
import sys
63
import time
7-
import unittest
8-
9-
10-
test_dir = os.path.dirname(os.path.abspath(__file__))
11-
sys.path.append(os.path.join(test_dir, os.path.pardir))
12-
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
13-
14-
try:
15-
from django.test.runner import DiscoverRunner as Runner
16-
except ImportError:
17-
# Django 1.5 and below
18-
from django.test.simple import DjangoTestSuiteRunner as Runner
19-
20-
from app.models import Poll, expensive_calculation
21-
22-
try:
23-
from django import setup
24-
except ImportError:
25-
# Django 1.6 and below does not require setup
26-
setup = lambda: None
27-
else:
28-
assert setup
4+
from django.test import TestCase
295

6+
from .models import Poll, expensive_calculation
307

318
# functions/classes for complex data type tests
329
def f():
@@ -51,7 +28,7 @@ def load_cache(name):
5128

5229
# Lifted originally from django/regressiontests/cache/tests.py.
5330
# In Django 1.8 (and earlier), tests are in tests/cache/tests.py.
54-
class PylibmcCacheTests(unittest.TestCase):
31+
class PylibmcCacheTests(TestCase):
5532
cache_name = 'default'
5633

5734
def setUp(self):
@@ -326,33 +303,3 @@ class PylibmcCacheWithBinaryTests(PylibmcCacheTests):
326303

327304
class PylibmcCacheWithOptionsTests(PylibmcCacheTests):
328305
cache_name = 'with_options'
329-
330-
331-
if __name__ == '__main__':
332-
# Initialize app in Django 1.7 and above
333-
setup()
334-
335-
# Log memcache errors to console
336-
from django_pylibmc.memcached import log
337-
handler = logging.StreamHandler()
338-
handler.setLevel(logging.DEBUG)
339-
log.addHandler(handler)
340-
341-
# Test that the cache is working at all
342-
from django.core.cache import cache
343-
assert cache
344-
test_val = 'The test passed'
345-
assert cache.set('test', test_val), "Could not set cache value"
346-
cache_val = cache.get('test')
347-
assert cache_val == test_val, "Could not get from cache"
348-
349-
# Ignore memcache errors during tests
350-
handler.setLevel(logging.CRITICAL)
351-
352-
# Run the tests
353-
runner = Runner()
354-
try:
355-
old_config = runner.setup_databases()
356-
unittest.main()
357-
finally:
358-
runner.teardown_databases(old_config)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ envlist =
99
py{27,34,35}-django-master,
1010

1111
[testenv]
12-
commands = {envpython} tests/tests.py
12+
commands = {envpython} runtests.py
1313
deps =
1414
django18: Django>=1.8,<1.9
1515
django19: Django>=1.9,<1.10

0 commit comments

Comments
 (0)