[TIP] py.test test status check at teardown

holger krekel holger at merlinux.eu
Thu Aug 9 10:44:58 PDT 2012


Hi Baiju,

On Thu, Aug 09, 2012 at 21:18 +0530, Baiju M 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')

You don't need to use cached_setup with scope=function.
Instead you can directly call _get_webdriver() and
call "request.addfinalizer(_release_webdriver)" to
simplify matters.  Your own solution as indicated
in the second post looks fine.

best,
holger

> def test_hello_without_screenshot(resource):
>     assert 1 == 1
> 
> def test_hello_with_screenshot(resource):
>     assert 1 == 2
> 
> --
> Baiju M
> 
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
> 



More information about the testing-in-python mailing list