1
votes

I am using Azure Pipelines and getting the following error:

ImportError: Failed to import test module: QueryUrls
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/home/vsts/work/1/s/QueryUrls/__init__.py", line 2, in <module>
    import azure.functions as func
ModuleNotFoundError: No module named 'azure'

The error occurs when I run unit tests for an Azure Function written in Python. The code I am runnning in the pipeline (to run the unit tests) is the following:

- script: |
  python -m unittest QueryUrls/test_queryurls.py
displayName: 'Test with unittest'

The pipeline was running correctly before I added the above lines. Here is the script that is being called:

# tests/test_httptrigger.py
import unittest
from pathlib import Path

import azure.functions as func
from . import main
#from QueryUrls import my_function

class TestFunction(unittest.TestCase):
    def test_my_function(self):
        # Construct a mock HTTP request.
        req = func.HttpRequest(
            method='GET',
            body=None,
            url='/api/QueryUrls',
            params={'name': 'World'})

        # Call the function.
        resp = my_function(req)

        # Check the output.
        self.assertEqual(
            resp.get_body(),
            b'Hello World',
        )

if __name__ == '__main__':
    unittest.main()
2

2 Answers

2
votes

Proper way to do this is to have a requirements txt which has all the dependencies

 pip install -r requirements.txt -r requirements-test.txt

add the azure-functions to the requirements-text.txt and run the script at the beginning

1
votes

Are you sure that azure.function is isntalled on the machine?

Please install it before you run your tests:

script: pip install azure-functions

For pip list on agent I got this:

2020-07-17T12:50:55.7295699Z Generating script.
2020-07-17T12:50:55.7324332Z Script contents:
2020-07-17T12:50:55.7324840Z pip list
2020-07-17T12:50:55.7325214Z ========================== Starting Command Output ===========================
2020-07-17T12:50:55.7366079Z [command]/bin/bash --noprofile --norc /home/vsts/work/_temp/64166a13-122f-49af-b5af-153c399305d9.sh
2020-07-17T12:50:57.7202987Z ansible (2.9.10)
2020-07-17T12:50:57.7249252Z chardet (2.3.0)
2020-07-17T12:50:57.7250502Z crcmod (1.7)
2020-07-17T12:50:57.7251632Z cryptography (1.2.3)
2020-07-17T12:50:57.7252618Z ecdsa (0.13)
2020-07-17T12:50:57.7253446Z enum34 (1.1.2)
2020-07-17T12:50:57.7254214Z httplib2 (0.9.1)
2020-07-17T12:50:57.7255377Z idna (2.0)
2020-07-17T12:50:57.7256319Z ipaddress (1.0.16)
2020-07-17T12:50:57.7257152Z Jinja2 (2.8)
2020-07-17T12:50:57.7257929Z MarkupSafe (0.23)
2020-07-17T12:50:57.7258731Z mercurial (4.4.1)
2020-07-17T12:50:57.7259442Z paramiko (1.16.0)
2020-07-17T12:50:57.7260158Z pip (8.1.1)
2020-07-17T12:50:57.7260864Z pyasn1 (0.1.9)
2020-07-17T12:50:57.7261625Z pycrypto (2.6.1)
2020-07-17T12:50:57.7262364Z Pygments (2.1)
2020-07-17T12:50:57.7263653Z PyYAML (3.11)
2020-07-17T12:50:57.7264117Z setuptools (20.7.0)
2020-07-17T12:50:57.7264553Z six (1.10.0)

So as you see there is no azure.functions.