@@ -73,20 +73,20 @@ marked ``smtp`` fixture function. Running the test looks like this::
73
73
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
74
74
rootdir: $REGENDOC_TMPDIR, inifile:
75
75
collected 1 items
76
-
76
+
77
77
test_smtpsimple.py F
78
-
78
+
79
79
======= FAILURES ========
80
80
_______ test_ehlo ________
81
-
81
+
82
82
smtp = <smtplib.SMTP object at 0xdeadbeef>
83
-
83
+
84
84
def test_ehlo(smtp):
85
85
response, msg = smtp.ehlo()
86
86
assert response == 250
87
87
> assert 0 # for demo purposes
88
88
E assert 0
89
-
89
+
90
90
test_smtpsimple.py:11: AssertionError
91
91
======= 1 failed in 0.12 seconds ========
92
92
@@ -169,7 +169,7 @@ function (in or below the directory where ``conftest.py`` is located)::
169
169
response, msg = smtp.ehlo()
170
170
assert response == 250
171
171
assert b"smtp.gmail.com" in msg
172
- assert 0 # for demo purposes
172
+ assert 0 # for demo purposes
173
173
174
174
def test_noop(smtp):
175
175
response, msg = smtp.noop()
@@ -184,32 +184,32 @@ inspect what is going on and can now run the tests::
184
184
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
185
185
rootdir: $REGENDOC_TMPDIR, inifile:
186
186
collected 2 items
187
-
187
+
188
188
test_module.py FF
189
-
189
+
190
190
======= FAILURES ========
191
191
_______ test_ehlo ________
192
-
192
+
193
193
smtp = <smtplib.SMTP object at 0xdeadbeef>
194
-
194
+
195
195
def test_ehlo(smtp):
196
196
response, msg = smtp.ehlo()
197
197
assert response == 250
198
198
assert b"smtp.gmail.com" in msg
199
199
> assert 0 # for demo purposes
200
200
E assert 0
201
-
201
+
202
202
test_module.py:6: AssertionError
203
203
_______ test_noop ________
204
-
204
+
205
205
smtp = <smtplib.SMTP object at 0xdeadbeef>
206
-
206
+
207
207
def test_noop(smtp):
208
208
response, msg = smtp.noop()
209
209
assert response == 250
210
210
> assert 0 # for demo purposes
211
211
E assert 0
212
-
212
+
213
213
test_module.py:11: AssertionError
214
214
======= 2 failed in 0.12 seconds ========
215
215
@@ -260,7 +260,7 @@ Let's execute it::
260
260
261
261
$ pytest -s -q --tb=no
262
262
FFteardown smtp
263
-
263
+
264
264
2 failed in 0.12 seconds
265
265
266
266
We see that the ``smtp `` instance is finalized after the two
@@ -290,7 +290,7 @@ because the ``smtp`` object automatically closes when
290
290
the ``with `` statement ends.
291
291
292
292
Note that if an exception happens during the *setup * code (before the ``yield `` keyword), the
293
- *teardown * code (after the ``yield ``) will not be called.
293
+ *teardown * code (after the ``yield ``) will not be called.
294
294
295
295
296
296
.. note ::
@@ -302,7 +302,7 @@ Note that if an exception happens during the *setup* code (before the ``yield``
302
302
303
303
An alternative option for executing *teardown * code is to
304
304
make use of the ``addfinalizer `` method of the `request-context `_ object to register
305
- finalization functions.
305
+ finalization functions.
306
306
307
307
Here's the ``smtp `` fixture changed to use ``addfinalizer `` for cleanup:
308
308
@@ -321,7 +321,8 @@ Here's the ``smtp`` fixture changed to use ``addfinalizer`` for cleanup:
321
321
request.addfinalizer(fin)
322
322
return smtp # provide the fixture value
323
323
324
- Both ``yield `` and ``addfinalizer `` methods work similar by calling their code after the test
324
+
325
+ Both ``yield `` and ``addfinalizer `` methods work similarly by calling their code after the test
325
326
ends, but ``addfinalizer `` has two key differences over ``yield ``:
326
327
327
328
1. It is possible to register multiple finalizer functions.
@@ -340,7 +341,8 @@ ends, but ``addfinalizer`` has two key differences over ``yield``:
340
341
return r
341
342
342
343
In the example above, if ``"C28" `` fails with an exception, ``"C1" `` and ``"C3" `` will still
343
- be properly closed.
344
+ be properly closed. Of course, if an exception happens before the finalize function is
345
+ registered then it will not be executed.
344
346
345
347
346
348
.. _`request-context` :
@@ -371,7 +373,7 @@ again, nothing much has changed::
371
373
372
374
$ pytest -s -q --tb=no
373
375
FFfinalizing <smtplib.SMTP object at 0xdeadbeef> (smtp.gmail.com)
374
-
376
+
375
377
2 failed in 0.12 seconds
376
378
377
379
Let's quickly create another test module that actually sets the
@@ -439,51 +441,51 @@ So let's just do another run::
439
441
FFFF
440
442
======= FAILURES ========
441
443
_______ test_ehlo[smtp.gmail.com] ________
442
-
444
+
443
445
smtp = <smtplib.SMTP object at 0xdeadbeef>
444
-
446
+
445
447
def test_ehlo(smtp):
446
448
response, msg = smtp.ehlo()
447
449
assert response == 250
448
450
assert b"smtp.gmail.com" in msg
449
451
> assert 0 # for demo purposes
450
452
E assert 0
451
-
453
+
452
454
test_module.py:6: AssertionError
453
455
_______ test_noop[smtp.gmail.com] ________
454
-
456
+
455
457
smtp = <smtplib.SMTP object at 0xdeadbeef>
456
-
458
+
457
459
def test_noop(smtp):
458
460
response, msg = smtp.noop()
459
461
assert response == 250
460
462
> assert 0 # for demo purposes
461
463
E assert 0
462
-
464
+
463
465
test_module.py:11: AssertionError
464
466
_______ test_ehlo[mail.python.org] ________
465
-
467
+
466
468
smtp = <smtplib.SMTP object at 0xdeadbeef>
467
-
469
+
468
470
def test_ehlo(smtp):
469
471
response, msg = smtp.ehlo()
470
472
assert response == 250
471
473
> assert b"smtp.gmail.com" in msg
472
474
E AssertionError: assert b'smtp.gmail.com' in b'mail.python.org\nSIZE 51200000\nETRN\nSTARTTLS\nENHANCEDSTATUSCODES\n8BITMIME\nDSN\nSMTPUTF8'
473
-
475
+
474
476
test_module.py:5: AssertionError
475
477
-------------------------- Captured stdout setup ---------------------------
476
478
finalizing <smtplib.SMTP object at 0xdeadbeef>
477
479
_______ test_noop[mail.python.org] ________
478
-
480
+
479
481
smtp = <smtplib.SMTP object at 0xdeadbeef>
480
-
482
+
481
483
def test_noop(smtp):
482
484
response, msg = smtp.noop()
483
485
assert response == 250
484
486
> assert 0 # for demo purposes
485
487
E assert 0
486
-
488
+
487
489
test_module.py:11: AssertionError
488
490
------------------------- Captured stdout teardown -------------------------
489
491
finalizing <smtplib.SMTP object at 0xdeadbeef>
@@ -555,7 +557,7 @@ Running the above tests results in the following test IDs being used::
555
557
<Function 'test_noop[smtp.gmail.com]'>
556
558
<Function 'test_ehlo[mail.python.org]'>
557
559
<Function 'test_noop[mail.python.org]'>
558
-
560
+
559
561
======= no tests ran in 0.12 seconds ========
560
562
561
563
.. _`interdependent fixtures` :
@@ -594,10 +596,10 @@ Here we declare an ``app`` fixture which receives the previously defined
594
596
cachedir: .cache
595
597
rootdir: $REGENDOC_TMPDIR, inifile:
596
598
collecting ... collected 2 items
597
-
599
+
598
600
test_appsetup.py::test_smtp_exists[smtp.gmail.com] PASSED
599
601
test_appsetup.py::test_smtp_exists[mail.python.org] PASSED
600
-
602
+
601
603
======= 2 passed in 0.12 seconds ========
602
604
603
605
Due to the parametrization of ``smtp `` the test will run twice with two
@@ -663,40 +665,40 @@ Let's run the tests in verbose mode and with looking at the print-output::
663
665
cachedir: .cache
664
666
rootdir: $REGENDOC_TMPDIR, inifile:
665
667
collecting ... collected 8 items
666
-
668
+
667
669
test_module.py::test_0[1] SETUP otherarg 1
668
670
RUN test0 with otherarg 1
669
671
PASSED TEARDOWN otherarg 1
670
-
672
+
671
673
test_module.py::test_0[2] SETUP otherarg 2
672
674
RUN test0 with otherarg 2
673
675
PASSED TEARDOWN otherarg 2
674
-
676
+
675
677
test_module.py::test_1[mod1] SETUP modarg mod1
676
678
RUN test1 with modarg mod1
677
679
PASSED
678
680
test_module.py::test_2[1-mod1] SETUP otherarg 1
679
681
RUN test2 with otherarg 1 and modarg mod1
680
682
PASSED TEARDOWN otherarg 1
681
-
683
+
682
684
test_module.py::test_2[2-mod1] SETUP otherarg 2
683
685
RUN test2 with otherarg 2 and modarg mod1
684
686
PASSED TEARDOWN otherarg 2
685
-
687
+
686
688
test_module.py::test_1[mod2] TEARDOWN modarg mod1
687
689
SETUP modarg mod2
688
690
RUN test1 with modarg mod2
689
691
PASSED
690
692
test_module.py::test_2[1-mod2] SETUP otherarg 1
691
693
RUN test2 with otherarg 1 and modarg mod2
692
694
PASSED TEARDOWN otherarg 1
693
-
695
+
694
696
test_module.py::test_2[2-mod2] SETUP otherarg 2
695
697
RUN test2 with otherarg 2 and modarg mod2
696
698
PASSED TEARDOWN otherarg 2
697
699
TEARDOWN modarg mod2
698
-
699
-
700
+
701
+
700
702
======= 8 passed in 0.12 seconds ========
701
703
702
704
You can see that the parametrized module-scoped ``modarg `` resource caused an
0 commit comments