Timing a unit test, including the set up
django, python, python-unittest, testing, unit-testing
Solution
I would not reinvent the wheel and use `nose` test runner with `nose-timer` plugin:
A timer plugin for nosetests that answers the question: how much time does every test take?
See more about `nose-timer` here:
- How to benchmark unit tests in Python without adding any code
Problem
How can you capture the time of an individual unit-test, including the set-up cost? I've got a test base with a set-up procedure which takes a non-trivial amount of time to complete. I've got several tests which descend from that test base, and I've got a decorator which, in theory, should print out the time it takes to run each test: ``` class TestBase(unittest.TestCase): def setUp(self): # some setup procedure that takes a long time def timed_test(decorated_test): def run_test(self, *kw, **kwargs): start = time.time() decorated_test(self, *kw, **kwargs) end = time.time() print "test_duration: %s (seconds)" % (end - start) return run_test class TestSomething(TestBase): @timed_test def test_something_useful(self): # some test ``` Now, when I run these tests it turns out that I'm only printing the time it took for the test to run not including the set-up time. Tangentially, a related question may be: is it best to deal with timing outside of your testing framework?