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&#39;t want to call it again in teardown because it might put the product in a different state. That&#39;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&#39;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">&lt;<a href="mailto:holger@merlinux.eu">holger@merlinux.eu</a>&gt;</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>
&gt; On Tue, May 11, 2010 at 10:44 AM, C. Titus Brown &lt;<a href="mailto:ctb@msu.edu">ctb@msu.edu</a>&gt; wrote:<br>
&gt; &gt; On Tue, May 11, 2010 at 11:30:10AM +0200, holger krekel wrote:<br>
&gt; &gt;&gt; &gt; I hope this is where people do Q&amp;A on py.test. Otherwise please direct me to<br>
&gt; &gt;&gt; &gt; the right place.<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; Yes, I think so.  Both nose and py.test (not sure about unittest2)<br>
&gt; &gt;&gt; also have their own mailing lists but policies are somewhat unclear.<br>
&gt; &gt;&gt; Often questions touch general testing issues and I find it interesting<br>
&gt; &gt;&gt; to see how a specific tools/other people see it.<br>
&gt;<br>
</div><div class="im">&gt; It&#39;s hacky, but you could accomplish this w/unittest by sub-classing<br>
&gt; TestResult (or more likely _TextTestResult), overriding the<br>
&gt; startTest(test) method and setting an attribute on the test instance<br>
&gt; passed to the test result instance.  Then in your tearDown() you can<br>
&gt; grab the test result object and find the result of the test run<br>
&gt; (though this will take some mucking w/the internal structures of<br>
&gt; TestResult which are not geared for accessing results for a specific<br>
&gt; test.  Due to this you should probably add some convenience methods to<br>
&gt; the TestResult class to make it easier).<br>
&gt;<br>
&gt; You&#39;ll also need to inject your new subclass to make sure your<br>
&gt; TestResult gets used instead of the underlying one.  Look at<br>
&gt; TestCase.defaultTestResult() and TextTestRunner._makeResult(),<br>
&gt;<br>
&gt; I&#39;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&#39;<br>
question).  There you could put this in a conftest.py file:<br>
<br>
    def pytest_runtest_makereport(item, call):<br>
        if call.when == &quot;call&quot;:<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 &quot;successful&quot; 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, &#39;check_post&#39;, 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>