Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Functions, python reference - Unit Testing #36991

Closed
LukeGahan opened this issue Aug 13, 2019 — with docs.microsoft.com · 7 comments
Closed

Azure Functions, python reference - Unit Testing #36991

LukeGahan opened this issue Aug 13, 2019 — with docs.microsoft.com · 7 comments

Comments

Copy link

LukeGahan commented Aug 13, 2019

Could you please include information on the running of unit tests when importing a module from a SharedCode path (As described in the "Folder Structure" section, from __app__.SharedCode import myFirstHelperFunction ).

At present the running of unit tests fails as the __app__ element of the module path is not available.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@mike-urnun-msft
Copy link
Contributor

Hi @LukeGahan - Thank you for your feedback! We will review and update as appropriate.

@PramodValavala-MSFT
Copy link
Member

PramodValavala-MSFT commented Aug 14, 2019

@LukeGahan There seems to be an open discussion here - Azure/azure-functions-python-worker#469 - for this which I believe would be a better place to discuss this for a more insightful discussion.

Also, I've tried with direct imports before which I believe should still work and something similar is discussed in the python-worker issue as well.

We are assigning this issue to the content author for further review.

@LukeGahan
Copy link
Author

LukeGahan commented Aug 14, 2019

Hi @PramodValavala-MSFT ,

Yeap that's definitely helpful, I'll follow up on that thread. I was using direct imports successfully until the end of last week but something seems to have changed in the way the python functions are executed. I tried logging the output of sys.path from my funtion and it seems that the home folder (/home/site/wwwroot/) is not present, which I believe is preventing me from doing a direct import

['/azure-functions-host/workers/python', '/home/site/wwwroot/.python_packages/lib/python3.6/site-packages', '/home/site/wwwroot/worker_venv/lib/python3.6/site-packages', '/tmp/functions\\standby\\wwwroot', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']

@raphaeldovale
Copy link

Any news on this? I needed to change my code due to the changes @LukeGahan said and my tests broke with error ModuleNotFoundError: No module named '__app__'

Even following the unit testing section I am unable to make it work.

Copy link

jrm346 commented Sep 17, 2019

Hi guys,

Here is my hacky way of letting me unit test while still having something that runs on the cloud.

try:
    from __app__.SharedCode import myFirstHelperFunction
except ModuleNotFoundError:
    from SharedCode import myFirstHelperFunction

I know its ugly but it lets me unittest and still have working functions.

Copy link

Thanks, @jrm346 ! I'm now doing something similar. The only difference is I was seeing ValueErrors so my exception handling is a little different.

try:
    from ..common import storage_manager
except (ModuleNotFoundError, ValueError):
    from common import storage_manager

The guidance on this still seems lacking to me. I landed on this approach after a few other attempts failed. It should be safe to assume that most real-world usage of Python functions will have both shared code and unit tests. Seems like the docs don't quite reflect that...

@PMEds28 PMEds28 closed this as completed Nov 14, 2019
Copy link

Im not sure why this is closed off as this is still very much an issue - following the documentation guidance I still get ModuleNotFoundError: No module named '__app__'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants