Holger,<br><br>Yes, a test result i mean an exception is raised or not.<br><br>There was a case where a helper function which i call in the test and in teardown fails for example<br><br>class TestProduct:<br> def test_product(self):<br>
# test logic<br> helper.reboot()<br><br> def teardown_method()<br> # clean up<br> helper.reboot()<br><br>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.<br>
<br>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.<br>
<br>Any feedback on better ways on what I am trying to do is welcomed. thanks.<br><br>-hans <br><br> <br><br><div class="gmail_quote">2010/5/11 holger krekel <span dir="ltr"><<a href="mailto:holger@merlinux.eu">holger@merlinux.eu</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi Mark, Hans,<br>
<div class="im"><br>
On Tue, May 11, 2010 at 11:33 -0400, Mark Roddy wrote:<br>
> On Tue, May 11, 2010 at 10:44 AM, C. Titus Brown <<a href="mailto:ctb@msu.edu">ctb@msu.edu</a>> wrote:<br>
> > On Tue, May 11, 2010 at 11:30:10AM +0200, holger krekel wrote:<br>
> >> > I hope this is where people do Q&A on py.test. Otherwise please direct me to<br>
> >> > the right place.<br>
> >><br>
> >> Yes, I think so. Both nose and py.test (not sure about unittest2)<br>
> >> also have their own mailing lists but policies are somewhat unclear.<br>
> >> Often questions touch general testing issues and I find it interesting<br>
> >> to see how a specific tools/other people see it.<br>
><br>
</div><div class="im">> It's hacky, but you could accomplish this w/unittest by sub-classing<br>
> TestResult (or more likely _TextTestResult), overriding the<br>
> startTest(test) method and setting an attribute on the test instance<br>
> passed to the test result instance. Then in your tearDown() you can<br>
> grab the test result object and find the result of the test run<br>
> (though this will take some mucking w/the internal structures of<br>
> TestResult which are not geared for accessing results for a specific<br>
> test. Due to this you should probably add some convenience methods to<br>
> the TestResult class to make it easier).<br>
><br>
> You'll also need to inject your new subclass to make sure your<br>
> TestResult gets used instead of the underlying one. Look at<br>
> TestCase.defaultTestResult() and TextTestRunner._makeResult(),<br>
><br>
> I'm not advocating this approach, but it will get the job done.<br>
<br>
</div>However, this approach is not applicable to py.test (was the context of Hans'<br>
question). There you could put this in a conftest.py file:<br>
<br>
def pytest_runtest_makereport(item, call):<br>
if call.when == "call":<br>
item.excinfo = call.excinfo # memorize outcome info<br>
<br>
def pytest_runtest_teardown(item):<br>
if item.excinfo:<br>
### do something if an exception was raised during a test call<br>
else:<br>
### do something else if no exception was raised during a test call<br>
<br>
These hooks would be called for each test function execution in the directory<br>
of the conftest.py. If the idea is to have a post-check function that gets run after each "successful" test function the above hook could be refined like this:<br>
<br>
def pytest_runtest_teardown(item):<br>
if not item.excinfo:<br>
instance = item.getparent(py.test.collect.Instance)<br>
meth = getattr(instance.obj, 'check_post', None)<br>
if meth:<br>
meth()<br>
<br>
This would allow to write test files like this:<br>
<br>
class TestPost:<br>
def setup_method(self):<br>
self.STATE = ...<br>
def test_method1(self):<br>
...<br>
def check_post(self):<br>
assert self.STATE ....<br>
<br>
where check_post() gets run for each non-exception throwing test method.<br>
<br>
HTH,<br>
<font color="#888888">holger<br>
</font></blockquote></div><br>