@@ -844,12 +844,42 @@ Once the test is finished, pytest will go back down the list of fixtures, but in
844
844
the *reverse order *, taking each one that yielded, and running the code inside
845
845
it that was *after * the ``yield `` statement.
846
846
847
- As a simple example, let's say we want to test sending email from one user to
848
- another. We'll have to first make each user, then send the email from one user
849
- to the other, and finally assert that the other user received that message in
850
- their inbox. If we want to clean up after the test runs, we'll likely have to
851
- make sure the other user's mailbox is emptied before deleting that user,
852
- otherwise the system may complain.
847
+ As a simple example, consider this basic email module:
848
+
849
+ .. code-block :: python
850
+
851
+ # content of emaillib.py
852
+ class MailAdminClient :
853
+ def create_user (self ):
854
+ return MailUser()
855
+
856
+ def delete_user (self , user ):
857
+ # do some cleanup
858
+ pass
859
+
860
+
861
+ class MailUser :
862
+ def __init__ (self ):
863
+ self .inbox = []
864
+
865
+ def send_email (self , email , other ):
866
+ other.inbox.append(email)
867
+
868
+ def clear_mailbox (self ):
869
+ self .mailbox.clear()
870
+
871
+
872
+ class Email :
873
+ def __init__ (self , subject , body ):
874
+ self .body = body
875
+ self .subject = subject
876
+
877
+ Let's say we want to test sending email from one user to another. We'll have to
878
+ first make each user, then send the email from one user to the other, and
879
+ finally assert that the other user received that message in their inbox. If we
880
+ want to clean up after the test runs, we'll likely have to make sure the other
881
+ user's mailbox is emptied before deleting that user, otherwise the system may
882
+ complain.
853
883
854
884
Here's what that might look like:
855
885
0 commit comments