[pytest-dev] Question about fixtures and the use of finalizers vs yield
Dr Keith M Franklin
keith.franklin at gmail.com
Mon Dec 4 08:51:20 EST 2017
I do actually normally write the finalizers function outside of the fixture so it can be reused in tests, but from what I can see that doesn’t tie you into using “addfinalizer” as I’m guessing you could just make a call the function on return from the yield.
Thanks for all the feedback, I thought that it might be the case that both are basically the same. However, I wanted to check and also see if there is a more “approved/appropriate” method or if it was just stylistic preference.
Keith
> On 4 Dec 2017, at 13:27, Floris Bruynooghe <flub at devork.be> wrote:
>
> Another often-overlooked benefit of request.addfinalizer() is that it
> can also be used in a test directly for one-off finalization:
>
> def test_foo(request):
> # arrange: set some stuff up just for this test
> request.addfinalizer(lambda: ...) # clean global state of thing setup
> # act
> # assert
>
> But as said, if you're in a fixture and only use one .addfinalizer()
> call there is no difference at all between it and the yield version.
> It's just a matter of style.
>
>
> On 4 December 2017 at 13:43, Ernesto D. Luzon Jr. <edluzonjr at gmail.com> wrote:
>>
>> PyTest fixtures using addfinalizer() become convenient when you want to make
>> them work in RobotFramework as well.
>>
>> def fixture_being_used_in_robotframework():
>> closure = "details of how to cleanup the fixture"
>>
>>
>> def clean_up():
>> print(closure)
>>
>> return clean_up
>>
>>
>> @pytest.fixture()
>> def reuse_robotframework_fixture(request):
>> request.addfinalizer(fixture_being_used_in_robotframework())
>>
>>
>> Cheers,
>> Ernesto
>>
>>
>>
>>>
>>>
>>> Message: 3
>>> Date: Mon, 4 Dec 2017 11:49:24 +0000
>>> From: Dr Keith M Franklin <keith.franklin at gmail.com>
>>> To: pytest-dev at python.org
>>> Subject: [pytest-dev] Question about fixtures and the use of
>>> finalizers vs yield
>>> Message-ID: <D7E4FF9A-6EE8-404B-8654-C1F7EE9E47A3 at gmail.com>
>>> Content-Type: text/plain; charset=utf-8
>>>
>>>
>>> Good morning/afternoon,
>>>
>>> I hope this is the right place to post this. I?ve been using PyTest for a
>>> while but recently come across someone else?s fixture which made me wonder
>>> if I?m doing this right. Basically I?ve been creating a fixture like and
>>> adding a finalizer like so:
>>>
>>> @pytest.fixture(scope=?function?)
>>> def my_fixture(request):
>>> # Code that does something e.g. creates a database record
>>> rec = DBObject()
>>>
>>> def clean_up():
>>> # Code required to clean up the fixture code and return the state
>>> back to the
>>> ...
>>> request.addfinalizer(clean_up)
>>> return rec
>>>
>>>
>>> However, I?ve seen fixtures written using yield instead e.g.
>>>
>>>
>>> @pytest.fixture(scope=?function?)
>>> def my_fixture(request):
>>> # Code that does something e.g. creates a database record
>>> rec = DBObject()
>>> yield rec
>>>
>>> # Code required to clean up the fixture code and return the state back
>>> to the
>>> ...
>>>
>>> Can I ask, is there any real difference between the two approaches? Is
>>> there a preferred method and if so why?
>>>
>>> Thanks in advance for any help.
>>>
>>> Keith
>>>
>>> ------------------------------
>>>
>>> Message: 4
>>> Date: Mon, 04 Dec 2017 12:14:42 +0000
>>> From: Bruno Oliveira <nicoddemus at gmail.com>
>>> To: Dr Keith M Franklin <keith.franklin at gmail.com>
>>> Cc: pytest-dev at python.org
>>> Subject: Re: [pytest-dev] Question about fixtures and the use of
>>> finalizers vs yield
>>> Message-ID:
>>>
>>> <CA+RQFff9Cx-P_Vs4jcU=cCQHy=mwuuRauU0vQEv7wdF=cZKv+A at mail.gmail.com>
>>> Content-Type: text/plain; charset="utf-8"
>>>
>>> Hi Keith,
>>>
>>> Both approaches are valid and identical; I personally prefer using yield
>>> because to me it feels more natural.
>>>
>>> Cheers,
>>>
>>
>>
>> _______________________________________________
>> pytest-dev mailing list
>> pytest-dev at python.org
>> https://mail.python.org/mailman/listinfo/pytest-dev
>>
> _______________________________________________
> pytest-dev mailing list
> pytest-dev at python.org
> https://mail.python.org/mailman/listinfo/pytest-dev
More information about the pytest-dev
mailing list