如何在烧瓶应用程序中测试我的依赖注入?
我一直在(单元)测试依赖性注射的(单元)测试上,他们正在使用pytest固定装置,并且我正在尝试在烧瓶应用中复制类似的内容。这就是我的应用的样子:
# all imports
class Container(containers.DeclarativeContainer):
wiring_config = containers.WiringConfiguration(modules=[".routes", ".scheduler"])
config = providers.Configuration(yaml_files=["src/conf/config.yaml"])
config.load(envs_required=True)
s3_repository = providers.Resource(
S3Repository, config.get("app.my_service.s3_bucket")
)
my_service = providers.Singleton(
MyService, config, s3_repository
)
我的app.py
:
container = Container()
container.init_resources()
app = Flask(__name__)
app.container = container
# connect url rules and register error handlers
routes.configure(app)
# schedule and kickoff background jobs
scheduler.schedule(app)
# set flask configuration and logging
app.config.from_mapping(app.container.config.get("app"))
setup_logging(app)
return app
my_service.py
class MyService:
def __init__(self, config: dict, s3_repository: S3Repository) -> None:
self.s3 = s3_repository
self.config = config
# other logic/methods
我的s3repository
:
class S3Repository:
def __init__(self, bucket):
self.bucket = bucket
def fetch(self, object_key, columns, filters):
# code to fetch
我正在尝试编写测试并工作第一次使用pytest
,这就是我到目前为止的:
# TODO - re-write tests for since we're now using dependency injection
import unittest
from unittest.mock import Mock
import pytest as pytest
from src.repository.s3_repository import S3Repository
from src.service.HealthSignalService import HealthSignalService
class TestApp(unittest.TestCase):
def something(self):
pass
@pytest.fixture
def mock_config(mocker):
return mocker.patch("providers.Configuration")
def test_app(mock_config):
from src import create_app
create_app()
当我运行时,我会看到:
@pytest.fixture
def mock_config(mocker):
E fixture 'mocker' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, mock_config, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
我在做什么错?我想念什么?现在,我正在关注本教程 - https:https:// docs。 pytest.org/en/7.1.x/explanation/fixtures.html
I've been following those tutorials on (unit) testing for dependency injections and they're using pytest fixtures and I'm trying to replicate something similar in my Flask app. This is what my app looks like:
# all imports
class Container(containers.DeclarativeContainer):
wiring_config = containers.WiringConfiguration(modules=[".routes", ".scheduler"])
config = providers.Configuration(yaml_files=["src/conf/config.yaml"])
config.load(envs_required=True)
s3_repository = providers.Resource(
S3Repository, config.get("app.my_service.s3_bucket")
)
my_service = providers.Singleton(
MyService, config, s3_repository
)
My app.py
:
container = Container()
container.init_resources()
app = Flask(__name__)
app.container = container
# connect url rules and register error handlers
routes.configure(app)
# schedule and kickoff background jobs
scheduler.schedule(app)
# set flask configuration and logging
app.config.from_mapping(app.container.config.get("app"))
setup_logging(app)
return app
my_service.py
class MyService:
def __init__(self, config: dict, s3_repository: S3Repository) -> None:
self.s3 = s3_repository
self.config = config
# other logic/methods
My S3Repository
:
class S3Repository:
def __init__(self, bucket):
self.bucket = bucket
def fetch(self, object_key, columns, filters):
# code to fetch
I'm trying to write my tests and working with pytest
for the first time and this is what I have so far:
# TODO - re-write tests for since we're now using dependency injection
import unittest
from unittest.mock import Mock
import pytest as pytest
from src.repository.s3_repository import S3Repository
from src.service.HealthSignalService import HealthSignalService
class TestApp(unittest.TestCase):
def something(self):
pass
@pytest.fixture
def mock_config(mocker):
return mocker.patch("providers.Configuration")
def test_app(mock_config):
from src import create_app
create_app()
When I run this I see:
@pytest.fixture
def mock_config(mocker):
E fixture 'mocker' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, mock_config, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
What am I doing wrong? What am I missing? Right now, I'm following this tutorial - https://docs.pytest.org/en/7.1.x/explanation/fixtures.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不会用
模拟器
的名称定义pytest.fixture
。传递给pytest函数参数的参数必须由pytest.fixture
例如,例如
pytest.fixture
已经定义的那些参数You never define a
pytest.fixture
with the name ofmocker
. The arguments passing to the pytest function arguments must be defined bypytest.fixture
for example
those arguments already defined by the
pytest.fixture