Writing integration tests for DAGs #14096
-
Hi everyone, so I am currently trying to write tests for Airflow DAGs using the unittest package and running into some conceptual issues. My code is basically the following: I have a function that returns my DAG. This way I can instantiate it once (the production DAG) and ideally, instantiate a temporary test version of it in the test suite. The reasoning behind this is that I don't want to add runs to the production DAG every time I run the test suite.
This DAG is instantiated inside my DAGS_FOLDER (all good so far). Then inside my test file I am doing the following:
I have tried several things for the section, but no luck so far. I keep getting airflow.exceptions.DagNotFound: Dag id test-dag not found in DagModel . I've tried instantiating the DagBag and adding the dag to it (using the bag_dag method or by manually setting dagbag.dags[test_dag.dag_id] = test_dag), but no luck. What I wanted was a way to create an ephemeral DAG whose only purpose is to be instantiated and run during the test! This seemed like a reasonable software engineering approach. Has anyone faced this problem in the past? How did you solve it? Any suggestions are very welcome! Cheers |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can take a look how we implemented System Tests in Apache Airflow : https://github.com/apache/airflow/blob/master/TESTING.rst#airflow-system-tests System tests are basically executing an 'example_dag' and wait until it completes. We have a lot of such example DAGs in Airflow and there is an effort ongoing to automate execution of those #10835 This is very much your case under the hood - but done using some of the Airflow classes - loading DAG is done via DagBag and then it is executed using a Local (i think? Maybe it was the Debug one) Executor. All that is packed in SystemTest class that does extra initialization and allows to run it as Pytest test case. |
Beta Was this translation helpful? Give feedback.
You can take a look how we implemented System Tests in Apache Airflow : https://github.com/apache/airflow/blob/master/TESTING.rst#airflow-system-tests
System tests are basically executing an 'example_dag' and wait until it completes. We have a lot of such example DAGs in Airflow and there is an effort ongoing to automate execution of those #10835
This is very much your case under the hood - but done using some of the Airflow classes - loading DAG is done via DagBag and then it is executed using a Local (i think? Maybe it was the Debug one) Executor. All that is packed in SystemTest class that does extra initialization and allows to run it as Pytest test case.