[TIP] parametrize a pytest finalizer

Pella,Chris Chris.Pella at safenet-inc.com
Thu Apr 10 07:49:46 PDT 2014


Hi Bruno,
That works well, although I don't understand why. I haven't used lambdas before,  but the signature is the same as for a normal function. I guess the anonymity makes pytest treat it differently.
Thanks a lot!

Chris

From: Bruno Oliveira [mailto:nicoddemus at gmail.com]
Sent: April-09-14 5:44 PM
To: Pella,Chris
Cc: testing-in-python at lists.idyll.org
Subject: Re: [TIP] parametrize a pytest finalizer

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<mailto: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<mailto:testing-in-python at lists.idyll.org>
http://lists.idyll.org/listinfo/testing-in-python


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.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20140410/bbfa0446/attachment-0001.htm>


More information about the testing-in-python mailing list