[TIP] getting the result of a test in teardown_method?

Robert Collins robertc at robertcollins.net
Sun May 23 19:56:16 PDT 2010


On Wed, May 12, 2010 at 10:11 AM, Hans Sebastian <hnsbstn at gmail.com> wrote:
> Holger,
>
> Yes, a test result i mean an exception is raised or not.
>
> There was a case where a helper function which i call in the test and in
> teardown fails for example
>
> class TestProduct:
>   def test_product(self):
>     # test logic
>     helper.reboot()
>
>   def teardown_method()
>     # clean up
>     helper.reboot()
>
> If reboot fails, i don't want to call it again in teardown because it might
> put the product in a different state. That's one example I had.
>
> Now I just want to measure the time a test takes for each test that passes,
> specifically get the timedelta between when the test started and when it
> ended. I can try your solution iwht the post-check function. However I
> notice pytest_runtest_teardown gets called before my method and class
> teardowns so the teardown won't get measured.
>
> Any feedback on better ways on what I am trying to do is welcomed. thanks.

Well in the pyunit world, its generally as simple as using a custom
TestResult, which many runners (e.g. trial) support directly, others
require a tiny bit of glue to set it up. The testrepository project i
use to run most of my tests now uses subunit and that generates timing
data pretty much automatically, using the testtools timing decorator.

Timing is not a responsibility of the test code, but of the infrastructure;)

-Rob



More information about the testing-in-python mailing list