Azure-docs: Azure Functions, python reference - Unit Testing

Created on 13 Aug 2019  Â·  7Comments  Â·  Source: MicrosoftDocs/azure-docs

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.

Pri1 assigned-to-author azure-functionsvc doc-enhancement triaged

Most helpful comment

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__'

All 7 comments

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

@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.

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']

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.

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.

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...

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__'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DeepPuddles picture DeepPuddles  Â·  3Comments

paulmarshall picture paulmarshall  Â·  3Comments

bityob picture bityob  Â·  3Comments

Agazoth picture Agazoth  Â·  3Comments

monteledwards picture monteledwards  Â·  3Comments