tjaeuth opened a new issue, #58893:
URL: https://github.com/apache/airflow/issues/58893

   ### What do you see as an issue?
   
   In the documentation under 
https://airflow.apache.org/docs/apache-airflow/stable/best-practices.html#unit-tests
   it is mentioned that you can run unit tests for your custom operators like 
this:
   
   ```python
   import pendulum
   
   from airflow.sdk import DAG, TaskInstanceState
   
   
   def test_my_custom_operator_execute_no_trigger(dag):
       TEST_TASK_ID = "my_custom_operator_task"
       with DAG(
           dag_id="my_custom_operator_dag",
           schedule="@daily",
           start_date=pendulum.datetime(2021, 9, 13, tz="UTC"),
       ) as dag:
           MyCustomOperator(
               task_id=TEST_TASK_ID,
               prefix="s3://bucket/some/prefix",
           )
   
       dagrun = dag.test()
       ti = dagrun.get_task_instance(task_id=TEST_TASK_ID)
       assert ti.state == TaskInstanceState.SUCCESS
       # Assert something related to tasks results: ti.xcom_pull()
   ```
   
   I see two problems:
   
   1. `test_my_custom_operator_execute_no_trigger(dag)` takes a *dag* parameter 
that is of no use, and it is not documented where it comes from.
   2. I was unable to use this code from inside my pytest test file. Only if I 
put this code into a dag file under the dag directory, then I was able to run 
this code as of the serialization error (same error as documented here: 
https://github.com/apache/airflow/issues/56657).
   
   
   
   
   ### Solving the problem
   
   I am not sure, but I am missing the link in the documentation that 
dag.test() can only be used in a DAG file and never in a pytest file.
   
   Please explain how to do unit testing in a pytest or unit test environment. 
Give some examples and not just this code snippet.
   
   ### Anything else
   
   Maybe I am wrong, and it should work in a pytest environment, and it is not 
because of a bug?
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to