@@ -829,6 +829,8 @@ This system can be leveraged in two ways.
8298291. ``yield `` fixtures (recommended)
830830^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
831831
832+ .. regendoc: wipe
833+
832834 "Yield" fixtures ``yield `` instead of ``return ``. With these
833835fixtures, we can run some code and pass an object back to the requesting
834836fixture/test, just like with the other fixtures. The only differences are:
@@ -866,13 +868,13 @@ As a simple example, consider this basic email module:
866868 other.inbox.append(email)
867869
868870 def clear_mailbox (self ):
869- self .mailbox .clear()
871+ self .inbox .clear()
870872
871873
872874 class Email :
873875 def __init__ (self , subject , body ):
874- self .body = body
875876 self .subject = subject
877+ self .body = body
876878
877879 Let's say we want to test sending email from one user to another. We'll have to
878880first make each user, then send the email from one user to the other, and
@@ -885,6 +887,7 @@ Here's what that might look like:
885887
886888.. code-block :: python
887889
890+ # content of test_emaillib.py
888891 import pytest
889892
890893 from emaillib import Email, MailAdminClient
@@ -899,17 +902,17 @@ Here's what that might look like:
899902 def sending_user (mail_admin ):
900903 user = mail_admin.create_user()
901904 yield user
902- admin_client .delete_user(user)
905+ mail_admin .delete_user(user)
903906
904907
905908 @pytest.fixture
906909 def receiving_user (mail_admin ):
907910 user = mail_admin.create_user()
908911 yield user
909- admin_client .delete_user(user)
912+ mail_admin .delete_user(user)
910913
911914
912- def test_email_received (sending_user , receiving_user , email ):
915+ def test_email_received (sending_user , receiving_user ):
913916 email = Email(subject = " Hey!" , body = " How's it going?" )
914917 sending_user.send_email(email, receiving_user)
915918 assert email in receiving_user.inbox
@@ -921,6 +924,10 @@ There is a risk that even having the order right on the teardown side of things
921924doesn't guarantee a safe cleanup. That's covered in a bit more detail in
922925:ref: `safe teardowns `.
923926
927+ .. code-block :: pytest
928+
929+ $ pytest -q test_emaillib.py
930+
924931 Handling errors for yield fixture
925932"""""""""""""""""""""""""""""""""
926933
@@ -952,6 +959,7 @@ Here's how the previous example would look using the ``addfinalizer`` method:
952959
953960.. code-block :: python
954961
962+ # content of test_emaillib.py
955963 import pytest
956964
957965 from emaillib import Email, MailAdminClient
@@ -966,15 +974,15 @@ Here's how the previous example would look using the ``addfinalizer`` method:
966974 def sending_user (mail_admin ):
967975 user = mail_admin.create_user()
968976 yield user
969- admin_client .delete_user(user)
977+ mail_admin .delete_user(user)
970978
971979
972980 @pytest.fixture
973981 def receiving_user (mail_admin , request ):
974982 user = mail_admin.create_user()
975983
976984 def delete_user ():
977- admin_client .delete_user(user)
985+ mail_admin .delete_user(user)
978986
979987 request.addfinalizer(delete_user)
980988 return user
@@ -986,7 +994,7 @@ Here's how the previous example would look using the ``addfinalizer`` method:
986994 sending_user.send_email(_email, receiving_user)
987995
988996 def empty_mailbox ():
989- receiving_user.delete_email(_email )
997+ receiving_user.clear_mailbox( )
990998
991999 request.addfinalizer(empty_mailbox)
9921000 return _email
@@ -999,6 +1007,10 @@ Here's how the previous example would look using the ``addfinalizer`` method:
9991007 It's a bit longer than yield fixtures and a bit more complex, but it
10001008does offer some nuances for when you're in a pinch.
10011009
1010+ .. code-block :: pytest
1011+
1012+ $ pytest -q test_emaillib.py
1013+
10021014 .. _`safe teardowns` :
10031015
10041016Safe teardowns
@@ -1014,6 +1026,7 @@ above):
10141026
10151027.. code-block :: python
10161028
1029+ # content of test_emaillib.py
10171030 import pytest
10181031
10191032 from emaillib import Email, MailAdminClient
@@ -1025,11 +1038,11 @@ above):
10251038 sending_user = mail_admin.create_user()
10261039 receiving_user = mail_admin.create_user()
10271040 email = Email(subject = " Hey!" , body = " How's it going?" )
1028- sending_user.send_emai (email, receiving_user)
1041+ sending_user.send_email (email, receiving_user)
10291042 yield receiving_user, email
1030- receiving_user.delete_email(email )
1031- admin_client .delete_user(sending_user)
1032- admin_client .delete_user(receiving_user)
1043+ receiving_user.clear_mailbox( )
1044+ mail_admin .delete_user(sending_user)
1045+ mail_admin .delete_user(receiving_user)
10331046
10341047
10351048 def test_email_received (setup ):
@@ -1046,6 +1059,10 @@ One option might be to go with the ``addfinalizer`` method instead of yield
10461059fixtures, but that might get pretty complex and difficult to maintain (and it
10471060wouldn't be compact anymore).
10481061
1062+ .. code-block :: pytest
1063+
1064+ $ pytest -q test_emaillib.py
1065+
10491066 .. _`safe fixture structure` :
10501067
10511068Safe fixture structure
@@ -1676,7 +1693,7 @@ again, nothing much has changed:
16761693
16771694.. code-block :: pytest
16781695
1679- $ pytest -s -q --tb=no
1696+ $ pytest -s -q --tb=no test_module.py
16801697 FFfinalizing <smtplib.SMTP object at 0xdeadbeef> (smtp.gmail.com)
16811698
16821699 ========================= short test summary info ==========================
0 commit comments