From b48f1d378b4a9f96425aac848eacf3ef1ee1cce5 Mon Sep 17 00:00:00 2001 From: Hugo Martins Date: Sat, 30 Jun 2018 02:01:49 +0100 Subject: [PATCH 1/7] Clarify examples in fixtures' documentation --- changelog/3592.doc.rst | 1 + doc/en/fixture.rst | 155 +++++++++++++++++++++-------------------- 2 files changed, 79 insertions(+), 77 deletions(-) create mode 100644 changelog/3592.doc.rst diff --git a/changelog/3592.doc.rst b/changelog/3592.doc.rst new file mode 100644 index 000000000..cbdf99666 --- /dev/null +++ b/changelog/3592.doc.rst @@ -0,0 +1 @@ +Clarify confusing examples in fixtures' documentation. \ No newline at end of file diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index e07d00eaa..c48caf0af 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -55,18 +55,18 @@ using it:: import pytest @pytest.fixture - def smtp(): + def smtp_connection(): import smtplib return smtplib.SMTP("smtp.gmail.com", 587, timeout=5) - def test_ehlo(smtp): - response, msg = smtp.ehlo() + def test_ehlo(smtp_connection): + response, msg = smtp_connection.ehlo() assert response == 250 assert 0 # for demo purposes -Here, the ``test_ehlo`` needs the ``smtp`` fixture value. pytest +Here, the ``test_ehlo`` needs the ``smtp_connection`` fixture value. pytest will discover and call the :py:func:`@pytest.fixture <_pytest.python.fixture>` -marked ``smtp`` fixture function. Running the test looks like this:: +marked ``smtp_connection`` fixture function. Running the test looks like this:: $ pytest test_smtpsimple.py =========================== test session starts ============================ @@ -81,8 +81,8 @@ marked ``smtp`` fixture function. Running the test looks like this:: smtp = - def test_ehlo(smtp): - response, msg = smtp.ehlo() + def test_ehlo(smtp_connection): + response, msg = smtp_connection.ehlo() assert response == 250 > assert 0 # for demo purposes E assert 0 @@ -91,16 +91,16 @@ marked ``smtp`` fixture function. Running the test looks like this:: ========================= 1 failed in 0.12 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 +``smtp_connection`` argument, the ``smtplib.SMTP()`` instance created by the fixture function. The test function fails on our deliberate ``assert 0``. Here is the exact protocol used by ``pytest`` to call the test function this way: 1. pytest :ref:`finds ` the ``test_ehlo`` because of the ``test_`` prefix. The test function needs a function argument - named ``smtp``. A matching fixture function is discovered by - looking for a fixture-marked function named ``smtp``. + named ``smtp_connection``. A matching fixture function is discovered by + looking for a fixture-marked function named ``smtp_connection``. -2. ``smtp()`` is called to create an instance. +2. ``smtp_connection()`` is called to create an instance. 3. ``test_ehlo()`` is called and fails in the last line of the test function. @@ -167,10 +167,10 @@ Fixtures requiring network access depend on connectivity and are usually time-expensive to create. Extending the previous example, we can add a ``scope="module"`` parameter to the :py:func:`@pytest.fixture <_pytest.python.fixture>` invocation -to cause the decorated ``smtp`` fixture function to only be invoked once -per test *module* (the default is to invoke once per test *function*). +to cause the decorated ``smtp_connection`` fixture function to only be invoked +once per test *module* (the default is to invoke once per test *function*). Multiple test functions in a test module will thus -each receive the same ``smtp`` fixture instance, thus saving time. +each receive the same ``smtp_connection`` fixture instance, thus saving time. The next example puts the fixture function into a separate ``conftest.py`` file so that tests from multiple test modules in the directory can @@ -181,23 +181,24 @@ access the fixture function:: import smtplib @pytest.fixture(scope="module") - def smtp(): + def smtp_connection(): return smtplib.SMTP("smtp.gmail.com", 587, timeout=5) -The name of the fixture again is ``smtp`` and you can access its result by -listing the name ``smtp`` as an input parameter in any test or fixture -function (in or below the directory where ``conftest.py`` is located):: +The name of the fixture again is ``smtp_connection`` and you can access its +result by listing the name ``smtp_connection`` as an input parameter in any +test or fixture function (in or below the directory where ``conftest.py`` is +located):: # content of test_module.py - def test_ehlo(smtp): - response, msg = smtp.ehlo() + def test_ehlo(smtp_connection): + response, msg = smtp_connection.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg assert 0 # for demo purposes - def test_noop(smtp): - response, msg = smtp.noop() + def test_noop(smtp_connection): + response, msg = smtp_connection.noop() assert response == 250 assert 0 # for demo purposes @@ -217,8 +218,8 @@ inspect what is going on and can now run the tests:: smtp = - def test_ehlo(smtp): - response, msg = smtp.ehlo() + def test_ehlo(smtp_connection): + response, msg = smtp_connection.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg > assert 0 # for demo purposes @@ -227,10 +228,10 @@ inspect what is going on and can now run the tests:: test_module.py:6: AssertionError ________________________________ test_noop _________________________________ - smtp = + smtp_connection = - def test_noop(smtp): - response, msg = smtp.noop() + def test_noop(smtp_connection): + response, msg = smtp_connection.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 @@ -239,18 +240,18 @@ inspect what is going on and can now run the tests:: ========================= 2 failed in 0.12 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 -test functions because pytest shows the incoming argument values in the -traceback. As a result, the two test functions using ``smtp`` run as -quick as a single one because they reuse the same instance. +that the same (module-scoped) ``smtp_connection`` object was passed into the +two test functions because pytest shows the incoming argument values in the +traceback. As a result, the two test functions using ``smtp_connection`` run +as quick as a single one because they reuse the same instance. -If you decide that you rather want to have a session-scoped ``smtp`` +If you decide that you rather want to have a session-scoped ``smtp_connection`` instance, you can simply declare it: .. code-block:: python @pytest.fixture(scope="session") - def smtp(): + def smtp_connection(): # the returned fixture value will be shared for # all tests needing it ... @@ -323,9 +324,9 @@ the code after the *yield* statement serves as the teardown code: @pytest.fixture(scope="module") - def smtp(): + def smtp_connection(): smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) - yield smtp # provide the fixture value + yield smtp_connection # provide the fixture value print("teardown smtp") smtp.close() @@ -336,11 +337,11 @@ tests. Let's execute it:: $ pytest -s -q --tb=no - FFteardown smtp + FFteardown smtp_connection 2 failed in 0.12 seconds -We see that the ``smtp`` instance is finalized after the two +We see that the ``smtp_connection`` instance is finalized after the two tests finished execution. Note that if we decorated our fixture function with ``scope='function'`` then fixture setup and cleanup would occur around each single test. In either case the test @@ -358,13 +359,13 @@ Note that we can also seamlessly use the ``yield`` syntax with ``with`` statemen @pytest.fixture(scope="module") - def smtp(): + def smtp_connection(): with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp: - yield smtp # provide the fixture value + yield smtp_connection # provide the fixture value -The ``smtp`` connection will be closed after the test finished execution -because the ``smtp`` object automatically closes when +The ``smtp_connection`` connection will be closed after the test finished +execution because the ``smtp_connection`` object automatically closes when the ``with`` statement ends. Note that if an exception happens during the *setup* code (before the ``yield`` keyword), the @@ -374,7 +375,7 @@ An alternative option for executing *teardown* code is to make use of the ``addfinalizer`` method of the `request-context`_ object to register finalization functions. -Here's the ``smtp`` fixture changed to use ``addfinalizer`` for cleanup: +Here's the ``smtp_connection`` fixture changed to use ``addfinalizer`` for cleanup: .. code-block:: python @@ -384,7 +385,7 @@ Here's the ``smtp`` fixture changed to use ``addfinalizer`` for cleanup: @pytest.fixture(scope="module") - def smtp(request): + def smtp_connection(request): smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) def fin(): @@ -425,7 +426,7 @@ Fixtures can introspect the requesting test context Fixture functions can accept the :py:class:`request ` object to introspect the "requesting" test function, class or module context. -Further extending the previous ``smtp`` fixture example, let's +Further extending the previous ``smtp_connection`` fixture example, let's read an optional server URL from the test module which uses our fixture:: # content of conftest.py @@ -433,10 +434,10 @@ read an optional server URL from the test module which uses our fixture:: import smtplib @pytest.fixture(scope="module") - def smtp(request): + def smtp_connection(request): server = getattr(request.module, "smtpserver", "smtp.gmail.com") smtp = smtplib.SMTP(server, 587, timeout=5) - yield smtp + yield smtp_connection print ("finalizing %s (%s)" % (smtp, server)) smtp.close() @@ -456,7 +457,7 @@ server URL in its module namespace:: smtpserver = "mail.python.org" # will be read by smtp fixture - def test_showhelo(smtp): + def test_showhelo(smtp_connection): assert 0, smtp.helo() Running it:: @@ -472,7 +473,7 @@ Running it:: ------------------------- Captured stdout teardown ------------------------- finalizing (mail.python.org) -voila! The ``smtp`` fixture function picked up our mail server name +voila! The ``smtp_connection`` fixture function picked up our mail server name from the module namespace. .. _`fixture-factory`: @@ -541,7 +542,7 @@ write exhaustive functional tests for components which themselves can be configured in multiple ways. Extending the previous example, we can flag the fixture to create two -``smtp`` fixture instances which will cause all tests using the fixture +``smtp_connection`` fixture instances which will cause all tests using the fixture to run twice. The fixture function gets access to each parameter through the special :py:class:`request ` object:: @@ -551,9 +552,9 @@ through the special :py:class:`request ` object:: @pytest.fixture(scope="module", params=["smtp.gmail.com", "mail.python.org"]) - def smtp(request): + def smtp_connection(request): smtp = smtplib.SMTP(request.param, 587, timeout=5) - yield smtp + yield smtp_connection print ("finalizing %s" % smtp) smtp.close() @@ -568,10 +569,10 @@ So let's just do another run:: ================================= FAILURES ================================= ________________________ test_ehlo[smtp.gmail.com] _________________________ - smtp = + smtp_connection = - def test_ehlo(smtp): - response, msg = smtp.ehlo() + def test_ehlo(smtp_connection): + response, msg = smtp_connection.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg > assert 0 # for demo purposes @@ -580,10 +581,10 @@ So let's just do another run:: test_module.py:6: AssertionError ________________________ test_noop[smtp.gmail.com] _________________________ - smtp = + smtp_connection = - def test_noop(smtp): - response, msg = smtp.noop() + def test_noop(smtp_connection): + response, msg = smtp_connection.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 @@ -591,10 +592,10 @@ So let's just do another run:: test_module.py:11: AssertionError ________________________ test_ehlo[mail.python.org] ________________________ - smtp = + ssmtp_connectionmtp = - def test_ehlo(smtp): - response, msg = smtp.ehlo() + def test_ehlo(smtp_connection): + response, msg = smtp_connection.ehlo() assert response == 250 > assert b"smtp.gmail.com" in msg E AssertionError: assert b'smtp.gmail.com' in b'mail.python.org\nPIPELINING\nSIZE 51200000\nETRN\nSTARTTLS\nAUTH DIGEST-MD5 NTLM CRAM-MD5\nENHANCEDSTATUSCODES\n8BITMIME\nDSN\nSMTPUTF8' @@ -604,10 +605,10 @@ So let's just do another run:: finalizing ________________________ test_noop[mail.python.org] ________________________ - smtp = + smtp_connection = - def test_noop(smtp): - response, msg = smtp.noop() + def test_noop(smtp_connection): + response, msg = smtp_connection.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 @@ -618,7 +619,7 @@ So let's just do another run:: 4 failed in 0.12 seconds We see that our two test functions each ran twice, against the different -``smtp`` instances. Note also, that with the ``mail.python.org`` +``smtp_connection`` instances. Note also, that with the ``mail.python.org`` connection the second test fails in ``test_ehlo`` because a different server string is expected than what arrived. @@ -730,25 +731,25 @@ can use other fixtures themselves. This contributes to a modular design of your fixtures and allows re-use of framework-specific fixtures across many projects. As a simple example, we can extend the previous example and instantiate an object ``app`` where we stick the already defined -``smtp`` resource into it:: +``smtp_connection`` resource into it:: # content of test_appsetup.py import pytest class App(object): - def __init__(self, smtp): - self.smtp = smtp + def __init__(self, smtp_connection): + self.smtp_connection = smtp_connection @pytest.fixture(scope="module") - def app(smtp): - return App(smtp) + def app(smtp_connection): + return App(smtp_connection) - def test_smtp_exists(app): - assert app.smtp + def test_smtp_connection_exists(app): + assert app.smtp_connection 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_connection`` fixture and instantiates an ``App`` object with it. Let's run it:: $ pytest -v test_appsetup.py =========================== test session starts ============================ @@ -762,14 +763,14 @@ Here we declare an ``app`` fixture which receives the previously defined ========================= 2 passed in 0.12 seconds ========================= -Due to the parametrization of ``smtp`` the test will run twice with two +Due to the parametrization of ``smtp_connection`` the test will run twice with two different ``App`` instances and respective smtp servers. There is no -need for the ``app`` fixture to be aware of the ``smtp`` parametrization -as pytest will fully analyse the fixture dependency graph. +need for the ``app`` fixture to be aware of the ``smtp_connection`` +parametrization as pytest will fully analyse the fixture dependency graph. Note, that the ``app`` fixture has a scope of ``module`` and uses a -module-scoped ``smtp`` fixture. The example would still work if ``smtp`` -was cached on a ``session`` scope: it is fine for fixtures to use +module-scoped ``smtp_connection`` fixture. The example would still work if +``smtp_connection`` was cached on a ``session`` scope: it is fine for fixtures to use "broader" scoped fixtures but not the other way round: A session-scoped fixture could not use a module-scoped one in a meaningful way. From f7c929c932c4a73934ee1ea3cb3fc2a5e2e3e852 Mon Sep 17 00:00:00 2001 From: Hugo Martins Date: Sat, 30 Jun 2018 14:34:19 +0100 Subject: [PATCH 2/7] Fix linting errors in docs/fixtures.rst --- changelog/3592.doc.rst | 2 +- doc/en/fixture.rst | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/changelog/3592.doc.rst b/changelog/3592.doc.rst index cbdf99666..38fc4a944 100644 --- a/changelog/3592.doc.rst +++ b/changelog/3592.doc.rst @@ -1 +1 @@ -Clarify confusing examples in fixtures' documentation. \ No newline at end of file +Clarify confusing examples in fixtures' documentation. diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index c48caf0af..e5c5d0b45 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -167,7 +167,7 @@ Fixtures requiring network access depend on connectivity and are usually time-expensive to create. Extending the previous example, we can add a ``scope="module"`` parameter to the :py:func:`@pytest.fixture <_pytest.python.fixture>` invocation -to cause the decorated ``smtp_connection`` fixture function to only be invoked +to cause the decorated ``smtp_connection`` fixture function to only be invoked once per test *module* (the default is to invoke once per test *function*). Multiple test functions in a test module will thus each receive the same ``smtp_connection`` fixture instance, thus saving time. @@ -184,9 +184,9 @@ access the fixture function:: def smtp_connection(): return smtplib.SMTP("smtp.gmail.com", 587, timeout=5) -The name of the fixture again is ``smtp_connection`` and you can access its -result by listing the name ``smtp_connection`` as an input parameter in any -test or fixture function (in or below the directory where ``conftest.py`` is +The name of the fixture again is ``smtp_connection`` and you can access its +result by listing the name ``smtp_connection`` as an input parameter in any +test or fixture function (in or below the directory where ``conftest.py`` is located):: # content of test_module.py @@ -240,9 +240,9 @@ inspect what is going on and can now run the tests:: ========================= 2 failed in 0.12 seconds ========================= You see the two ``assert 0`` failing and more importantly you can also see -that the same (module-scoped) ``smtp_connection`` object was passed into the +that the same (module-scoped) ``smtp_connection`` object was passed into the two test functions because pytest shows the incoming argument values in the -traceback. As a result, the two test functions using ``smtp_connection`` run +traceback. As a result, the two test functions using ``smtp_connection`` run as quick as a single one because they reuse the same instance. If you decide that you rather want to have a session-scoped ``smtp_connection`` @@ -364,7 +364,7 @@ Note that we can also seamlessly use the ``yield`` syntax with ``with`` statemen yield smtp_connection # provide the fixture value -The ``smtp_connection`` connection will be closed after the test finished +The ``smtp_connection`` connection will be closed after the test finished execution because the ``smtp_connection`` object automatically closes when the ``with`` statement ends. @@ -765,11 +765,11 @@ Here we declare an ``app`` fixture which receives the previously defined Due to the parametrization of ``smtp_connection`` the test will run twice with two different ``App`` instances and respective smtp servers. There is no -need for the ``app`` fixture to be aware of the ``smtp_connection`` +need for the ``app`` fixture to be aware of the ``smtp_connection`` parametrization as pytest will fully analyse the fixture dependency graph. Note, that the ``app`` fixture has a scope of ``module`` and uses a -module-scoped ``smtp_connection`` fixture. The example would still work if +module-scoped ``smtp_connection`` fixture. The example would still work if ``smtp_connection`` was cached on a ``session`` scope: it is fine for fixtures to use "broader" scoped fixtures but not the other way round: A session-scoped fixture could not use a module-scoped one in a From 8232fd1a2d1534b7a99479f12c37ad23a00e0716 Mon Sep 17 00:00:00 2001 From: Hugo Martins Date: Tue, 3 Jul 2018 23:24:59 +0100 Subject: [PATCH 3/7] Fix remaining "smtp" references --- doc/en/fixture.rst | 162 +++++++++++++++++++++------------------------ 1 file changed, 76 insertions(+), 86 deletions(-) diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index e5c5d0b45..070989c47 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -73,20 +73,20 @@ marked ``smtp_connection`` fixture function. Running the test looks like this:: platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - + test_smtpsimple.py F [100%] - + ================================= FAILURES ================================= ________________________________ test_ehlo _________________________________ - - smtp = - + + smtp_connection = + def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 > assert 0 # for demo purposes E assert 0 - + test_smtpsimple.py:11: AssertionError ========================= 1 failed in 0.12 seconds ========================= @@ -102,7 +102,7 @@ the exact protocol used by ``pytest`` to call the test function this way: 2. ``smtp_connection()`` is called to create an instance. -3. ``test_ehlo()`` is called and fails in the last +3. ``test_ehlo()`` is called and fails in the last line of the test function. Note that if you misspell a function argument or want @@ -210,32 +210,32 @@ inspect what is going on and can now run the tests:: platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - + test_module.py FF [100%] - + ================================= FAILURES ================================= ________________________________ test_ehlo _________________________________ - - smtp = - + + smtp_connection = + def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg > assert 0 # for demo purposes E assert 0 - + test_module.py:6: AssertionError ________________________________ test_noop _________________________________ - + smtp_connection = - + def test_noop(smtp_connection): response, msg = smtp_connection.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 - + test_module.py:11: AssertionError ========================= 2 failed in 0.12 seconds ========================= @@ -325,10 +325,10 @@ the code after the *yield* statement serves as the teardown code: @pytest.fixture(scope="module") def smtp_connection(): - smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) + smtp_connection = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) yield smtp_connection # provide the fixture value print("teardown smtp") - smtp.close() + smtp_connection.close() The ``print`` and ``smtp.close()`` statements will execute when the last test in the module has finished execution, regardless of the exception status of the @@ -338,7 +338,7 @@ Let's execute it:: $ pytest -s -q --tb=no FFteardown smtp_connection - + 2 failed in 0.12 seconds We see that the ``smtp_connection`` instance is finalized after the two @@ -360,7 +360,7 @@ Note that we can also seamlessly use the ``yield`` syntax with ``with`` statemen @pytest.fixture(scope="module") def smtp_connection(): - with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp: + with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp_connection: yield smtp_connection # provide the fixture value @@ -386,14 +386,14 @@ Here's the ``smtp_connection`` fixture changed to use ``addfinalizer`` for clean @pytest.fixture(scope="module") def smtp_connection(request): - smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) + smtp_connection = smtplib.SMTP("smtp.gmail.com", 587, timeout=5) def fin(): - print("teardown smtp") - smtp.close() + print("teardown smtp_connection") + smtp_connection.close() request.addfinalizer(fin) - return smtp # provide the fixture value + return smtp_connection # provide the fixture value Both ``yield`` and ``addfinalizer`` methods work similarly by calling their code after the test @@ -436,10 +436,10 @@ read an optional server URL from the test module which uses our fixture:: @pytest.fixture(scope="module") def smtp_connection(request): server = getattr(request.module, "smtpserver", "smtp.gmail.com") - smtp = smtplib.SMTP(server, 587, timeout=5) + smtp_connection = smtplib.SMTP(server, 587, timeout=5) yield smtp_connection - print ("finalizing %s (%s)" % (smtp, server)) - smtp.close() + print ("finalizing %s (%s)" % (smtp_connection, server)) + smtp_connection.close() We use the ``request.module`` attribute to optionally obtain an ``smtpserver`` attribute from the test module. If we just execute @@ -447,7 +447,7 @@ again, nothing much has changed:: $ pytest -s -q --tb=no FFfinalizing (smtp.gmail.com) - + 2 failed in 0.12 seconds Let's quickly create another test module that actually sets the @@ -458,7 +458,7 @@ server URL in its module namespace:: smtpserver = "mail.python.org" # will be read by smtp fixture def test_showhelo(smtp_connection): - assert 0, smtp.helo() + assert 0, smtp_connection.helo() Running it:: @@ -467,9 +467,8 @@ Running it:: ================================= FAILURES ================================= ______________________________ test_showhelo _______________________________ test_anothersmtp.py:5: in test_showhelo - assert 0, smtp.helo() - E AssertionError: (250, b'mail.python.org') - E assert 0 + assert 0, smtp_connection.helo() + E NameError: name 'smtp_connection' is not defined ------------------------- Captured stdout teardown ------------------------- finalizing (mail.python.org) @@ -553,10 +552,10 @@ through the special :py:class:`request ` object:: @pytest.fixture(scope="module", params=["smtp.gmail.com", "mail.python.org"]) def smtp_connection(request): - smtp = smtplib.SMTP(request.param, 587, timeout=5) + smtp_connection = smtplib.SMTP(request.param, 587, timeout=5) yield smtp_connection print ("finalizing %s" % smtp) - smtp.close() + smtp_connection.close() The main change is the declaration of ``params`` with :py:func:`@pytest.fixture <_pytest.python.fixture>`, a list of values @@ -568,52 +567,43 @@ So let's just do another run:: FFFF [100%] ================================= FAILURES ================================= ________________________ test_ehlo[smtp.gmail.com] _________________________ - - smtp_connection = - + + smtp_connection = + def test_ehlo(smtp_connection): - response, msg = smtp_connection.ehlo() - assert response == 250 - assert b"smtp.gmail.com" in msg - > assert 0 # for demo purposes - E assert 0 - - test_module.py:6: AssertionError + > response, msg = smtp_connection.ehlo() + E AttributeError: 'function' object has no attribute 'ehlo' + + test_module.py:3: AttributeError ________________________ test_noop[smtp.gmail.com] _________________________ - - smtp_connection = - + + smtp_connection = + def test_noop(smtp_connection): - response, msg = smtp_connection.noop() - assert response == 250 - > assert 0 # for demo purposes - E assert 0 - - test_module.py:11: AssertionError + > response, msg = smtp_connection.noop() + E AttributeError: 'function' object has no attribute 'noop' + + test_module.py:9: AttributeError ________________________ test_ehlo[mail.python.org] ________________________ - - ssmtp_connectionmtp = - + + smtp_connection = + def test_ehlo(smtp_connection): - response, msg = smtp_connection.ehlo() - assert response == 250 - > assert b"smtp.gmail.com" in msg - E AssertionError: assert b'smtp.gmail.com' in b'mail.python.org\nPIPELINING\nSIZE 51200000\nETRN\nSTARTTLS\nAUTH DIGEST-MD5 NTLM CRAM-MD5\nENHANCEDSTATUSCODES\n8BITMIME\nDSN\nSMTPUTF8' - - test_module.py:5: AssertionError + > response, msg = smtp_connection.ehlo() + E AttributeError: 'function' object has no attribute 'ehlo' + + test_module.py:3: AttributeError -------------------------- Captured stdout setup --------------------------- finalizing ________________________ test_noop[mail.python.org] ________________________ - - smtp_connection = - + + smtp_connection = + def test_noop(smtp_connection): - response, msg = smtp_connection.noop() - assert response == 250 - > assert 0 # for demo purposes - E assert 0 - - test_module.py:11: AssertionError + > response, msg = smtp_connection.noop() + E AttributeError: 'function' object has no attribute 'noop' + + test_module.py:9: AttributeError ------------------------- Captured stdout teardown ------------------------- finalizing 4 failed in 0.12 seconds @@ -684,7 +674,7 @@ Running the above tests results in the following test IDs being used:: - + ======================= no tests ran in 0.12 seconds ======================= .. _`fixture-parametrize-marks`: @@ -714,11 +704,11 @@ Running this test will *skip* the invocation of ``data_set`` with value ``2``:: cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 3 items - + test_fixture_marks.py::test_data[0] PASSED [ 33%] test_fixture_marks.py::test_data[1] PASSED [ 66%] test_fixture_marks.py::test_data[2] SKIPPED [100%] - + =================== 2 passed, 1 skipped in 0.12 seconds ==================== .. _`interdependent fixtures`: @@ -757,10 +747,10 @@ Here we declare an ``app`` fixture which receives the previously defined cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 2 items - - test_appsetup.py::test_smtp_exists[smtp.gmail.com] PASSED [ 50%] - test_appsetup.py::test_smtp_exists[mail.python.org] PASSED [100%] - + + test_appsetup.py::test_smtp_connection_exists[smtp.gmail.com] PASSED [ 50%] + test_appsetup.py::test_smtp_connection_exists[mail.python.org] PASSED [100%] + ========================= 2 passed in 0.12 seconds ========================= Due to the parametrization of ``smtp_connection`` the test will run twice with two @@ -826,26 +816,26 @@ Let's run the tests in verbose mode and with looking at the print-output:: cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 8 items - + test_module.py::test_0[1] SETUP otherarg 1 RUN test0 with otherarg 1 PASSED TEARDOWN otherarg 1 - + test_module.py::test_0[2] SETUP otherarg 2 RUN test0 with otherarg 2 PASSED TEARDOWN otherarg 2 - + test_module.py::test_1[mod1] SETUP modarg mod1 RUN test1 with modarg mod1 PASSED test_module.py::test_2[mod1-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod1 PASSED TEARDOWN otherarg 1 - + test_module.py::test_2[mod1-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod1 PASSED TEARDOWN otherarg 2 - + test_module.py::test_1[mod2] TEARDOWN modarg mod1 SETUP modarg mod2 RUN test1 with modarg mod2 @@ -853,13 +843,13 @@ Let's run the tests in verbose mode and with looking at the print-output:: test_module.py::test_2[mod2-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod2 PASSED TEARDOWN otherarg 1 - + test_module.py::test_2[mod2-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod2 PASSED TEARDOWN otherarg 2 TEARDOWN modarg mod2 - - + + ========================= 8 passed in 0.12 seconds ========================= You can see that the parametrized module-scoped ``modarg`` resource caused an From 63b25304c3727f37874d0923db4603a08ba7b1b2 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 3 Jul 2018 22:20:29 -0300 Subject: [PATCH 4/7] Run pre-commit fixers --- doc/en/fixture.rst | 80 +++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index 070989c47..18e431196 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -73,20 +73,20 @@ marked ``smtp_connection`` fixture function. Running the test looks like this:: platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 1 item - + test_smtpsimple.py F [100%] - + ================================= FAILURES ================================= ________________________________ test_ehlo _________________________________ - + smtp_connection = - + def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 > assert 0 # for demo purposes E assert 0 - + test_smtpsimple.py:11: AssertionError ========================= 1 failed in 0.12 seconds ========================= @@ -210,32 +210,32 @@ inspect what is going on and can now run the tests:: platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 2 items - + test_module.py FF [100%] - + ================================= FAILURES ================================= ________________________________ test_ehlo _________________________________ - + smtp_connection = - + def test_ehlo(smtp_connection): response, msg = smtp_connection.ehlo() assert response == 250 assert b"smtp.gmail.com" in msg > assert 0 # for demo purposes E assert 0 - + test_module.py:6: AssertionError ________________________________ test_noop _________________________________ - + smtp_connection = - + def test_noop(smtp_connection): response, msg = smtp_connection.noop() assert response == 250 > assert 0 # for demo purposes E assert 0 - + test_module.py:11: AssertionError ========================= 2 failed in 0.12 seconds ========================= @@ -338,7 +338,7 @@ Let's execute it:: $ pytest -s -q --tb=no FFteardown smtp_connection - + 2 failed in 0.12 seconds We see that the ``smtp_connection`` instance is finalized after the two @@ -447,7 +447,7 @@ again, nothing much has changed:: $ pytest -s -q --tb=no FFfinalizing (smtp.gmail.com) - + 2 failed in 0.12 seconds Let's quickly create another test module that actually sets the @@ -567,42 +567,42 @@ So let's just do another run:: FFFF [100%] ================================= FAILURES ================================= ________________________ test_ehlo[smtp.gmail.com] _________________________ - + smtp_connection = - + def test_ehlo(smtp_connection): > response, msg = smtp_connection.ehlo() E AttributeError: 'function' object has no attribute 'ehlo' - + test_module.py:3: AttributeError ________________________ test_noop[smtp.gmail.com] _________________________ - + smtp_connection = - + def test_noop(smtp_connection): > response, msg = smtp_connection.noop() E AttributeError: 'function' object has no attribute 'noop' - + test_module.py:9: AttributeError ________________________ test_ehlo[mail.python.org] ________________________ - + smtp_connection = - + def test_ehlo(smtp_connection): > response, msg = smtp_connection.ehlo() E AttributeError: 'function' object has no attribute 'ehlo' - + test_module.py:3: AttributeError -------------------------- Captured stdout setup --------------------------- finalizing ________________________ test_noop[mail.python.org] ________________________ - + smtp_connection = - + def test_noop(smtp_connection): > response, msg = smtp_connection.noop() E AttributeError: 'function' object has no attribute 'noop' - + test_module.py:9: AttributeError ------------------------- Captured stdout teardown ------------------------- finalizing @@ -674,7 +674,7 @@ Running the above tests results in the following test IDs being used:: - + ======================= no tests ran in 0.12 seconds ======================= .. _`fixture-parametrize-marks`: @@ -704,11 +704,11 @@ Running this test will *skip* the invocation of ``data_set`` with value ``2``:: cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 3 items - + test_fixture_marks.py::test_data[0] PASSED [ 33%] test_fixture_marks.py::test_data[1] PASSED [ 66%] test_fixture_marks.py::test_data[2] SKIPPED [100%] - + =================== 2 passed, 1 skipped in 0.12 seconds ==================== .. _`interdependent fixtures`: @@ -747,10 +747,10 @@ Here we declare an ``app`` fixture which receives the previously defined cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 2 items - + test_appsetup.py::test_smtp_connection_exists[smtp.gmail.com] PASSED [ 50%] test_appsetup.py::test_smtp_connection_exists[mail.python.org] PASSED [100%] - + ========================= 2 passed in 0.12 seconds ========================= Due to the parametrization of ``smtp_connection`` the test will run twice with two @@ -816,26 +816,26 @@ Let's run the tests in verbose mode and with looking at the print-output:: cachedir: .pytest_cache rootdir: $REGENDOC_TMPDIR, inifile: collecting ... collected 8 items - + test_module.py::test_0[1] SETUP otherarg 1 RUN test0 with otherarg 1 PASSED TEARDOWN otherarg 1 - + test_module.py::test_0[2] SETUP otherarg 2 RUN test0 with otherarg 2 PASSED TEARDOWN otherarg 2 - + test_module.py::test_1[mod1] SETUP modarg mod1 RUN test1 with modarg mod1 PASSED test_module.py::test_2[mod1-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod1 PASSED TEARDOWN otherarg 1 - + test_module.py::test_2[mod1-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod1 PASSED TEARDOWN otherarg 2 - + test_module.py::test_1[mod2] TEARDOWN modarg mod1 SETUP modarg mod2 RUN test1 with modarg mod2 @@ -843,13 +843,13 @@ Let's run the tests in verbose mode and with looking at the print-output:: test_module.py::test_2[mod2-1] SETUP otherarg 1 RUN test2 with otherarg 1 and modarg mod2 PASSED TEARDOWN otherarg 1 - + test_module.py::test_2[mod2-2] SETUP otherarg 2 RUN test2 with otherarg 2 and modarg mod2 PASSED TEARDOWN otherarg 2 TEARDOWN modarg mod2 - - + + ========================= 8 passed in 0.12 seconds ========================= You can see that the parametrized module-scoped ``modarg`` resource caused an From 4dfe2eee945db025aad942fef71b98b4f789c349 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 11 Jul 2018 20:24:39 -0300 Subject: [PATCH 5/7] Fix finalize call --- doc/en/fixture.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index 18e431196..a02ab8215 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -554,7 +554,7 @@ through the special :py:class:`request ` object:: def smtp_connection(request): smtp_connection = smtplib.SMTP(request.param, 587, timeout=5) yield smtp_connection - print ("finalizing %s" % smtp) + print("finalizing %s" % smtp_connection) smtp_connection.close() The main change is the declaration of ``params`` with From 2c4759ce5761b3fbc32f1d98a808b655ae964f2d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 11 Jul 2018 23:36:25 +0000 Subject: [PATCH 6/7] Run regendocs --- doc/en/builtin.rst | 2 +- doc/en/example/reportingdemo.rst | 100 ++++++++++++++----------------- doc/en/example/simple.rst | 2 +- doc/en/fixture.rst | 46 ++++++++------ 4 files changed, 74 insertions(+), 76 deletions(-) diff --git a/doc/en/builtin.rst b/doc/en/builtin.rst index c2d23469b..e52151a1b 100644 --- a/doc/en/builtin.rst +++ b/doc/en/builtin.rst @@ -90,7 +90,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a monkeypatch.setitem(mapping, name, value) monkeypatch.delitem(obj, name, raising=True) monkeypatch.setenv(name, value, prepend=False) - monkeypatch.delenv(name, value, raising=True) + monkeypatch.delenv(name, raising=True) monkeypatch.syspath_prepend(path) monkeypatch.chdir(path) diff --git a/doc/en/example/reportingdemo.rst b/doc/en/example/reportingdemo.rst index 4691b128b..a7cc81694 100644 --- a/doc/en/example/reportingdemo.rst +++ b/doc/en/example/reportingdemo.rst @@ -32,7 +32,6 @@ get on the terminal - we are working on that):: self = def test_simple(self): - def f(): return 42 @@ -44,7 +43,7 @@ get on the terminal - we are working on that):: E + where 42 = .f at 0xdeadbeef>() E + and 43 = .g at 0xdeadbeef>() - failure_demo.py:37: AssertionError + failure_demo.py:35: AssertionError ____________________ TestFailing.test_simple_multiline _____________________ self = @@ -52,7 +51,7 @@ get on the terminal - we are working on that):: def test_simple_multiline(self): > otherfunc_multi(42, 6 * 9) - failure_demo.py:40: + failure_demo.py:38: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ a = 42, b = 54 @@ -67,7 +66,6 @@ get on the terminal - we are working on that):: self = def test_not(self): - def f(): return 42 @@ -75,7 +73,7 @@ get on the terminal - we are working on that):: E assert not 42 E + where 42 = .f at 0xdeadbeef>() - failure_demo.py:47: AssertionError + failure_demo.py:44: AssertionError _________________ TestSpecialisedExplanations.test_eq_text _________________ self = @@ -86,7 +84,7 @@ get on the terminal - we are working on that):: E - spam E + eggs - failure_demo.py:53: AssertionError + failure_demo.py:49: AssertionError _____________ TestSpecialisedExplanations.test_eq_similar_text _____________ self = @@ -99,7 +97,7 @@ get on the terminal - we are working on that):: E + foo 2 bar E ? ^ - failure_demo.py:56: AssertionError + failure_demo.py:52: AssertionError ____________ TestSpecialisedExplanations.test_eq_multiline_text ____________ self = @@ -112,7 +110,7 @@ get on the terminal - we are working on that):: E + eggs E bar - failure_demo.py:59: AssertionError + failure_demo.py:55: AssertionError ______________ TestSpecialisedExplanations.test_eq_long_text _______________ self = @@ -129,7 +127,7 @@ get on the terminal - we are working on that):: E + 1111111111b222222222 E ? ^ - failure_demo.py:64: AssertionError + failure_demo.py:60: AssertionError _________ TestSpecialisedExplanations.test_eq_long_text_multiline __________ self = @@ -149,7 +147,7 @@ get on the terminal - we are working on that):: E E ...Full output truncated (7 lines hidden), use '-vv' to show - failure_demo.py:69: AssertionError + failure_demo.py:65: AssertionError _________________ TestSpecialisedExplanations.test_eq_list _________________ self = @@ -160,7 +158,7 @@ get on the terminal - we are working on that):: E At index 2 diff: 2 != 3 E Use -v to get the full diff - failure_demo.py:72: AssertionError + failure_demo.py:68: AssertionError ______________ TestSpecialisedExplanations.test_eq_list_long _______________ self = @@ -173,7 +171,7 @@ get on the terminal - we are working on that):: E At index 100 diff: 1 != 2 E Use -v to get the full diff - failure_demo.py:77: AssertionError + failure_demo.py:73: AssertionError _________________ TestSpecialisedExplanations.test_eq_dict _________________ self = @@ -191,7 +189,7 @@ get on the terminal - we are working on that):: E E ...Full output truncated (2 lines hidden), use '-vv' to show - failure_demo.py:80: AssertionError + failure_demo.py:76: AssertionError _________________ TestSpecialisedExplanations.test_eq_set __________________ self = @@ -209,7 +207,7 @@ get on the terminal - we are working on that):: E E ...Full output truncated (2 lines hidden), use '-vv' to show - failure_demo.py:83: AssertionError + failure_demo.py:79: AssertionError _____________ TestSpecialisedExplanations.test_eq_longer_list ______________ self = @@ -220,7 +218,7 @@ get on the terminal - we are working on that):: E Right contains more items, first extra item: 3 E Use -v to get the full diff - failure_demo.py:86: AssertionError + failure_demo.py:82: AssertionError _________________ TestSpecialisedExplanations.test_in_list _________________ self = @@ -229,7 +227,7 @@ get on the terminal - we are working on that):: > assert 1 in [0, 2, 3, 4, 5] E assert 1 in [0, 2, 3, 4, 5] - failure_demo.py:89: AssertionError + failure_demo.py:85: AssertionError __________ TestSpecialisedExplanations.test_not_in_text_multiline __________ self = @@ -248,7 +246,7 @@ get on the terminal - we are working on that):: E E ...Full output truncated (2 lines hidden), use '-vv' to show - failure_demo.py:93: AssertionError + failure_demo.py:89: AssertionError ___________ TestSpecialisedExplanations.test_not_in_text_single ____________ self = @@ -261,7 +259,7 @@ get on the terminal - we are working on that):: E single foo line E ? +++ - failure_demo.py:97: AssertionError + failure_demo.py:93: AssertionError _________ TestSpecialisedExplanations.test_not_in_text_single_long _________ self = @@ -274,7 +272,7 @@ get on the terminal - we are working on that):: E head head foo tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail E ? +++ - failure_demo.py:101: AssertionError + failure_demo.py:97: AssertionError ______ TestSpecialisedExplanations.test_not_in_text_single_long_term _______ self = @@ -287,11 +285,10 @@ get on the terminal - we are working on that):: E head head fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffftail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail tail E ? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - failure_demo.py:105: AssertionError + failure_demo.py:101: AssertionError ______________________________ test_attribute ______________________________ def test_attribute(): - class Foo(object): b = 1 @@ -300,11 +297,10 @@ get on the terminal - we are working on that):: E assert 1 == 2 E + where 1 = .Foo object at 0xdeadbeef>.b - failure_demo.py:114: AssertionError + failure_demo.py:109: AssertionError _________________________ test_attribute_instance __________________________ def test_attribute_instance(): - class Foo(object): b = 1 @@ -313,13 +309,11 @@ get on the terminal - we are working on that):: E + where 1 = .Foo object at 0xdeadbeef>.b E + where .Foo object at 0xdeadbeef> = .Foo'>() - failure_demo.py:122: AssertionError + failure_demo.py:116: AssertionError __________________________ test_attribute_failure __________________________ def test_attribute_failure(): - class Foo(object): - def _get_b(self): raise Exception("Failed to get attrib") @@ -328,7 +322,7 @@ get on the terminal - we are working on that):: i = Foo() > assert i.b == 2 - failure_demo.py:135: + failure_demo.py:127: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = .Foo object at 0xdeadbeef> @@ -337,11 +331,10 @@ get on the terminal - we are working on that):: > raise Exception("Failed to get attrib") E Exception: Failed to get attrib - failure_demo.py:130: Exception + failure_demo.py:122: Exception _________________________ test_attribute_multiple __________________________ def test_attribute_multiple(): - class Foo(object): b = 1 @@ -355,7 +348,7 @@ get on the terminal - we are working on that):: E + and 2 = .Bar object at 0xdeadbeef>.b E + where .Bar object at 0xdeadbeef> = .Bar'>() - failure_demo.py:146: AssertionError + failure_demo.py:137: AssertionError __________________________ TestRaises.test_raises __________________________ self = @@ -364,13 +357,13 @@ get on the terminal - we are working on that):: s = "qwe" # NOQA > raises(TypeError, "int(s)") - failure_demo.py:157: + failure_demo.py:147: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ > int(s) E ValueError: invalid literal for int() with base 10: 'qwe' - <0-codegen $PYTHON_PREFIX/lib/python3.5/site-packages/_pytest/python_api.py:634>:1: ValueError + <0-codegen $PYTHON_PREFIX/lib/python3.5/site-packages/_pytest/python_api.py:635>:1: ValueError ______________________ TestRaises.test_raises_doesnt _______________________ self = @@ -379,7 +372,7 @@ get on the terminal - we are working on that):: > raises(IOError, "int('3')") E Failed: DID NOT RAISE - failure_demo.py:160: Failed + failure_demo.py:150: Failed __________________________ TestRaises.test_raise ___________________________ self = @@ -388,7 +381,7 @@ get on the terminal - we are working on that):: > raise ValueError("demo error") E ValueError: demo error - failure_demo.py:163: ValueError + failure_demo.py:153: ValueError ________________________ TestRaises.test_tupleerror ________________________ self = @@ -397,7 +390,7 @@ get on the terminal - we are working on that):: > a, b = [1] # NOQA E ValueError: not enough values to unpack (expected 2, got 1) - failure_demo.py:166: ValueError + failure_demo.py:156: ValueError ______ TestRaises.test_reinterpret_fails_with_print_for_the_fun_of_it ______ self = @@ -408,7 +401,7 @@ get on the terminal - we are working on that):: > a, b = items.pop() E TypeError: 'int' object is not iterable - failure_demo.py:171: TypeError + failure_demo.py:161: TypeError --------------------------- Captured stdout call --------------------------- items is [1, 2, 3] ________________________ TestRaises.test_some_error ________________________ @@ -419,7 +412,7 @@ get on the terminal - we are working on that):: > if namenotexi: # NOQA E NameError: name 'namenotexi' is not defined - failure_demo.py:174: NameError + failure_demo.py:164: NameError ____________________ test_dynamic_compile_shows_nicely _____________________ def test_dynamic_compile_shows_nicely(): @@ -434,20 +427,19 @@ get on the terminal - we are working on that):: sys.modules[name] = module > module.foo() - failure_demo.py:192: + failure_demo.py:182: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def foo(): > assert 1 == 0 E AssertionError - <2-codegen 'abc-123' $REGENDOC_TMPDIR/assertion/failure_demo.py:189>:2: AssertionError + <2-codegen 'abc-123' $REGENDOC_TMPDIR/assertion/failure_demo.py:179>:2: AssertionError ____________________ TestMoreErrors.test_complex_error _____________________ self = def test_complex_error(self): - def f(): return 44 @@ -456,7 +448,7 @@ get on the terminal - we are working on that):: > somefunc(f(), g()) - failure_demo.py:205: + failure_demo.py:193: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ failure_demo.py:11: in somefunc otherfunc(x, y) @@ -478,7 +470,7 @@ get on the terminal - we are working on that):: > a, b = items E ValueError: not enough values to unpack (expected 2, got 0) - failure_demo.py:209: ValueError + failure_demo.py:197: ValueError ____________________ TestMoreErrors.test_z2_type_error _____________________ self = @@ -488,7 +480,7 @@ get on the terminal - we are working on that):: > a, b = items E TypeError: 'int' object is not iterable - failure_demo.py:213: TypeError + failure_demo.py:201: TypeError ______________________ TestMoreErrors.test_startswith ______________________ self = @@ -501,13 +493,12 @@ get on the terminal - we are working on that):: E + where False = ('456') E + where = '123'.startswith - failure_demo.py:218: AssertionError + failure_demo.py:206: AssertionError __________________ TestMoreErrors.test_startswith_nested ___________________ self = def test_startswith_nested(self): - def f(): return "123" @@ -521,7 +512,7 @@ get on the terminal - we are working on that):: E + where '123' = .f at 0xdeadbeef>() E + and '456' = .g at 0xdeadbeef>() - failure_demo.py:228: AssertionError + failure_demo.py:215: AssertionError _____________________ TestMoreErrors.test_global_func ______________________ self = @@ -532,7 +523,7 @@ get on the terminal - we are working on that):: E + where False = isinstance(43, float) E + where 43 = globf(42) - failure_demo.py:231: AssertionError + failure_demo.py:218: AssertionError _______________________ TestMoreErrors.test_instance _______________________ self = @@ -543,7 +534,7 @@ get on the terminal - we are working on that):: E assert 42 != 42 E + where 42 = .x - failure_demo.py:235: AssertionError + failure_demo.py:222: AssertionError _______________________ TestMoreErrors.test_compare ________________________ self = @@ -553,7 +544,7 @@ get on the terminal - we are working on that):: E assert 11 < 5 E + where 11 = globf(10) - failure_demo.py:238: AssertionError + failure_demo.py:225: AssertionError _____________________ TestMoreErrors.test_try_finally ______________________ self = @@ -564,13 +555,12 @@ get on the terminal - we are working on that):: > assert x == 0 E assert 1 == 0 - failure_demo.py:243: AssertionError + failure_demo.py:230: AssertionError ___________________ TestCustomAssertMsg.test_single_line ___________________ self = def test_single_line(self): - class A(object): a = 1 @@ -580,13 +570,12 @@ get on the terminal - we are working on that):: E assert 1 == 2 E + where 1 = .A'>.a - failure_demo.py:256: AssertionError + failure_demo.py:241: AssertionError ____________________ TestCustomAssertMsg.test_multiline ____________________ self = def test_multiline(self): - class A(object): a = 1 @@ -600,13 +589,12 @@ get on the terminal - we are working on that):: E assert 1 == 2 E + where 1 = .A'>.a - failure_demo.py:264: AssertionError + failure_demo.py:248: AssertionError ___________________ TestCustomAssertMsg.test_custom_repr ___________________ self = def test_custom_repr(self): - class JSON(object): a = 1 @@ -623,7 +611,7 @@ get on the terminal - we are working on that):: E assert 1 == 2 E + where 1 = This is JSON\n{\n 'foo': 'bar'\n}.a - failure_demo.py:278: AssertionError + failure_demo.py:261: AssertionError ============================= warnings summary ============================= Metafunc.addcall is deprecated and scheduled to be removed in pytest 4.0. diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index 180637ae9..f8c1fd279 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -415,7 +415,7 @@ Now we can profile which test functions execute the slowest:: ========================= slowest 3 test durations ========================= 0.30s call test_some_are_slow.py::test_funcslow2 - 0.20s call test_some_are_slow.py::test_funcslow1 + 0.21s call test_some_are_slow.py::test_funcslow1 0.10s call test_some_are_slow.py::test_funcfast ========================= 3 passed in 0.12 seconds ========================= diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index a02ab8215..1fd16fa34 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -337,7 +337,7 @@ tests. Let's execute it:: $ pytest -s -q --tb=no - FFteardown smtp_connection + FFteardown smtp 2 failed in 0.12 seconds @@ -468,7 +468,8 @@ Running it:: ______________________________ test_showhelo _______________________________ test_anothersmtp.py:5: in test_showhelo assert 0, smtp_connection.helo() - E NameError: name 'smtp_connection' is not defined + E AssertionError: (250, b'mail.python.org') + E assert 0 ------------------------- Captured stdout teardown ------------------------- finalizing (mail.python.org) @@ -568,42 +569,51 @@ So let's just do another run:: ================================= FAILURES ================================= ________________________ test_ehlo[smtp.gmail.com] _________________________ - smtp_connection = + smtp_connection = def test_ehlo(smtp_connection): - > response, msg = smtp_connection.ehlo() - E AttributeError: 'function' object has no attribute 'ehlo' + response, msg = smtp_connection.ehlo() + assert response == 250 + assert b"smtp.gmail.com" in msg + > assert 0 # for demo purposes + E assert 0 - test_module.py:3: AttributeError + test_module.py:6: AssertionError ________________________ test_noop[smtp.gmail.com] _________________________ - smtp_connection = + smtp_connection = def test_noop(smtp_connection): - > response, msg = smtp_connection.noop() - E AttributeError: 'function' object has no attribute 'noop' + response, msg = smtp_connection.noop() + assert response == 250 + > assert 0 # for demo purposes + E assert 0 - test_module.py:9: AttributeError + test_module.py:11: AssertionError ________________________ test_ehlo[mail.python.org] ________________________ - smtp_connection = + smtp_connection = def test_ehlo(smtp_connection): - > response, msg = smtp_connection.ehlo() - E AttributeError: 'function' object has no attribute 'ehlo' + response, msg = smtp_connection.ehlo() + assert response == 250 + > assert b"smtp.gmail.com" in msg + E AssertionError: assert b'smtp.gmail.com' in b'mail.python.org\nPIPELINING\nSIZE 51200000\nETRN\nSTARTTLS\nAUTH DIGEST-MD5 NTLM CRAM-MD5\nENHANCEDSTATUSCODES\n8BITMIME\nDSN\nSMTPUTF8' - test_module.py:3: AttributeError + test_module.py:5: AssertionError -------------------------- Captured stdout setup --------------------------- finalizing ________________________ test_noop[mail.python.org] ________________________ - smtp_connection = + smtp_connection = def test_noop(smtp_connection): - > response, msg = smtp_connection.noop() - E AttributeError: 'function' object has no attribute 'noop' + response, msg = smtp_connection.noop() + assert response == 250 + > assert 0 # for demo purposes + E assert 0 - test_module.py:9: AttributeError + test_module.py:11: AssertionError ------------------------- Captured stdout teardown ------------------------- finalizing 4 failed in 0.12 seconds From aa47b64e2a337df4520eea5a680b14990b724ee1 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 11 Jul 2018 21:07:21 -0300 Subject: [PATCH 7/7] Improve CHANGELOG entry --- changelog/3592.doc.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/3592.doc.rst b/changelog/3592.doc.rst index 38fc4a944..1d4d35352 100644 --- a/changelog/3592.doc.rst +++ b/changelog/3592.doc.rst @@ -1 +1 @@ -Clarify confusing examples in fixtures' documentation. +Use ``smtp_connection`` instead of ``smtp`` in fixtures documentation to avoid possible confusion.