Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Airflow Python Unit Test?

Tags:

python

airflow

I'd like to add some unit tests for our DAGs, but could not find any. Is there a framework for unit test for DAGs? There is an End-to-End testing framework that exists but I guess it's dead: https://issues.apache.org/jira/browse/AIRFLOW-79. Please suggest, Thanks!

like image 653
Chengzhi Avatar asked Jul 31 '17 14:07

Chengzhi


2 Answers

Test your operators like this:

class TestMyOperator(TestCase):

    def test_execute(self):
        with DAG(dag_id="foo", start_date=datetime.now()):
            task = MyOperator(task_id="foo")
        ti = TaskInstance(task=task, execution_date=datetime.now())
        result = task.execute(ti.get_template_context())
        self.assertEqual(result, "foo")

Source

like image 199
Beau Barker Avatar answered Oct 18 '22 21:10

Beau Barker


Currently I wasn't able to find anything better than simply using BashOperator:

with DAG('platform-test', start_date=datetime(2017, 8, 29)) as dag:
    test_command = "python3 -m unittest --verbose {}".format(platform_test_fname)
    op = BashOperator(
        task_id="platform-test",
        bash_command=test_command,
    )
like image 2
Ikar Pohorský Avatar answered Oct 18 '22 22:10

Ikar Pohorský