【发布时间】:2017-10-17 14:19:54
【问题描述】:
我正在使用 Py.Test 在 Python Flask 应用程序中测试函数。
当我使用一个包含所有固定装置和测试的“app_test.py”文件时,我的测试通过了。现在我已经将灯具拆分成它们自己的模块,并将测试分成不同的模块,每个模块都会导入我遇到的问题的灯具模块。
如果我对每个模块单独运行测试,一切都会顺利进行:
pytest tests/test_1.py、pytest tests/test_2.py、pytest tests/test_3.py 等。但是,如果我想使用一个命令按顺序运行所有测试,例如pytest tests.
我的第一个测试模块通过,所有未来的测试都报告错误:
AssertionError: A setup function was called after the first request was handled.
This usually indicates a bug in the application where a module was not imported
and decorators or other functionality was called too late.
E To fix this make sure to import all your view modules, database models
and everything related at a central place before the application starts
serving requests.
一个文件中的所有测试和固定装置看起来像这样:
# app_test.py
from flask_app import create_app
@pytest.fixtures(scope="session")
def app(request):
app = create_app()
with app.app_context():
yield app
@pytest.fixture(scope="session"):
def client(request, app):
client = app.test_client()
return client
def test1(client):
# test body
def test2(client):
# test body
...
我运行$ pytest app_test.py,一切运行良好。
现在假设我们将它们分成三个不同的模块:fixures.py、test_1.py 和 test_2.py。现在的代码如下所示。
# tests/fixtures.py
from flask_app import create_app
@pytest.fixtures(scope="session")
def app(request):
app = create_app()
with app.app_context():
yield app
@pytest.fixture(scope="session"):
def client(request, app):
client = app.test_client()
return client
# tests/test_1.py
from tests.fixtures import app, client
def test_1(client):
# test body
# tests/test_2.py
from tests.fixtures import app, client
def test_2(client):
# test body
如果我们运行$ pytest tests,那么tests/test_1.py 将通过,而tests/test_2.py 将引发错误。
我查看了这个gist,并尝试使用@pytest.mark.usefixture 标记测试功能,但没有成功。
如何在包含多个测试文件的目录上运行带有模块化夹具的 Py.Test?
【问题讨论】:
标签: python python-3.x unit-testing flask pytest