我在上的文档中发现了一些令人困惑的内容https://docs.pytest.org/en/latest/fixture



我正在学习pytest,这给了我一个优势:我完全一无所知,所以我可以告诉你我觉得困惑的地方以及我是如何克服的。

在https://docs.pytest.org/en/latest/fixture.html#,有两个示例python文件:test_module.py和testrongmtpsimple.py。我发现这些示例令人困惑的是,有两种方法,smtp_connection.ehlo((和smtp_conconnection.noop((。直观上看不出这些是smtplib类对象的方法。SMTP。当然,他们是。我不得不通过一些软件来弄清楚这一点。

Inhttps://docs.pytest.org/en/latest/fixture.html#,有两个示例python文件:test_module.py和testrongmtpsimple.py。我发现这些示例令人困惑的是,有两种方法,smtp_connection.ehlo((和smtp_conconnection.noop((。直观上看不出这些是smtplib类对象的方法。SMTP。当然,他们是。我不得不通过一些软件来弄清楚这一点。

因此,我修改了代码并添加了一些注释,希望对学习pytest的人来说更容易。

def test_noop(smtp_connection):
# See https://docs.python.org/3/library/smtplib.html#smtp-objects
# The documentation there says that the noop method is not documented there.
response, msg = smtp_connection.noop()
assert response == 250
assert 0  # for demo purposes

def smtp_connection():
import smtplib
# This isn't clear from https://docs.pytest.org/en/latest/fixture.html but smtplib has
# at least two attributes: ehlo and noop
print ( dir(smtplib.SMTP), file=sys.stderr )
# Return an object of class smtplib.SMTP which has methods ehlo and noop
return smtplib.SMTP("smtp.gmail.com", 587, timeout=5)

def test_ehlo(smtp_connection):
# smtp_connection is an object of type smtplib.SMTP.
# See https://docs.python.org/3/library/smtplib.html#smtp-objects
# The documentation there says that the noop method is not documented there.
response, msg = smtp_connection.ehlo()
assert response == 250
assert 0 # for demo purposes

为了更清楚,我可能会在smtp_connection通过代码工作时打印它的对象id。这可能有些过头了。

相关内容

最新更新