skip to Main Content

I have my DevOps pipeline which is using pytest to execute unit tests found in Python code.

I’m using a root folder called "core" for the main python functionality, and reference it using the following format:

import unittest
from core.objects.paragraph import Paragraph
from core.objects.sentence import Sentence

Running this locally (python -m pytest) works correctly and executes all of the tests.

However, everytime I run this from Azure DevOps I get the error message:

ModuleNotFoundError: No module named 'core'

I have no idea why this is happening 🤷‍♂️🤷‍♂️🤷‍♂️

For reference, this is my YAML build definition

trigger:
- main

pool:
  vmImage: ubuntu-latest
strategy:
  matrix:
    Python310:
      python.version: '3.10.7'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '$(python.version)'
  displayName: 'Use Python $(python.version)'

- script: |
    python -m pip install --upgrade pip
    pip install -r requirements.txt
  displayName: 'Install dependencies'

- script: |
    pip install pytest pytest-azurepipelines
  displayName: 'Install pytest'

- script: |
    python -m pytest --junitxml=$(Build.StagingDirectory)/Testoutput.xml
  workingDirectory: 'tests'
  displayName: 'Test with PyTest'

Once the execution has finished, this is the error message I’m seeing:

____________ ERROR collecting test_objects/test_paragraph_object.py ____________
ImportError while importing test module '/home/vsts/work/1/s/tests/test_objects/test_paragraph_object.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.10.7/x64/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_objects/test_paragraph_object.py:2: in <module>
    from core.objects.paragraph import Paragraph
E   ModuleNotFoundError: No module named 'core'
------------ generated xml file: /home/vsts/work/1/a/Testoutput.xml ------------

2

Answers


  1. Please make sure the directory/home/vsts/work/1/s/tests/has a __init__.pyfile.

    Login or Signup to reply.
  2. I was facing this problem. My project was structured as following:

    dags/
    ├─ deployments/
    │  ├─ item/
    │  ├─ users/
    │  │  ├─ dag_users.py
    ├─ utils1/
    │  ├─ helpers.py
    ├─ utils2/
    │  ├─ helpers2.py
    tests/
    ├─ test_dag_integrity.py
    

    From the pytest perspective, it is called src structure (as shown in the pytest documentation https://docs.pytest.org/en/latest/explanation/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner)

    So, I added a file into root folder called

    pyproject.toml

    Containing the following content

    [tool.pytest.ini_options]
    pythonpath = "dags"
    

    The structure after add toml file

    dags/
    tests/
    pyproject.toml
    

    This solve the problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search