在这个最小的示例中,pytest我们将使用测试我们的Hello World应用程序确实返回“ Hello,World!”。URL上有GET请求时,HTTP OK状态码为200/
首先,让我们安装pytest到我们的virtualenv中
pip install pytest
仅供参考,这是我们的世界应用:
# hello.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'在我们的旁边hello.py,我们定义了一个名为的测试模块test_hello.py,该模块将由py.test
# test_hello.py
from hello import app
def test_hello():
response = app.test_client().get('/')
assertresponse.status_code== 200
assertresponse.data== b'Hello, World!'只是为了回顾一下,此时我们通过tree命令获得的项目结构为:
. ├── hello.py └── test_hello.py
现在,我们可以使用py.test将自动发现我们test_hello.py和其中的测试功能的命令运行此测试
$ py.test
您应该看到一些输出和1测试已通过的指示,例如
=== test session starts === collected 1 items test_hello.py . === 1 passed in 0.13 seconds ===