如何在烧瓶应用程序中测试我的依赖注入?

发布于 2025-01-24 04:03:29 字数 2794 浏览 3 评论 0原文

我一直在(单元)测试依赖性注射的(单元)测试上,他们正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除会话 2025-01-31 04:03:29

您永远不会用模拟器的名称定义pytest.fixture。传递给pytest函数参数的参数必须由pytest.fixture

例如,例如

@pytest.fixture
def connection():
    ...

@pytest.fixture
def database(connection):
    ...

def test_somecase(connection, database):
    ...

pytest.fixture已经定义的那些参数

You never define a pytest.fixture with the name of mocker. The arguments passing to the pytest function arguments must be defined by pytest.fixture

for example

@pytest.fixture
def connection():
    ...

@pytest.fixture
def database(connection):
    ...

def test_somecase(connection, database):
    ...

those arguments already defined by the pytest.fixture

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文