Skip to content

Update the docs recommending dag.test() for custom operator tests - #71542

Merged
amoghrajesh merged 1 commit into
apache:mainfrom
astronomer:dag-test-clarification
Aug 14, 2026
Merged

amoghrajesh merged 1 commit into
apache:mainfrom
astronomer:dag-test-clarification

Conversation

@amoghrajesh

Copy link
Copy Markdown
Contributor

Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Our unit testing docs recommend a pattern that cannot work today. The best practices doc
tells you to build a Dag in memory and call dag.test() on it, but dag.test() creates a real Dag run, which requires the Dag to be serialized in the metadata database. A Dag built inside a test function is in no Dag bundle, so nothing
serializes it and the call raises:

    Cannot create DagRun for DAG my_custom_operator_dag because the dag is not serialized

For someone upgrading 3.1 to 3.2 or 3.3, they can hit above after Airflow 3.2 removed TaskInstance.run() and TaskInstance.render_templates() (#59835) and they might go looking for a replacement.

Changes:

  • best-practices.rst: replace the custom-operator example with direct
    execute() / poke() calls, add render_template_fields() and a deferrable
    variant, and name the removed TaskInstance methods alongside their
    replacements so a search for them lands here.
  • core-concepts/debug.rst: state the two prerequisites for dag.test() (a
    migrated database, a Dag file inside a configured bundle) and quote the error
    you get when they are not met.
  • howto/custom-operator.rst: add a testing section. Someone writing a custom
    operator reads this page, finds nothing about testing, and goes looking
    elsewhere.
  • tutorial/fundamentals.rst: correct the claim that airflow dags test runs
    "without registering any state in the database". It creates a Dag run and
    records task state.

The underlying confusion the docs never addressed: dag.test() is an integration tool needing a database and a Dag on disk, while execute() is the unit-test tool. The docs blurred the two, so readers reached for the wrong one.

Verification

Installed 3.1.0 and 3.3.0 in clean venvs and ran a custom operator and sensor
both ways:

3.1.0 3.3.0
in-memory Dag + dag.test() fails, not serialized fails, not serialized
Dag in a file + airflow dags test passes passes
TaskInstance.run present absent

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@eladkal eladkal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eladkal eladkal added this to the Airflow 3.3.2 milestone Aug 13, 2026
@amoghrajesh amoghrajesh self-assigned this Aug 14, 2026
@amoghrajesh
amoghrajesh merged commit 297b0c7 into apache:main Aug 14, 2026
70 checks passed
@amoghrajesh
amoghrajesh deleted the dag-test-clarification branch August 14, 2026 05:52
@github-actions

Copy link
Copy Markdown
Contributor

Backport successfully created: v3-3-test

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
✅ v3-3-test PR Link

potiuk pushed a commit that referenced this pull request Aug 17, 2026
…or tests (#71542)

(cherry picked from commit 297b0c7)

Co-authored-by: Amogh Desai <[email protected]>
potiuk pushed a commit that referenced this pull request Aug 17, 2026
…or tests (#71542) (#71587)

(cherry picked from commit 297b0c7)

Co-authored-by: Amogh Desai <[email protected]>
vatsrahul1001 pushed a commit that referenced this pull request Sep 7, 2026
…or tests (#71542) (#71587)

(cherry picked from commit 297b0c7)

Co-authored-by: Amogh Desai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants