@@ -829,6 +829,8 @@ This system can be leveraged in two ways.
829
829
1. ``yield `` fixtures (recommended)
830
830
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
831
831
832
+ .. regendoc: wipe
833
+
832
834
"Yield" fixtures ``yield `` instead of ``return ``. With these
833
835
fixtures, we can run some code and pass an object back to the requesting
834
836
fixture/test, just like with the other fixtures. The only differences are:
@@ -866,13 +868,13 @@ As a simple example, consider this basic email module:
866
868
other.inbox.append(email)
867
869
868
870
def clear_mailbox (self ):
869
- self .mailbox .clear()
871
+ self .inbox .clear()
870
872
871
873
872
874
class Email :
873
875
def __init__ (self , subject , body ):
874
- self .body = body
875
876
self .subject = subject
877
+ self .body = body
876
878
877
879
Let's say we want to test sending email from one user to another. We'll have to
878
880
first 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:
885
887
886
888
.. code-block :: python
887
889
890
+ # content of test_emaillib.py
888
891
import pytest
889
892
890
893
from emaillib import Email, MailAdminClient
@@ -899,17 +902,17 @@ Here's what that might look like:
899
902
def sending_user (mail_admin ):
900
903
user = mail_admin.create_user()
901
904
yield user
902
- admin_client .delete_user(user)
905
+ mail_admin .delete_user(user)
903
906
904
907
905
908
@pytest.fixture
906
909
def receiving_user (mail_admin ):
907
910
user = mail_admin.create_user()
908
911
yield user
909
- admin_client .delete_user(user)
912
+ mail_admin .delete_user(user)
910
913
911
914
912
- def test_email_received (sending_user , receiving_user , email ):
915
+ def test_email_received (sending_user , receiving_user ):
913
916
email = Email(subject = " Hey!" , body = " How's it going?" )
914
917
sending_user.send_email(email, receiving_user)
915
918
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
921
924
doesn't guarantee a safe cleanup. That's covered in a bit more detail in
922
925
:ref: `safe teardowns `.
923
926
927
+ .. code-block :: pytest
928
+
929
+ $ pytest -q test_emaillib.py
930
+
924
931
Handling errors for yield fixture
925
932
"""""""""""""""""""""""""""""""""
926
933
@@ -952,6 +959,7 @@ Here's how the previous example would look using the ``addfinalizer`` method:
952
959
953
960
.. code-block :: python
954
961
962
+ # content of test_emaillib.py
955
963
import pytest
956
964
957
965
from emaillib import Email, MailAdminClient
@@ -966,15 +974,15 @@ Here's how the previous example would look using the ``addfinalizer`` method:
966
974
def sending_user (mail_admin ):
967
975
user = mail_admin.create_user()
968
976
yield user
969
- admin_client .delete_user(user)
977
+ mail_admin .delete_user(user)
970
978
971
979
972
980
@pytest.fixture
973
981
def receiving_user (mail_admin , request ):
974
982
user = mail_admin.create_user()
975
983
976
984
def delete_user ():
977
- admin_client .delete_user(user)
985
+ mail_admin .delete_user(user)
978
986
979
987
request.addfinalizer(delete_user)
980
988
return user
@@ -986,7 +994,7 @@ Here's how the previous example would look using the ``addfinalizer`` method:
986
994
sending_user.send_email(_email, receiving_user)
987
995
988
996
def empty_mailbox ():
989
- receiving_user.delete_email(_email )
997
+ receiving_user.clear_mailbox( )
990
998
991
999
request.addfinalizer(empty_mailbox)
992
1000
return _email
@@ -999,6 +1007,10 @@ Here's how the previous example would look using the ``addfinalizer`` method:
999
1007
It's a bit longer than yield fixtures and a bit more complex, but it
1000
1008
does offer some nuances for when you're in a pinch.
1001
1009
1010
+ .. code-block :: pytest
1011
+
1012
+ $ pytest -q test_emaillib.py
1013
+
1002
1014
.. _`safe teardowns` :
1003
1015
1004
1016
Safe teardowns
@@ -1014,6 +1026,7 @@ above):
1014
1026
1015
1027
.. code-block :: python
1016
1028
1029
+ # content of test_emaillib.py
1017
1030
import pytest
1018
1031
1019
1032
from emaillib import Email, MailAdminClient
@@ -1025,11 +1038,11 @@ above):
1025
1038
sending_user = mail_admin.create_user()
1026
1039
receiving_user = mail_admin.create_user()
1027
1040
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)
1029
1042
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)
1033
1046
1034
1047
1035
1048
def test_email_received (setup ):
@@ -1046,6 +1059,10 @@ One option might be to go with the ``addfinalizer`` method instead of yield
1046
1059
fixtures, but that might get pretty complex and difficult to maintain (and it
1047
1060
wouldn't be compact anymore).
1048
1061
1062
+ .. code-block :: pytest
1063
+
1064
+ $ pytest -q test_emaillib.py
1065
+
1049
1066
.. _`safe fixture structure` :
1050
1067
1051
1068
Safe fixture structure
@@ -1676,7 +1693,7 @@ again, nothing much has changed:
1676
1693
1677
1694
.. code-block :: pytest
1678
1695
1679
- $ pytest -s -q --tb=no
1696
+ $ pytest -s -q --tb=no test_module.py
1680
1697
FFfinalizing <smtplib.SMTP object at 0xdeadbeef> (smtp.gmail.com)
1681
1698
1682
1699
========================= short test summary info ==========================
0 commit comments