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.frank...@gmail.com> > To: pytest-dev@python.org > Subject: [pytest-dev] Question about fixtures and the use of > finalizers vs yield > Message-ID: <d7e4ff9a-6ee8-404b-8654-c1f7ee9e4...@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 <nicodde...@gmail.com> > To: Dr Keith M Franklin <keith.frank...@gmail.com> > Cc: pytest-dev@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@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@python.org https://mail.python.org/mailman/listinfo/pytest-dev