fix issue561 example adapted to python3.
This commit is contained in:
		
							parent
							
								
									672e42e558
								
							
						
					
					
						commit
						e98f77037e
					
				|  | @ -1,3 +1,8 @@ | ||||||
|  | NEXT | ||||||
|  | ----------- | ||||||
|  | 
 | ||||||
|  | - fixed issue561: adapt autouse fixture example for python3. | ||||||
|  | 
 | ||||||
| 2.6.1 | 2.6.1 | ||||||
| ----------------------------------- | ----------------------------------- | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,2 +1,2 @@ | ||||||
| # | # | ||||||
| __version__ = '2.6.1' | __version__ = '2.6.2.dev1' | ||||||
|  |  | ||||||
|  | @ -75,25 +75,26 @@ will discover and call the :py:func:`@pytest.fixture <_pytest.python.fixture>` | ||||||
| marked ``smtp`` fixture function.  Running the test looks like this:: | marked ``smtp`` fixture function.  Running the test looks like this:: | ||||||
| 
 | 
 | ||||||
|     $ py.test test_smtpsimple.py |     $ py.test test_smtpsimple.py | ||||||
|     =========================== test session starts ============================ |     ============================= test session starts ============================== | ||||||
|     platform linux -- Python 3.4.0 -- py-1.4.23 -- pytest-2.6.1 |     platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.1 | ||||||
|     collected 1 items |     collected 1 items | ||||||
|      |      | ||||||
|     test_smtpsimple.py F |     test_smtpsimple.py F | ||||||
|      |      | ||||||
|     ================================= FAILURES ================================= |     =================================== FAILURES =================================== | ||||||
|     ________________________________ test_ehlo _________________________________ |     __________________________________ test_ehlo ___________________________________ | ||||||
|      |      | ||||||
|     smtp = <smtplib.SMTP object at 0x2ba44047c390> |     smtp = <smtplib.SMTP instance at 0x7ff89e00d7e8> | ||||||
|      |      | ||||||
|         def test_ehlo(smtp): |         def test_ehlo(smtp): | ||||||
|             response, msg = smtp.ehlo() |             response, msg = smtp.ehlo() | ||||||
|             assert response == 250 |             assert response == 250 | ||||||
|     >       assert "merlinux" in msg |             assert "merlinux" in msg | ||||||
|     E       TypeError: Type str doesn't support the buffer API |     >       assert 0 # for demo purposes | ||||||
|  |     E       assert 0 | ||||||
|      |      | ||||||
|     test_smtpsimple.py:11: TypeError |     test_smtpsimple.py:12: AssertionError | ||||||
|     ========================= 1 failed in 0.18 seconds ========================= |     =========================== 1 failed in 0.17 seconds =========================== | ||||||
| 
 | 
 | ||||||
| In the failure traceback we see that the test function was called with a | In the failure traceback we see that the test function was called with a | ||||||
| ``smtp`` argument, the ``smtplib.SMTP()`` instance created by the fixture | ``smtp`` argument, the ``smtplib.SMTP()`` instance created by the fixture | ||||||
|  | @ -192,27 +193,28 @@ We deliberately insert failing ``assert 0`` statements in order to | ||||||
| inspect what is going on and can now run the tests:: | inspect what is going on and can now run the tests:: | ||||||
| 
 | 
 | ||||||
|     $ py.test test_module.py |     $ py.test test_module.py | ||||||
|     =========================== test session starts ============================ |     ============================= test session starts ============================== | ||||||
|     platform linux -- Python 3.4.0 -- py-1.4.23 -- pytest-2.6.1 |     platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.1 | ||||||
|     collected 2 items |     collected 2 items | ||||||
|      |      | ||||||
|     test_module.py FF |     test_module.py FF | ||||||
|      |      | ||||||
|     ================================= FAILURES ================================= |     =================================== FAILURES =================================== | ||||||
|     ________________________________ test_ehlo _________________________________ |     __________________________________ test_ehlo ___________________________________ | ||||||
|      |      | ||||||
|     smtp = <smtplib.SMTP object at 0x2b37ace44fd0> |     smtp = <smtplib.SMTP instance at 0x7f36c67e3ab8> | ||||||
|      |      | ||||||
|         def test_ehlo(smtp): |         def test_ehlo(smtp): | ||||||
|             response = smtp.ehlo() |             response = smtp.ehlo() | ||||||
|             assert response[0] == 250 |             assert response[0] == 250 | ||||||
|     >       assert "merlinux" in response[1] |             assert "merlinux" in response[1] | ||||||
|     E       TypeError: Type str doesn't support the buffer API |     >       assert 0  # for demo purposes | ||||||
|  |     E       assert 0 | ||||||
|      |      | ||||||
|     test_module.py:5: TypeError |     test_module.py:6: AssertionError | ||||||
|     ________________________________ test_noop _________________________________ |     __________________________________ test_noop ___________________________________ | ||||||
|      |      | ||||||
|     smtp = <smtplib.SMTP object at 0x2b37ace44fd0> |     smtp = <smtplib.SMTP instance at 0x7f36c67e3ab8> | ||||||
|      |      | ||||||
|         def test_noop(smtp): |         def test_noop(smtp): | ||||||
|             response = smtp.noop() |             response = smtp.noop() | ||||||
|  | @ -221,7 +223,7 @@ inspect what is going on and can now run the tests:: | ||||||
|     E       assert 0 |     E       assert 0 | ||||||
|      |      | ||||||
|     test_module.py:11: AssertionError |     test_module.py:11: AssertionError | ||||||
|     ========================= 2 failed in 0.18 seconds ========================= |     =========================== 2 failed in 0.17 seconds =========================== | ||||||
| 
 | 
 | ||||||
| You see the two ``assert 0`` failing and more importantly you can also see | You see the two ``assert 0`` failing and more importantly you can also see | ||||||
| that the same (module-scoped) ``smtp`` object was passed into the two | that the same (module-scoped) ``smtp`` object was passed into the two | ||||||
|  | @ -269,7 +271,7 @@ Let's execute it:: | ||||||
|     $ py.test -s -q --tb=no |     $ py.test -s -q --tb=no | ||||||
|     FFteardown smtp |     FFteardown smtp | ||||||
|      |      | ||||||
|     2 failed in 0.22 seconds |     2 failed in 0.16 seconds | ||||||
| 
 | 
 | ||||||
| We see that the ``smtp`` instance is finalized after the two | We see that the ``smtp`` instance is finalized after the two | ||||||
| tests finished execution.  Note that if we decorated our fixture | tests finished execution.  Note that if we decorated our fixture | ||||||
|  | @ -309,8 +311,9 @@ We use the ``request.module`` attribute to optionally obtain an | ||||||
| again, nothing much has changed:: | again, nothing much has changed:: | ||||||
| 
 | 
 | ||||||
|     $ py.test -s -q --tb=no |     $ py.test -s -q --tb=no | ||||||
|     FF |     FFteardown smtp | ||||||
|     2 failed in 0.19 seconds |      | ||||||
|  |     2 failed in 0.17 seconds | ||||||
| 
 | 
 | ||||||
| Let's quickly create another test module that actually sets the | Let's quickly create another test module that actually sets the | ||||||
| server URL in its module namespace:: | server URL in its module namespace:: | ||||||
|  | @ -326,11 +329,11 @@ Running it:: | ||||||
| 
 | 
 | ||||||
|     $ py.test -qq --tb=short test_anothersmtp.py |     $ py.test -qq --tb=short test_anothersmtp.py | ||||||
|     F |     F | ||||||
|     ================================= FAILURES ================================= |     =================================== FAILURES =================================== | ||||||
|     ______________________________ test_showhelo _______________________________ |     ________________________________ test_showhelo _________________________________ | ||||||
|     test_anothersmtp.py:5: in test_showhelo |     test_anothersmtp.py:5: in test_showhelo | ||||||
|         assert 0, smtp.helo() |         assert 0, smtp.helo() | ||||||
|     E   AssertionError: (250, b'mail.python.org') |     E   AssertionError: (250, 'hq.merlinux.eu') | ||||||
| 
 | 
 | ||||||
| voila! The ``smtp`` fixture function picked up our mail server name | voila! The ``smtp`` fixture function picked up our mail server name | ||||||
| from the module namespace. | from the module namespace. | ||||||
|  | @ -374,21 +377,22 @@ So let's just do another run:: | ||||||
| 
 | 
 | ||||||
|     $ py.test -q test_module.py |     $ py.test -q test_module.py | ||||||
|     FFFF |     FFFF | ||||||
|     ================================= FAILURES ================================= |     =================================== FAILURES =================================== | ||||||
|     __________________________ test_ehlo[merlinux.eu] __________________________ |     ____________________________ test_ehlo[merlinux.eu] ____________________________ | ||||||
|      |      | ||||||
|     smtp = <smtplib.SMTP object at 0x2b5a2bc0e048> |     smtp = <smtplib.SMTP instance at 0x7f4fdf04cea8> | ||||||
|      |      | ||||||
|         def test_ehlo(smtp): |         def test_ehlo(smtp): | ||||||
|             response = smtp.ehlo() |             response = smtp.ehlo() | ||||||
|             assert response[0] == 250 |             assert response[0] == 250 | ||||||
|     >       assert "merlinux" in response[1] |             assert "merlinux" in response[1] | ||||||
|     E       TypeError: Type str doesn't support the buffer API |     >       assert 0  # for demo purposes | ||||||
|  |     E       assert 0 | ||||||
|      |      | ||||||
|     test_module.py:5: TypeError |     test_module.py:6: AssertionError | ||||||
|     __________________________ test_noop[merlinux.eu] __________________________ |     ____________________________ test_noop[merlinux.eu] ____________________________ | ||||||
|      |      | ||||||
|     smtp = <smtplib.SMTP object at 0x2b5a2bc0e048> |     smtp = <smtplib.SMTP instance at 0x7f4fdf04cea8> | ||||||
|      |      | ||||||
|         def test_noop(smtp): |         def test_noop(smtp): | ||||||
|             response = smtp.noop() |             response = smtp.noop() | ||||||
|  | @ -397,22 +401,22 @@ So let's just do another run:: | ||||||
|     E       assert 0 |     E       assert 0 | ||||||
|      |      | ||||||
|     test_module.py:11: AssertionError |     test_module.py:11: AssertionError | ||||||
|     ________________________ test_ehlo[mail.python.org] ________________________ |     __________________________ test_ehlo[mail.python.org] __________________________ | ||||||
|      |      | ||||||
|     smtp = <smtplib.SMTP object at 0x2b5a2bdb3ac8> |     smtp = <smtplib.SMTP instance at 0x7f4fdf07c290> | ||||||
|      |      | ||||||
|         def test_ehlo(smtp): |         def test_ehlo(smtp): | ||||||
|             response = smtp.ehlo() |             response = smtp.ehlo() | ||||||
|             assert response[0] == 250 |             assert response[0] == 250 | ||||||
|     >       assert "merlinux" in response[1] |     >       assert "merlinux" in response[1] | ||||||
|     E       TypeError: Type str doesn't support the buffer API |     E       assert 'merlinux' in 'mail.python.org\nSIZE 25600000\nETRN\nSTARTTLS\nENHANCEDSTATUSCODES\n8BITMIME\nDSN\nSMTPUTF8' | ||||||
|      |      | ||||||
|     test_module.py:5: TypeError |     test_module.py:5: AssertionError | ||||||
|     -------------------------- Captured stdout setup --------------------------- |     ---------------------------- Captured stdout setup ----------------------------- | ||||||
|     finalizing <smtplib.SMTP object at 0x2b5a2bc0e048> |     finalizing <smtplib.SMTP instance at 0x7f4fdf04cea8> | ||||||
|     ________________________ test_noop[mail.python.org] ________________________ |     __________________________ test_noop[mail.python.org] __________________________ | ||||||
|      |      | ||||||
|     smtp = <smtplib.SMTP object at 0x2b5a2bdb3ac8> |     smtp = <smtplib.SMTP instance at 0x7f4fdf07c290> | ||||||
|      |      | ||||||
|         def test_noop(smtp): |         def test_noop(smtp): | ||||||
|             response = smtp.noop() |             response = smtp.noop() | ||||||
|  | @ -421,7 +425,7 @@ So let's just do another run:: | ||||||
|     E       assert 0 |     E       assert 0 | ||||||
|      |      | ||||||
|     test_module.py:11: AssertionError |     test_module.py:11: AssertionError | ||||||
|     4 failed in 6.25 seconds |     4 failed in 6.26 seconds | ||||||
| 
 | 
 | ||||||
| We see that our two test functions each ran twice, against the different | We see that our two test functions each ran twice, against the different | ||||||
| ``smtp`` instances.  Note also, that with the ``mail.python.org`` | ``smtp`` instances.  Note also, that with the ``mail.python.org`` | ||||||
|  | @ -460,14 +464,14 @@ Here we declare an ``app`` fixture which receives the previously defined | ||||||
| ``smtp`` fixture and instantiates an ``App`` object with it.  Let's run it:: | ``smtp`` fixture and instantiates an ``App`` object with it.  Let's run it:: | ||||||
| 
 | 
 | ||||||
|     $ py.test -v test_appsetup.py |     $ py.test -v test_appsetup.py | ||||||
|     =========================== test session starts ============================ |     ============================= test session starts ============================== | ||||||
|     platform linux -- Python 3.4.0 -- py-1.4.23 -- pytest-2.6.1 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4 |     platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.1 -- /home/hpk/venv/0/bin/python | ||||||
|     collecting ... collected 2 items |     collecting ... collected 2 items | ||||||
|      |      | ||||||
|     test_appsetup.py::test_smtp_exists[merlinux.eu] PASSED |     test_appsetup.py::test_smtp_exists[merlinux.eu] PASSED | ||||||
|     test_appsetup.py::test_smtp_exists[mail.python.org] PASSED |     test_appsetup.py::test_smtp_exists[mail.python.org] PASSED | ||||||
|      |      | ||||||
|     ========================= 2 passed in 5.90 seconds ========================= |     =========================== 2 passed in 5.46 seconds =========================== | ||||||
| 
 | 
 | ||||||
| Due to the parametrization of ``smtp`` the test will run twice with two | Due to the parametrization of ``smtp`` the test will run twice with two | ||||||
| different ``App`` instances and respective smtp servers.  There is no | different ``App`` instances and respective smtp servers.  There is no | ||||||
|  | @ -505,7 +509,7 @@ to show the setup/teardown flow:: | ||||||
|     @pytest.fixture(scope="module", params=["mod1", "mod2"]) |     @pytest.fixture(scope="module", params=["mod1", "mod2"]) | ||||||
|     def modarg(request): |     def modarg(request): | ||||||
|         param = request.param |         param = request.param | ||||||
|         print "create", param |         print ("create", param) | ||||||
|         def fin(): |         def fin(): | ||||||
|             print ("fin %s" % param) |             print ("fin %s" % param) | ||||||
|         return param |         return param | ||||||
|  | @ -515,30 +519,39 @@ to show the setup/teardown flow:: | ||||||
|         return request.param |         return request.param | ||||||
| 
 | 
 | ||||||
|     def test_0(otherarg): |     def test_0(otherarg): | ||||||
|         print "  test0", otherarg |         print ("  test0", otherarg) | ||||||
|     def test_1(modarg): |     def test_1(modarg): | ||||||
|         print "  test1", modarg |         print ("  test1", modarg) | ||||||
|     def test_2(otherarg, modarg): |     def test_2(otherarg, modarg): | ||||||
|         print "  test2", otherarg, modarg |         print ("  test2", otherarg, modarg) | ||||||
| 
 | 
 | ||||||
| Let's run the tests in verbose mode and with looking at the print-output:: | Let's run the tests in verbose mode and with looking at the print-output:: | ||||||
| 
 | 
 | ||||||
|     $ py.test -v -s test_module.py |     $ py.test -v -s test_module.py | ||||||
|     =========================== test session starts ============================ |     ============================= test session starts ============================== | ||||||
|     platform linux -- Python 3.4.0 -- py-1.4.23 -- pytest-2.6.1 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4 |     platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.1 -- /home/hpk/venv/0/bin/python | ||||||
|     collecting ... collected 0 items / 1 errors |     collecting ... collected 8 items | ||||||
|      |      | ||||||
|     ================================== ERRORS ================================== |     test_module.py::test_0[1] ('  test0', 1) | ||||||
|     _____________________ ERROR collecting test_module.py ______________________ |     PASSED | ||||||
|     /home/hpk/p/pytest/.tox/regen/lib/python3.4/site-packages/_pytest/python.py:463: in _importtestmodule |     test_module.py::test_0[2] ('  test0', 2) | ||||||
|         mod = self.fspath.pyimport(ensuresyspath=True) |     PASSED | ||||||
|     /home/hpk/p/pytest/.tox/regen/lib/python3.4/site-packages/py/_path/local.py:620: in pyimport |     test_module.py::test_1[mod1] ('create', 'mod1') | ||||||
|         __import__(modname) |     ('  test1', 'mod1') | ||||||
|     E     File "/tmp/doc-exec-184/test_module.py", line 6 |     PASSED | ||||||
|     E       print "create", param |     test_module.py::test_2[1-mod1] ('  test2', 1, 'mod1') | ||||||
|     E                    ^ |     PASSED | ||||||
|     E   SyntaxError: invalid syntax |     test_module.py::test_2[2-mod1] ('  test2', 2, 'mod1') | ||||||
|     ========================= 1 error in 0.03 seconds ========================== |     PASSED | ||||||
|  |     test_module.py::test_1[mod2] ('create', 'mod2') | ||||||
|  |     ('  test1', 'mod2') | ||||||
|  |     PASSED | ||||||
|  |     test_module.py::test_2[1-mod2] ('  test2', 1, 'mod2') | ||||||
|  |     PASSED | ||||||
|  |     test_module.py::test_2[2-mod2] ('  test2', 2, 'mod2') | ||||||
|  |     PASSED | ||||||
|  |      | ||||||
|  |     =========================== 8 passed in 0.01 seconds =========================== | ||||||
| 
 | 
 | ||||||
| You can see that the parametrized module-scoped ``modarg`` resource caused | You can see that the parametrized module-scoped ``modarg`` resource caused | ||||||
| an ordering of test execution that lead to the fewest possible "active" resources. The finalizer for the ``mod1`` parametrized resource was executed | an ordering of test execution that lead to the fewest possible "active" resources. The finalizer for the ``mod1`` parametrized resource was executed | ||||||
|  |  | ||||||
							
								
								
									
										2
									
								
								setup.py
								
								
								
								
							
							
						
						
									
										2
									
								
								setup.py
								
								
								
								
							|  | @ -27,7 +27,7 @@ def main(): | ||||||
|         name='pytest', |         name='pytest', | ||||||
|         description='pytest: simple powerful testing with Python', |         description='pytest: simple powerful testing with Python', | ||||||
|         long_description=long_description, |         long_description=long_description, | ||||||
|         version='2.6.1', |         version='2.6.2.dev1', | ||||||
|         url='http://pytest.org', |         url='http://pytest.org', | ||||||
|         license='MIT license', |         license='MIT license', | ||||||
|         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], |         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue