ABM*_*ABM 4 python unit-testing pytest flask python-3.x
我正在使用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.
Run Code Online (Sandbox Code Playgroud)
一个文件中的所有测试和装置看起来像这样:
# 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
...
Run Code Online (Sandbox Code Playgroud)
我跑了$ 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
Run Code Online (Sandbox Code Playgroud)
# tests/test_1.py
from tests.fixtures import app, client
def test_1(client):
# test body
Run Code Online (Sandbox Code Playgroud)
# tests/test_2.py
from tests.fixtures import app, client
def test_2(client):
# test body
Run Code Online (Sandbox Code Playgroud)
如果我们运行,$ pytest tests那么tests/test_1.py将通过,而tests/test_2.py将引发错误.
我已经看过这个要点,并尝试用测试功能标记@pytest.mark.usefixture,但没有成功.
如何在包含多个测试文件的目录上运行带有模块化夹具的Py.Test?
您稍微不正确地使用灯具.
具体来说,您声明了多个具有相同名称且具有相同功能对象的灯具,但在不同模块中检测到.从pytest的角度来看,这些应该是单独的函数,因为pytest会dir(module)检测文件中的测试和fixture.但不知何故,由于同一个函数对象,我认为,pytest会记住它们是一样的; 所以一旦你到达第二个测试文件,它会尝试本地检测到的夹具名称,但发现它已经准备好并失败了.
正确的用法是创建一个伪插件conftest.py,并将所有的灯具放在那里.声明后,这些灯具将可用于该目录和所有子目录中的所有文件.
注意:不得将此类灯具导入测试文件.灯具不是功能.Pytest自动准备它们并将它们提供给测试.
# tests/conftest.py
import pytest
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
Run Code Online (Sandbox Code Playgroud)
和测试文件(注意没有导入!):
# tests/test_1.py
def test_1(client):
# test body
# tests/test_2.py
def test_2(client):
# test body
Run Code Online (Sandbox Code Playgroud)
查看更多:https://docs.pytest.org/en/latest/writing_plugins.html
| 归档时间: |
|
| 查看次数: |
1619 次 |
| 最近记录: |