Chengzhi
Chengzhi

Reputation: 2591

Airflow Python Unit Test?

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!

Upvotes: 28

Views: 10541

Answers (2)

bcb
bcb

Reputation: 2078

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

Upvotes: 14

Ikar Pohorský
Ikar Pohorský

Reputation: 5039

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,
    )

Upvotes: 2

Related Questions