Permalink
Browse files

[test speedup] Added --time & --detailed options #247

  • Loading branch information...
nemesisdesign committed Jul 13, 2015
1 parent aad2e5f commit 7e522df1d5e28c5c7d2c44df4b6b88a23a98a866
@@ -171,7 +171,7 @@ Run all the tests with::
./runtests.py --keepdb
The ``keepdb`` option allows to avoid recreating the test database at each run, hence saving precious time.
The ``keepdb`` option allows to avoid recreating the test database at each run, **hence saving precious time**.
If you want to speed up tests even more, tweak your local postgresql configuration by setting these values::
@@ -209,6 +209,40 @@ Calculate test coverage for specific modules::
coverage run --source=nodeshot.core.nodes ./manage.py test --keepdb nodeshot.core.nodes && coverage report
Measure time spent running tests
--------------------------------
Automated tests that involve database calls and/or HTTP requests can quickly become slow.
Slow tests mean low productivity, especially if you are used to **Test Driven Development**.
For this reason, if you notice that some tests are slow, you have two additional options to measure test executition times,
**find out which tests are slow** and refactor them.
Time option
^^^^^^^^^^^
``--time`` will measure how much time is needed to execute each test class.
Detailed option
^^^^^^^^^^^^^^^
``--time --detailed`` will measure how much time is needed to execute each test class **and** each single test.
Examples
^^^^^^^^
Here's a couple of examples::
# general measurement
./runtests.py --keepdb --time
# detailed measurement
./runtests.py --keepdb --time --detailed
# detailed measurement for a specific test class
python manage.py test --keepdb --time --detailed nodeshot.core.nodes
.. _build-the-docs:
==============================
@@ -37,6 +37,7 @@ class MailingTest(TestCase):
]
def setUp(self):
super(MailingTest, self).setUp()
# create outward record
self.message = Outward.objects.create(
status=0,
@@ -43,6 +43,7 @@ class TestNotification(TestCase):
]
def setUp(self):
super(TestNotification, self).setUp()
# empty outbox, emails generated from EmailAddress model in nodeshot.community.profiles
mail.outbox = []
@@ -26,6 +26,7 @@ class ProfilesTest(TestCase):
]
def setUp(self):
super(ProfilesTest, self).setUp()
self.client.login(username='registered', password='tester')
if EMAIL_CONFIRMATION:
for user in User.objects.all():
@@ -0,0 +1,44 @@
import sys
# disable logging during tests
if 'test' in sys.argv:
import logging
logging.disable(logging.CRITICAL)
# add support for --time option to measure the time needed to execute each Test Class
if 'test' in sys.argv and '--time' in sys.argv:
sys.argv.remove('--time')
import time
from unittest import TestCase
@classmethod
def setUpClass(cls):
print("\n\033[95m%s.%s started\033[0m" % (cls.__module__, cls.__name__))
cls.class_start_time = time.time()
@classmethod
def tearDownClass(cls):
print("\n\033[94m%s.%s finished in %.3f seconds\033[0m" % (cls.__module__, cls.__name__, time.time() - cls.class_start_time))
TestCase.setUpClass = setUpClass
TestCase.tearDownClass = tearDownClass
# add support for --detailed option to measure the time needed to execute each test
if '--detailed' in sys.argv:
sys.argv.remove('--detailed')
def setUp(self):
self.method_start_time = time.time()
def tearDown(self):
execution_time = time.time() - self.method_start_time
if execution_time < 1.5:
color = "\033[92m"
elif execution_time < 3.5:
color = "\033[93m"
else:
color = "\033[91m"
print("\n%s%s executed in %.3f seconds\033[0m" % (color, self._testMethodName, execution_time))
TestCase.setUp = setUp
TestCase.tearDown = tearDown
@@ -33,9 +33,10 @@ class TestOldImporter(TestCase):
]
def setUp(self):
super(TestOldImporter, self).setUp()
Link.objects.all().delete()
OldLink.objects.all().delete()
for fixture in self.mysql_fixtures:
management.call_command('loaddata', fixture, database='old_nodeshot')
@@ -28,6 +28,7 @@ class LinkTest(TestCase):
]
def setUp(self):
super(LinkTest, self).setUp()
l = Link()
l.interface_a = Interface.objects.find(2)
l.interface_b = Interface.objects.find(3)
@@ -246,7 +247,6 @@ class TopologyTest(StaticLiveServerTestCase):
'test_layers.json',
'test_status.json',
'test_nodes.json',
'test_routing_protocols.json',
'test_topology_data.json'
]
View
@@ -5,3 +5,11 @@
app = init_celery('ci')
# measure test run time with --time and --detailed:
# ./runtests.py --time
# or
# django test <module> --time --detailed
from nodeshot.core.base import tests_speedup

0 comments on commit 7e522df

Please sign in to comment.