Pada
15 September 2026, semua lingkungan Cloud Composer 1 dan Cloud Composer 2 versi 2.0.x akan
mencapai akhir masa pakainya yang direncanakan, dan Anda tidak akan dapat menggunakannya. Sebaiknya rencanakan
migrasi ke Cloud Composer 3.
Pengujian unit DAG
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Contoh pengujian unit untuk DAG Python.
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code demonstrates how to create a unit test for a Python Directed Acyclic Graph (DAG) in Airflow.\u003c/p\u003e\n"],["\u003cp\u003eThe test validates that a module contains a valid DAG and detects any task cycles within it using the \u003ccode\u003etest_cycle\u003c/code\u003e function.\u003c/p\u003e\n"],["\u003cp\u003eApplication Default Credentials are required to authenticate to Cloud Composer.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Cloud sample browser can be used to discover more examples.\u003c/p\u003e\n"]]],[],null,["# DAG unit testing\n\nA sample unit test for a Python DAG.\n\nCode sample\n-----------\n\n### Python\n\n\nTo authenticate to Cloud Composer, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from airflow import models\n from airflow.utils.dag_cycle_tester import test_cycle\n\n\n def assert_has_valid_dag(module):\n \"\"\"Assert that a module contains a valid DAG.\"\"\"\n\n no_dag_found = True\n\n for dag in vars(module).values():\n if isinstance(dag, models.DAG):\n no_dag_found = False\n test_cycle(dag) # Throws if a task cycle is found.\n\n if no_dag_found:\n raise AssertionError(\"module does not contain a valid DAG\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=composer)."]]