'How do I unit test my dependency injection in my Flask app?

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danangjoyoo