preocess_video
in workflow.py
calls detect_objects()
whose return_value is what I want to mock.
But When I call process_video()
in test_workflow.py
I don't get the mocked return value, instead It actually call the detect_objects
function.
How can I mock the return value of detect_objects
when calling the parent function process_video
Directory Structure
.
├── __tests__
│ └── test_workflow.py
└── video_processor
├── __init__.py
├── detectors
│ ├── __init__.py
│ ├── object.py
└── workflow.py
Workflow.py
def process_video(request: Request, config: Config):
object_detections = detect_objects(request, config) # Want to mock the return value of detect_objects
# Detect_objects call other function internally
# More logic
# Some more logic
return data
test_workfkow.py
object_detections_dict == ... # sample data
def test_process_workflow(requests_mock, mocker):
# Arrange
request = Request(...)
config = Config(...)
# Mock some API calls
requests_mock.post(...)
requests_mock.post(...)
with mocker.patch("video_processor.detectors.object.detect_objects") as mock:
mock.return_value = object_detection_dict
process_video(request, config)
Tried this as suggested in comments:
# test_workflow.py
object_detections_dict == ... # sample data dict
@pytest.fixture
def detect_objects_fixture(mocker: MockerFixture):
mocker.patch("video_processor.detectors.object.detect_objects", return_value = object_detection_dict)
def test_process_workflow(requests_mock, detect_objects_fixture):
# Arrange
request = Request(...)
config = Config(...)
# Mock some API calls
requests_mock.post(...)
requests_mock.post(...)
result = process_video(request, config)
Creating a fixture using pytest_mock and unitest.mock to patch the return value of a function
Exemple :
Add detect_objects_fixture to the parameters of your test function, pytest should use the fixture to patch the return value of the function you targeted.
Here is an example :
Output :
Documentation
pip install pytest-mock