@@ -375,7 +375,7 @@ Fixtures are reusable
375
375
^^^^^^^^^^^^^^^^^^^^^
376
376
377
377
One of the things that makes pytest's fixture system so powerful, is that it
378
- gives us the abilty to define a generic setup step that can reused over and
378
+ gives us the ability to define a generic setup step that can reused over and
379
379
over, just like a normal function would be used. Two different tests can request
380
380
the same fixture and have pytest give each test their own result from that
381
381
fixture.
@@ -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:
@@ -844,17 +846,48 @@ Once the test is finished, pytest will go back down the list of fixtures, but in
844
846
the *reverse order *, taking each one that yielded, and running the code inside
845
847
it that was *after * the ``yield `` statement.
846
848
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.
849
+ As a simple example, consider this basic email module:
850
+
851
+ .. code-block :: python
852
+
853
+ # content of emaillib.py
854
+ class MailAdminClient :
855
+ def create_user (self ):
856
+ return MailUser()
857
+
858
+ def delete_user (self , user ):
859
+ # do some cleanup
860
+ pass
861
+
862
+
863
+ class MailUser :
864
+ def __init__ (self ):
865
+ self .inbox = []
866
+
867
+ def send_email (self , email , other ):
868
+ other.inbox.append(email)
869
+
870
+ def clear_mailbox (self ):
871
+ self .inbox.clear()
872
+
873
+
874
+ class Email :
875
+ def __init__ (self , subject , body ):
876
+ self .subject = subject
877
+ self .body = body
878
+
879
+ Let's say we want to test sending email from one user to another. We'll have to
880
+ first make each user, then send the email from one user to the other, and
881
+ finally assert that the other user received that message in their inbox. If we
882
+ want to clean up after the test runs, we'll likely have to make sure the other
883
+ user's mailbox is emptied before deleting that user, otherwise the system may
884
+ complain.
853
885
854
886
Here's what that might look like:
855
887
856
888
.. code-block :: python
857
889
890
+ # content of test_emaillib.py
858
891
import pytest
859
892
860
893
from emaillib import Email, MailAdminClient
@@ -869,17 +902,17 @@ Here's what that might look like:
869
902
def sending_user (mail_admin ):
870
903
user = mail_admin.create_user()
871
904
yield user
872
- admin_client .delete_user(user)
905
+ mail_admin .delete_user(user)
873
906
874
907
875
908
@pytest.fixture
876
909
def receiving_user (mail_admin ):
877
910
user = mail_admin.create_user()
878
911
yield user
879
- admin_client .delete_user(user)
912
+ mail_admin .delete_user(user)
880
913
881
914
882
- def test_email_received (sending_user , receiving_user , email ):
915
+ def test_email_received (sending_user , receiving_user ):
883
916
email = Email(subject = " Hey!" , body = " How's it going?" )
884
917
sending_user.send_email(email, receiving_user)
885
918
assert email in receiving_user.inbox
@@ -891,6 +924,12 @@ There is a risk that even having the order right on the teardown side of things
891
924
doesn't guarantee a safe cleanup. That's covered in a bit more detail in
892
925
:ref: `safe teardowns `.
893
926
927
+ .. code-block :: pytest
928
+
929
+ $ pytest -q test_emaillib.py
930
+ . [100%]
931
+ 1 passed in 0.12s
932
+
894
933
Handling errors for yield fixture
895
934
"""""""""""""""""""""""""""""""""
896
935
@@ -902,7 +941,7 @@ attempt to tear them down as it normally would.
902
941
2. Adding finalizers directly
903
942
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
904
943
905
- While yield fixtures are considered to be the cleaner and more straighforward
944
+ While yield fixtures are considered to be the cleaner and more straightforward
906
945
option, there is another choice, and that is to add "finalizer" functions
907
946
directly to the test's `request-context `_ object. It brings a similar result as
908
947
yield fixtures, but requires a bit more verbosity.
@@ -922,6 +961,7 @@ Here's how the previous example would look using the ``addfinalizer`` method:
922
961
923
962
.. code-block :: python
924
963
964
+ # content of test_emaillib.py
925
965
import pytest
926
966
927
967
from emaillib import Email, MailAdminClient
@@ -936,15 +976,15 @@ Here's how the previous example would look using the ``addfinalizer`` method:
936
976
def sending_user (mail_admin ):
937
977
user = mail_admin.create_user()
938
978
yield user
939
- admin_client .delete_user(user)
979
+ mail_admin .delete_user(user)
940
980
941
981
942
982
@pytest.fixture
943
983
def receiving_user (mail_admin , request ):
944
984
user = mail_admin.create_user()
945
985
946
986
def delete_user ():
947
- admin_client .delete_user(user)
987
+ mail_admin .delete_user(user)
948
988
949
989
request.addfinalizer(delete_user)
950
990
return user
@@ -956,7 +996,7 @@ Here's how the previous example would look using the ``addfinalizer`` method:
956
996
sending_user.send_email(_email, receiving_user)
957
997
958
998
def empty_mailbox ():
959
- receiving_user.delete_email(_email )
999
+ receiving_user.clear_mailbox( )
960
1000
961
1001
request.addfinalizer(empty_mailbox)
962
1002
return _email
@@ -969,6 +1009,12 @@ Here's how the previous example would look using the ``addfinalizer`` method:
969
1009
It's a bit longer than yield fixtures and a bit more complex, but it
970
1010
does offer some nuances for when you're in a pinch.
971
1011
1012
+ .. code-block :: pytest
1013
+
1014
+ $ pytest -q test_emaillib.py
1015
+ . [100%]
1016
+ 1 passed in 0.12s
1017
+
972
1018
.. _`safe teardowns` :
973
1019
974
1020
Safe teardowns
@@ -984,6 +1030,7 @@ above):
984
1030
985
1031
.. code-block :: python
986
1032
1033
+ # content of test_emaillib.py
987
1034
import pytest
988
1035
989
1036
from emaillib import Email, MailAdminClient
@@ -995,11 +1042,11 @@ above):
995
1042
sending_user = mail_admin.create_user()
996
1043
receiving_user = mail_admin.create_user()
997
1044
email = Email(subject = " Hey!" , body = " How's it going?" )
998
- sending_user.send_emai (email, receiving_user)
1045
+ sending_user.send_email (email, receiving_user)
999
1046
yield receiving_user, email
1000
- receiving_user.delete_email(email )
1001
- admin_client .delete_user(sending_user)
1002
- admin_client .delete_user(receiving_user)
1047
+ receiving_user.clear_mailbox( )
1048
+ mail_admin .delete_user(sending_user)
1049
+ mail_admin .delete_user(receiving_user)
1003
1050
1004
1051
1005
1052
def test_email_received (setup ):
@@ -1016,6 +1063,12 @@ One option might be to go with the ``addfinalizer`` method instead of yield
1016
1063
fixtures, but that might get pretty complex and difficult to maintain (and it
1017
1064
wouldn't be compact anymore).
1018
1065
1066
+ .. code-block :: pytest
1067
+
1068
+ $ pytest -q test_emaillib.py
1069
+ . [100%]
1070
+ 1 passed in 0.12s
1071
+
1019
1072
.. _`safe fixture structure` :
1020
1073
1021
1074
Safe fixture structure
@@ -1026,7 +1079,7 @@ making one state-changing action each, and then bundling them together with
1026
1079
their teardown code, as :ref: `the email examples above <yield fixtures >` showed.
1027
1080
1028
1081
The chance that a state-changing operation can fail but still modify state is
1029
- neglibible , as most of these operations tend to be `transaction `_-based (at
1082
+ negligible , as most of these operations tend to be `transaction `_-based (at
1030
1083
least at the level of testing where state could be left behind). So if we make
1031
1084
sure that any successful state-changing action gets torn down by moving it to a
1032
1085
separate fixture function and separating it from other, potentially failing
@@ -1124,7 +1177,7 @@ never have been made.
1124
1177
.. _`conftest.py` :
1125
1178
.. _`conftest` :
1126
1179
1127
- Fixture availabiility
1180
+ Fixture availability
1128
1181
---------------------
1129
1182
1130
1183
Fixture availability is determined from the perspective of the test. A fixture
@@ -1410,9 +1463,9 @@ pytest doesn't know where ``c`` should go in the case, so it should be assumed
1410
1463
that it could go anywhere between ``g `` and ``b ``.
1411
1464
1412
1465
This isn't necessarily bad, but it's something to keep in mind. If the order
1413
- they execute in could affect the behavior a test is targetting , or could
1466
+ they execute in could affect the behavior a test is targeting , or could
1414
1467
otherwise influence the result of a test, then the order should be defined
1415
- explicitely in a way that allows pytest to linearize/"flatten" that order.
1468
+ explicitly in a way that allows pytest to linearize/"flatten" that order.
1416
1469
1417
1470
.. _`autouse order` :
1418
1471
@@ -1506,7 +1559,7 @@ of what we've gone over so far.
1506
1559
1507
1560
All that's needed is stepping up to a larger scope, then having the **act **
1508
1561
step defined as an autouse fixture, and finally, making sure all the fixtures
1509
- are targetting that highler level scope.
1562
+ are targeting that higher level scope.
1510
1563
1511
1564
Let's pull :ref: `an example from above <safe fixture structure >`, and tweak it a
1512
1565
bit. Let's say that in addition to checking for a welcome message in the header,
@@ -1646,7 +1699,7 @@ again, nothing much has changed:
1646
1699
1647
1700
.. code-block :: pytest
1648
1701
1649
- $ pytest -s -q --tb=no
1702
+ $ pytest -s -q --tb=no test_module.py
1650
1703
FFfinalizing <smtplib.SMTP object at 0xdeadbeef> (smtp.gmail.com)
1651
1704
1652
1705
========================= short test summary info ==========================
@@ -1777,7 +1830,7 @@ Parametrizing fixtures
1777
1830
-----------------------------------------------------------------
1778
1831
1779
1832
Fixture functions can be parametrized in which case they will be called
1780
- multiple times, each time executing the set of dependent tests, i. e. the
1833
+ multiple times, each time executing the set of dependent tests, i.e. the
1781
1834
tests that depend on this fixture. Test functions usually do not need
1782
1835
to be aware of their re-running. Fixture parametrization helps to
1783
1836
write exhaustive functional tests for components which themselves can be
@@ -1931,11 +1984,13 @@ Running the above tests results in the following test IDs being used:
1931
1984
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
1932
1985
cachedir: $PYTHON_PREFIX/.pytest_cache
1933
1986
rootdir: $REGENDOC_TMPDIR
1934
- collected 10 items
1987
+ collected 11 items
1935
1988
1936
1989
<Module test_anothersmtp.py>
1937
1990
<Function test_showhelo[smtp.gmail.com]>
1938
1991
<Function test_showhelo[mail.python.org]>
1992
+ <Module test_emaillib.py>
1993
+ <Function test_email_received>
1939
1994
<Module test_ids.py>
1940
1995
<Function test_a[spam]>
1941
1996
<Function test_a[ham]>
@@ -1947,7 +2002,7 @@ Running the above tests results in the following test IDs being used:
1947
2002
<Function test_ehlo[mail.python.org]>
1948
2003
<Function test_noop[mail.python.org]>
1949
2004
1950
- ======================= 10 tests collected in 0.12s ========================
2005
+ ======================= 11 tests collected in 0.12s ========================
1951
2006
1952
2007
.. _`fixture-parametrize-marks` :
1953
2008
0 commit comments