You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "ivanwakeup (via GitHub)" <gi...@apache.org> on 2024/04/11 18:57:47 UTC

[I] Unable to mock Variable in python unittest [airflow]

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

   ### What do you see as an issue?
   
   The airflow Best Practices documentation page suggests using `unittest.mock.patch` along with setting `os.environ` values to mock Variables and Connections. This doesn't seem to work in practice (testing against airflow 2.8.1)--airflow attempts to read Variable values from variable table in DB instead of using `os.environ` overrides.
   
   
   
   ### Solving the problem
   
   example non-working code:
   
   ```
   from unittest.mock import patch
   from airflow.models import Variable
   
   def test_variable_fetch():
       with patch.dict('os.environ', AIRFLOW_VAR_my_test_var="my_test_value"):
           print(Variable.get("my_test_var"))
   ```
   
   ### Anything else
   
   https://airflow.apache.org/docs/apache-airflow/2.8.4/best-practices.html#mocking-variables-and-connections - guide page in question
   
   ### 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: commits-unsubscribe@airflow.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to mock Variable in python unittest [airflow]

Posted by "aritra24 (via GitHub)" <gi...@apache.org>.
aritra24 commented on issue #38944:
URL: https://github.com/apache/airflow/issues/38944#issuecomment-2050983951

   Closing this issue in that case


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to mock Variable in python unittest [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on issue #38944:
URL: https://github.com/apache/airflow/issues/38944#issuecomment-2050324177

   Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to mock Variable in python unittest [airflow]

Posted by "ivanwakeup (via GitHub)" <gi...@apache.org>.
ivanwakeup commented on issue #38944:
URL: https://github.com/apache/airflow/issues/38944#issuecomment-2050438532

   I think i found the issue. lowercase `my_test_var` instead of `MY_TEST_VAR` in the variable definition causes airflow to not pickup the value. changing this fixes it.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Unable to mock Variable in python unittest [airflow]

Posted by "aritra24 (via GitHub)" <gi...@apache.org>.
aritra24 closed issue #38944: Unable to mock Variable in python unittest
URL: https://github.com/apache/airflow/issues/38944


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org