From e98f77037e794bf805b4dcea2a2726c503c087a9 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Fri, 8 Aug 2014 15:20:37 +0200 Subject: [PATCH] fix issue561 example adapted to python3. --- CHANGELOG | 5 ++ _pytest/__init__.py | 2 +- doc/en/fixture.txt | 141 ++++++++++++++++++++++++-------------------- setup.py | 2 +- 4 files changed, 84 insertions(+), 66 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index bcd586181..9ff564f85 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +NEXT +----------- + +- fixed issue561: adapt autouse fixture example for python3. + 2.6.1 ----------------------------------- diff --git a/_pytest/__init__.py b/_pytest/__init__.py index bd3cc04ab..39aed090e 100644 --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,2 @@ # -__version__ = '2.6.1' +__version__ = '2.6.2.dev1' diff --git a/doc/en/fixture.txt b/doc/en/fixture.txt index dab7723dd..712454964 100644 --- a/doc/en/fixture.txt +++ b/doc/en/fixture.txt @@ -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:: $ py.test test_smtpsimple.py - =========================== test session starts ============================ - platform linux -- Python 3.4.0 -- py-1.4.23 -- pytest-2.6.1 + ============================= test session starts ============================== + platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.1 collected 1 items test_smtpsimple.py F - ================================= FAILURES ================================= - ________________________________ test_ehlo _________________________________ + =================================== FAILURES =================================== + __________________________________ test_ehlo ___________________________________ - smtp = + smtp = def test_ehlo(smtp): response, msg = smtp.ehlo() assert response == 250 - > assert "merlinux" in msg - E TypeError: Type str doesn't support the buffer API + assert "merlinux" in msg + > assert 0 # for demo purposes + E assert 0 - test_smtpsimple.py:11: TypeError - ========================= 1 failed in 0.18 seconds ========================= + test_smtpsimple.py:12: AssertionError + =========================== 1 failed in 0.17 seconds =========================== In the failure traceback we see that the test function was called with a ``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:: $ py.test test_module.py - =========================== test session starts ============================ - platform linux -- Python 3.4.0 -- py-1.4.23 -- pytest-2.6.1 + ============================= test session starts ============================== + platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.1 collected 2 items test_module.py FF - ================================= FAILURES ================================= - ________________________________ test_ehlo _________________________________ + =================================== FAILURES =================================== + __________________________________ test_ehlo ___________________________________ - smtp = + smtp = def test_ehlo(smtp): response = smtp.ehlo() assert response[0] == 250 - > assert "merlinux" in response[1] - E TypeError: Type str doesn't support the buffer API + assert "merlinux" in response[1] + > assert 0 # for demo purposes + E assert 0 - test_module.py:5: TypeError - ________________________________ test_noop _________________________________ + test_module.py:6: AssertionError + __________________________________ test_noop ___________________________________ - smtp = + smtp = def test_noop(smtp): response = smtp.noop() @@ -221,7 +223,7 @@ inspect what is going on and can now run the tests:: E assert 0 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 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 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 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:: $ py.test -s -q --tb=no - FF - 2 failed in 0.19 seconds + FFteardown smtp + + 2 failed in 0.17 seconds Let's quickly create another test module that actually sets the server URL in its module namespace:: @@ -326,11 +329,11 @@ Running it:: $ py.test -qq --tb=short test_anothersmtp.py F - ================================= FAILURES ================================= - ______________________________ test_showhelo _______________________________ + =================================== FAILURES =================================== + ________________________________ test_showhelo _________________________________ test_anothersmtp.py:5: in test_showhelo 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 from the module namespace. @@ -374,21 +377,22 @@ So let's just do another run:: $ py.test -q test_module.py FFFF - ================================= FAILURES ================================= - __________________________ test_ehlo[merlinux.eu] __________________________ + =================================== FAILURES =================================== + ____________________________ test_ehlo[merlinux.eu] ____________________________ - smtp = + smtp = def test_ehlo(smtp): response = smtp.ehlo() assert response[0] == 250 - > assert "merlinux" in response[1] - E TypeError: Type str doesn't support the buffer API + assert "merlinux" in response[1] + > assert 0 # for demo purposes + E assert 0 - test_module.py:5: TypeError - __________________________ test_noop[merlinux.eu] __________________________ + test_module.py:6: AssertionError + ____________________________ test_noop[merlinux.eu] ____________________________ - smtp = + smtp = def test_noop(smtp): response = smtp.noop() @@ -397,22 +401,22 @@ So let's just do another run:: E assert 0 test_module.py:11: AssertionError - ________________________ test_ehlo[mail.python.org] ________________________ + __________________________ test_ehlo[mail.python.org] __________________________ - smtp = + smtp = def test_ehlo(smtp): response = smtp.ehlo() assert response[0] == 250 > 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 - -------------------------- Captured stdout setup --------------------------- - finalizing - ________________________ test_noop[mail.python.org] ________________________ + test_module.py:5: AssertionError + ---------------------------- Captured stdout setup ----------------------------- + finalizing + __________________________ test_noop[mail.python.org] __________________________ - smtp = + smtp = def test_noop(smtp): response = smtp.noop() @@ -421,7 +425,7 @@ So let's just do another run:: E assert 0 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 ``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:: $ py.test -v test_appsetup.py - =========================== 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 + ============================= test session starts ============================== + platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.1 -- /home/hpk/venv/0/bin/python collecting ... collected 2 items test_appsetup.py::test_smtp_exists[merlinux.eu] 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 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"]) def modarg(request): param = request.param - print "create", param + print ("create", param) def fin(): print ("fin %s" % param) return param @@ -515,30 +519,39 @@ to show the setup/teardown flow:: return request.param def test_0(otherarg): - print " test0", otherarg + print (" test0", otherarg) def test_1(modarg): - print " test1", modarg + print (" test1", 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:: $ py.test -v -s test_module.py - =========================== 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 - collecting ... collected 0 items / 1 errors + ============================= test session starts ============================== + platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.1 -- /home/hpk/venv/0/bin/python + collecting ... collected 8 items - ================================== ERRORS ================================== - _____________________ ERROR collecting test_module.py ______________________ - /home/hpk/p/pytest/.tox/regen/lib/python3.4/site-packages/_pytest/python.py:463: in _importtestmodule - mod = self.fspath.pyimport(ensuresyspath=True) - /home/hpk/p/pytest/.tox/regen/lib/python3.4/site-packages/py/_path/local.py:620: in pyimport - __import__(modname) - E File "/tmp/doc-exec-184/test_module.py", line 6 - E print "create", param - E ^ - E SyntaxError: invalid syntax - ========================= 1 error in 0.03 seconds ========================== + test_module.py::test_0[1] (' test0', 1) + PASSED + test_module.py::test_0[2] (' test0', 2) + PASSED + test_module.py::test_1[mod1] ('create', 'mod1') + (' test1', 'mod1') + PASSED + test_module.py::test_2[1-mod1] (' test2', 1, 'mod1') + PASSED + test_module.py::test_2[2-mod1] (' test2', 2, 'mod1') + 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 an ordering of test execution that lead to the fewest possible "active" resources. The finalizer for the ``mod1`` parametrized resource was executed diff --git a/setup.py b/setup.py index 638552e87..2c877151f 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ def main(): name='pytest', description='pytest: simple powerful testing with Python', long_description=long_description, - version='2.6.1', + version='2.6.2.dev1', url='http://pytest.org', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],