Mocking current_app in Flask unit pytest
Edit: Correct patch when using from flask import current_app
service.py
from flask import current_appdef fuu(): current_app.logger.info('hello world')
service_test.py
import unittestfrom unittest.mock import patchclass TestService(unittest.TestCase): @patch('service.current_app') def test_generate_image_happy_path(self, mock): from service import fuu fuu() assert 1 == 1
Answering for anyone that runs into the same issue. What you can do is create a dummy Flask
app and use its context in your test. No need to patch anything.
import unittestfrom service import fuuclass TestService(unittest.TestCase): app = Flask('test') def test_generate_image_happy_path(self): with app.app_context(): fuu() assert 1 == 1
Of course if you want to have access to your Flask
config, you'd have to pass that in to this dummy app.