Fix remaining "smtp" references

This commit is contained in:
Hugo Martins 2018-07-03 23:24:59 +01:00
parent f52a5d3be2
commit 8232fd1a2d
1 changed files with 76 additions and 86 deletions

View File

@ -79,7 +79,7 @@ marked ``smtp_connection`` fixture function. Running the test looks like this::
================================= FAILURES ================================= ================================= FAILURES =================================
________________________________ test_ehlo _________________________________ ________________________________ test_ehlo _________________________________
smtp = <smtplib.SMTP object at 0xdeadbeef> smtp_connection = <smtplib.SMTP object at 0xdeadbeef>
def test_ehlo(smtp_connection): def test_ehlo(smtp_connection):
response, msg = smtp_connection.ehlo() response, msg = smtp_connection.ehlo()
@ -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. 2. ``smtp_connection()`` is called to create an instance.
3. ``test_ehlo(<SMTP instance>)`` is called and fails in the last 3. ``test_ehlo(<smtp_connection instance>)`` is called and fails in the last
line of the test function. line of the test function.
Note that if you misspell a function argument or want Note that if you misspell a function argument or want
@ -216,7 +216,7 @@ inspect what is going on and can now run the tests::
================================= FAILURES ================================= ================================= FAILURES =================================
________________________________ test_ehlo _________________________________ ________________________________ test_ehlo _________________________________
smtp = <smtplib.SMTP object at 0xdeadbeef> smtp_connection = <smtplib.SMTP object at 0xdeadbeef>
def test_ehlo(smtp_connection): def test_ehlo(smtp_connection):
response, msg = smtp_connection.ehlo() response, msg = smtp_connection.ehlo()
@ -325,10 +325,10 @@ the code after the *yield* statement serves as the teardown code:
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def smtp_connection(): 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 yield smtp_connection # provide the fixture value
print("teardown smtp") print("teardown smtp")
smtp.close() smtp_connection.close()
The ``print`` and ``smtp.close()`` statements will execute when the last test in 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 the module has finished execution, regardless of the exception status of the
@ -360,7 +360,7 @@ Note that we can also seamlessly use the ``yield`` syntax with ``with`` statemen
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def smtp_connection(): 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 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") @pytest.fixture(scope="module")
def smtp_connection(request): 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(): def fin():
print("teardown smtp") print("teardown smtp_connection")
smtp.close() smtp_connection.close()
request.addfinalizer(fin) 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 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") @pytest.fixture(scope="module")
def smtp_connection(request): def smtp_connection(request):
server = getattr(request.module, "smtpserver", "smtp.gmail.com") 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 yield smtp_connection
print ("finalizing %s (%s)" % (smtp, server)) print ("finalizing %s (%s)" % (smtp_connection, server))
smtp.close() smtp_connection.close()
We use the ``request.module`` attribute to optionally obtain an We use the ``request.module`` attribute to optionally obtain an
``smtpserver`` attribute from the test module. If we just execute ``smtpserver`` attribute from the test module. If we just execute
@ -458,7 +458,7 @@ server URL in its module namespace::
smtpserver = "mail.python.org" # will be read by smtp fixture smtpserver = "mail.python.org" # will be read by smtp fixture
def test_showhelo(smtp_connection): def test_showhelo(smtp_connection):
assert 0, smtp.helo() assert 0, smtp_connection.helo()
Running it:: Running it::
@ -467,9 +467,8 @@ Running it::
================================= 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_connection.helo()
E AssertionError: (250, b'mail.python.org') E NameError: name 'smtp_connection' is not defined
E assert 0
------------------------- Captured stdout teardown ------------------------- ------------------------- Captured stdout teardown -------------------------
finalizing <smtplib.SMTP object at 0xdeadbeef> (mail.python.org) finalizing <smtplib.SMTP object at 0xdeadbeef> (mail.python.org)
@ -553,10 +552,10 @@ through the special :py:class:`request <FixtureRequest>` object::
@pytest.fixture(scope="module", @pytest.fixture(scope="module",
params=["smtp.gmail.com", "mail.python.org"]) params=["smtp.gmail.com", "mail.python.org"])
def smtp_connection(request): def smtp_connection(request):
smtp = smtplib.SMTP(request.param, 587, timeout=5) smtp_connection = smtplib.SMTP(request.param, 587, timeout=5)
yield smtp_connection yield smtp_connection
print ("finalizing %s" % smtp) print ("finalizing %s" % smtp)
smtp.close() smtp_connection.close()
The main change is the declaration of ``params`` with The main change is the declaration of ``params`` with
:py:func:`@pytest.fixture <_pytest.python.fixture>`, a list of values :py:func:`@pytest.fixture <_pytest.python.fixture>`, a list of values
@ -569,51 +568,42 @@ So let's just do another run::
================================= FAILURES ================================= ================================= FAILURES =================================
________________________ test_ehlo[smtp.gmail.com] _________________________ ________________________ test_ehlo[smtp.gmail.com] _________________________
smtp_connection = <smtplib.SMTP object at 0xdeadbeef> smtp_connection = <function smtp_connection at 0xdeadbeef>
def test_ehlo(smtp_connection): def test_ehlo(smtp_connection):
response, msg = smtp_connection.ehlo() > response, msg = smtp_connection.ehlo()
assert response == 250 E AttributeError: 'function' object has no attribute 'ehlo'
assert b"smtp.gmail.com" in msg
> assert 0 # for demo purposes
E assert 0
test_module.py:6: AssertionError test_module.py:3: AttributeError
________________________ test_noop[smtp.gmail.com] _________________________ ________________________ test_noop[smtp.gmail.com] _________________________
smtp_connection = <smtplib.SMTP object at 0xdeadbeef> smtp_connection = <function smtp_connection at 0xdeadbeef>
def test_noop(smtp_connection): def test_noop(smtp_connection):
response, msg = smtp_connection.noop() > response, msg = smtp_connection.noop()
assert response == 250 E AttributeError: 'function' object has no attribute 'noop'
> assert 0 # for demo purposes
E assert 0
test_module.py:11: AssertionError test_module.py:9: AttributeError
________________________ test_ehlo[mail.python.org] ________________________ ________________________ test_ehlo[mail.python.org] ________________________
ssmtp_connectionmtp = <smtplib.SMTP object at 0xdeadbeef> smtp_connection = <function smtp_connection at 0xdeadbeef>
def test_ehlo(smtp_connection): def test_ehlo(smtp_connection):
response, msg = smtp_connection.ehlo() > response, msg = smtp_connection.ehlo()
assert response == 250 E AttributeError: 'function' object has no attribute 'ehlo'
> 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 test_module.py:3: AttributeError
-------------------------- Captured stdout setup --------------------------- -------------------------- Captured stdout setup ---------------------------
finalizing <smtplib.SMTP object at 0xdeadbeef> finalizing <smtplib.SMTP object at 0xdeadbeef>
________________________ test_noop[mail.python.org] ________________________ ________________________ test_noop[mail.python.org] ________________________
smtp_connection = <smtplib.SMTP object at 0xdeadbeef> smtp_connection = <function smtp_connection at 0xdeadbeef>
def test_noop(smtp_connection): def test_noop(smtp_connection):
response, msg = smtp_connection.noop() > response, msg = smtp_connection.noop()
assert response == 250 E AttributeError: 'function' object has no attribute 'noop'
> assert 0 # for demo purposes
E assert 0
test_module.py:11: AssertionError test_module.py:9: AttributeError
------------------------- Captured stdout teardown ------------------------- ------------------------- Captured stdout teardown -------------------------
finalizing <smtplib.SMTP object at 0xdeadbeef> finalizing <smtplib.SMTP object at 0xdeadbeef>
4 failed in 0.12 seconds 4 failed in 0.12 seconds
@ -758,8 +748,8 @@ Here we declare an ``app`` fixture which receives the previously defined
rootdir: $REGENDOC_TMPDIR, inifile: rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 2 items collecting ... collected 2 items
test_appsetup.py::test_smtp_exists[smtp.gmail.com] PASSED [ 50%] test_appsetup.py::test_smtp_connection_exists[smtp.gmail.com] PASSED [ 50%]
test_appsetup.py::test_smtp_exists[mail.python.org] PASSED [100%] test_appsetup.py::test_smtp_connection_exists[mail.python.org] PASSED [100%]
========================= 2 passed in 0.12 seconds ========================= ========================= 2 passed in 0.12 seconds =========================