toddlers/tests/conftest.py

32 lines
959 B
Python
Raw Normal View History

import sys
from unittest.mock import MagicMock
import pytest
from toddlers.utils.requests import make_session
@pytest.fixture
def toddler(request, monkeypatch):
"""Fixture creating a toddler for a class testing it
The test class must set the toddler class in its `toddler_cls` attribute.
The fixture will mock out the `make_session` function before creating the
object so that any request objects the toddler creates in its constructor
won't be real.
"""
test_cls = request.cls
if not hasattr(test_cls, "toddler_cls"):
raise RuntimeError(f"{test_cls} needs to set `toddler_cls`")
toddler_cls = test_cls.toddler_cls
toddler_module = sys.modules[toddler_cls.__module__]
for name in dir(toddler_module):
obj = getattr(toddler_module, name)
if obj is make_session:
monkeypatch.setattr(toddler_module, name, MagicMock())
toddler_obj = toddler_cls()
return toddler_obj