vectro404
vectro404

Reputation: 49

Access runtime from results in unittest

When I run a unittest in Python, I would like to access the time the test took (not each individual testcase but the whole test). This is displayed as part of the result. Is there a way to do this other than just parsing the results string?

Upvotes: 2

Views: 141

Answers (1)

john_science
john_science

Reputation: 6541

Sadly, no, you can't access the test time directly. At least not without modifying some of the unittest classes.

That time is calculated in TextTestRunner.run and is not saved as a member of a class, but streamed as a print-out:

self.stream.writeln("Ran %d test%s in %.3fs" %
                    (run, run != 1 and "s" or "", timeTaken))

If you over-write TextTestRunner.run and the method that calls it in unittest.main, then you could access the timeTaken variable.

It would be easier to just parse the output text.

Moreover, since you can now see the timeTaken formatting statement used to write that line, you can parse it without risk.

Upvotes: 1

Related Questions