Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ env:
- DJANGO=Django==1.6.11

install:
- pip install flake8 coverage coveralls $DJANGO
- pip install flake8 coverage coveralls $DJANGO freezegun
- pip install -e .

matrix:
Expand Down
14 changes: 12 additions & 2 deletions django_slowtests/testrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
from django.conf import settings


try: # pragma: no cover
import freezegun

def _time():
return freezegun.api.real_time()
except ImportError: # pragma: no cover
def _time():
return time.time()


TIMINGS = {}
NUM_SLOW_TESTS = getattr(settings, 'NUM_SLOW_TESTS', 10)
SLOW_TEST_THRESHOLD_MS = getattr(settings, 'SLOW_TEST_THRESHOLD_MS', 0)
Expand All @@ -26,7 +36,7 @@ def run(self, result, debug=False):
if result.shouldStop:
break

start_time = time.time()
start_time = _time()

if _isnotsuite(test):
self._tearDownPreviousClass(test, result)
Expand All @@ -43,7 +53,7 @@ def run(self, result, debug=False):
else:
test.debug()

TIMINGS[str(test)] = time.time() - start_time
TIMINGS[str(test)] = _time() - start_time

if topLevel:
self._tearDownPreviousClass(None, result)
Expand Down
15 changes: 15 additions & 0 deletions django_slowtests/tests/fake.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
from django.test import TestCase
from freezegun import freeze_time


class FakeTestCase(TestCase):
Expand All @@ -15,3 +16,17 @@ def test_slow_thing(self):

def test_setup_class_was_run(self):
self.assertTrue(self._setupClassRan)


@freeze_time('2016-02-03 12:34:56')
class FakeFrozenInPastTestCase(TestCase):

def test_this_should_not_have_a_negative_duration(self):
self.assertTrue(True)


@freeze_time('3017-02-03 12:34:56')
class FakeFrozenInFutureTestCase(TestCase):

def test_this_should_not_have_very_long_duration(self):
self.assertTrue(True)
25 changes: 24 additions & 1 deletion django_slowtests/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from django.test import TestCase
from unittest import TestResult
from ..testrunner import TimingSuite
from ..testrunner import TimingSuite, TIMINGS


class TimingSuiteTests(TestCase):

def setUp(self):
TIMINGS.clear()

def test_add_a_test(self):
from .fake import FakeTestCase
suite = TimingSuite()
Expand All @@ -14,3 +17,23 @@ def test_add_a_test(self):
suite.run(result)
self.assertEquals(len(suite._tests), 2)
self.assertEquals(len(result.errors), 0)

def test_timing_is_correct_when_freezegun_sets_time_in_past(self):
from .fake import FakeFrozenInPastTestCase
suite = TimingSuite()
result = TestResult()
suite.addTest(FakeFrozenInPastTestCase('test_this_should_not_have_a_negative_duration'))
suite.run(result)
test_name = str(suite._tests[0])
self.assertTrue(TIMINGS[test_name] > 0)
self.assertTrue(TIMINGS[test_name] < 1)

def test_timing_is_correct_when_freezegun_sets_time_in_future(self):
from .fake import FakeFrozenInFutureTestCase
suite = TimingSuite()
result = TestResult()
suite.addTest(FakeFrozenInFutureTestCase('test_this_should_not_have_very_long_duration'))
suite.run(result)
test_name = str(suite._tests[0])
self.assertTrue(TIMINGS[test_name] > 0)
self.assertTrue(TIMINGS[test_name] < 1)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ deps =
dj1.8: Django>=1.8,<1.9
dj1.9: Django>=1.9,<1.10
dj1.10: Django>=1.10,<1.11
freezegun>=0.1.8
commands = coverage run setup.py test