parent
							
								
									0a89db2739
								
							
						
					
					
						commit
						bb5f200ed7
					
				|  | @ -243,7 +243,9 @@ Fixture finalization / executing teardown code | ||||||
| 
 | 
 | ||||||
| pytest supports execution of fixture specific finalization code | pytest supports execution of fixture specific finalization code | ||||||
| when the fixture goes out of scope.  By using a ``yield`` statement instead of ``return``, all | when the fixture goes out of scope.  By using a ``yield`` statement instead of ``return``, all | ||||||
| the code after the *yield* statement serves as the teardown code.:: | the code after the *yield* statement serves as the teardown code: | ||||||
|  | 
 | ||||||
|  | .. code-block:: python | ||||||
| 
 | 
 | ||||||
|     # content of conftest.py |     # content of conftest.py | ||||||
| 
 | 
 | ||||||
|  | @ -275,22 +277,23 @@ occur around each single test.  In either case the test | ||||||
| module itself does not need to change or know about these details | module itself does not need to change or know about these details | ||||||
| of fixture setup. | of fixture setup. | ||||||
| 
 | 
 | ||||||
| Note that we can also seamlessly use the ``yield`` syntax with ``with`` statements:: | Note that we can also seamlessly use the ``yield`` syntax with ``with`` statements: | ||||||
|  | 
 | ||||||
|  | .. code-block:: python | ||||||
| 
 | 
 | ||||||
|     # content of test_yield2.py |     # content of test_yield2.py | ||||||
| 
 | 
 | ||||||
|  |     import smtplib | ||||||
|     import pytest |     import pytest | ||||||
| 
 | 
 | ||||||
|     @pytest.fixture |     @pytest.fixture(scope="module") | ||||||
|     def passwd(): |     def smtp(request): | ||||||
|         with open("/etc/passwd") as f: |         with smtplib.SMTP("smtp.gmail.com") as smtp: | ||||||
|             yield f.readlines() |             yield smtp  # provide the fixture value | ||||||
| 
 | 
 | ||||||
|     def test_has_lines(passwd): |  | ||||||
|         assert len(passwd) >= 1 |  | ||||||
| 
 | 
 | ||||||
| The file ``f`` will be closed after the test finished execution | The ``smtp`` connection will be closed after the test finished execution | ||||||
| because the Python ``file`` object supports finalization when | because the ``smtp`` object automatically closes when | ||||||
| the ``with`` statement ends. | the ``with`` statement ends. | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue