[TIP] parametrize a pytest finalizer

Bruno Oliveira nicoddemus at gmail.com
Wed Apr 9 14:44:09 PDT 2014


Hi Chris,

You can use a lambda function:

@pytest.fixture()
def test_fixture(request):

    def cleanup(cleanup_parameter):
        print cleanup_parameter

    try:
        raise
    except Exception:
        cleanup("Problem in fixture!")

    request.addfinalizer(lambda: cleanup("Cleaning up"))

Make sure to run the above using "-s" so no output is captured by pytest.

Hope this helps,

Best Regards,


On Wed, Apr 9, 2014 at 6:15 PM, Pella,Chris <Chris.Pella at safenet-inc.com>wrote:

> It doesn't seem possible to pass arguments to a pytest finalizer. I can
> work around it this way, but it seems exceedingly ugly:
>
>
>
> @pytest.fixture(scope = "class")
>
> def test_fixture(request):
>
>
>
>     global cleanup_parameter
>
>     cleanup_parameter = "Cleaning up"
>
>
>
>     def cleanup():
>
>         global cleanup_parameter
>
>         print cleanup_parameter
>
>
>
>     try:
>
>         raise
>
>     except Exception:
>
>         cleanup_parameter = "Problem in fixture!"
>
>         cleanup()
>
>
>
>     request.addfinalizer(cleanup)
>
>
>
>
>
> Is there a better way?
>
> The information contained in this electronic mail transmission
> may be privileged and confidential, and therefore, protected
> from disclosure. If you have received this communication in
> error, please notify us immediately by replying to this
> message and deleting it from your computer without copying
> or disclosing it.
>
>
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20140409/e4c89742/attachment.htm>


More information about the testing-in-python mailing list