Timing a unit test, including customization

How can you record the time of a single unit test, including installation costs?

I have a test base with a setup procedure that requires a non-trivial amount of time to complete. I have several tests that come off this test base, and I have a decorator that theoretically should print 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 print only the time needed to run the test, not including the setup time. A tangentially related question might be: is it better to deal with timing outside the scope of testing?

+4
1

nose nose-timer:

nosetests, : ?

nose-timer :

+5

Source: https://habr.com/ru/post/1548503/


All Articles