4

I am preparing unittests for Airflow DAGs, and I have ran into below problem.

I have a module with common functions, which imports airflow variables into common global variable:

"""
Script for common functions used in airflow DAGs
"""
[...]
from airflow.models import Variable
[...]
config = Variable.get("some_dag_details")
[...]

I have code that import DAG files for test validation.

import unittest
from unittest.mock import patch
from airflow.models import DagBag
from some_package import common_functions


class DAGValidationTest(unittest.TestCase):
    DETAILS = {
        "some important data"
    }

    def setUp(self):
        with patch.object(common_functions.Variable, 'get', return_value=self.DETAILS) as mock_get_variable:
[...]

Problem is that code for connecting to database is called before I am able to mock class Variable or variable config. Python is showing 4th line (from some_package import common_functions) as the error source. How can I mock this object before calling this script ?

0 Answers0