[TIP] py.test test status check at teardown

Baiju M baiju.m.mail at gmail.com
Thu Aug 9 09:43:45 PDT 2012


I think, I found the answer. However, please let me know if any better
approach is there.

I added this into conftest.py (If add this into the same example file,
it's not working):

def pytest_runtest_makereport(item, call):
    resource = item.funcargs['resource']
    resource.excinfo = call.excinfo

Then, I modified the teardown function like this:

def _release_webdriver(webdriver):
    if webdriver.excinfo and webdriver.excinfo.typename != 'Skipped':
        screenshot = webdriver.get_screenshot_as_base64()
    webdriver.quit()


On Thu, Aug 9, 2012 at 9:18 PM, Baiju M <baiju.m.mail at gmail.com> wrote:
> Hi,
>
> I am using a funcarg with cached_setup with scope as 'function'.
> At teardown, I want to check whether test is failed or not
> and perform some action. Any pointers ?
> Here is my sample code.
>
> class Webdriver(object):
>     def get_screenshot_as_base64(self):
>         return "ScreenShot"
>     def quit(self):
>         pass
>
> def _get_webdriver():
>     return Webdriver()
>
> def _release_webdriver(webdriver):
>     # FIXME: How to check test is failed or not ?
>     screenshot = webdriver.get_screenshot_as_base64()
>     webdriver.quit()
>
> def pytest_funcarg__resource(request):
>
>     return request.cached_setup(
>         setup=_get_webdriver,
>         teardown=_release_webdriver,
>         scope='function')
>
> def test_hello_without_screenshot(resource):
>     assert 1 == 1
>
> def test_hello_with_screenshot(resource):
>     assert 1 == 2
>
> --
> Baiju M



More information about the testing-in-python mailing list