0

I am running pytest on Github actions. It finds my tests in tests. One of the tests imports a function from utils/get_data.py which results in an error ModuleNotFoundError: No module named 'utils', which is an other folder I have.

is the git folder not set as the root directory in github actions pytest?

============================= test session starts ==============================
platform linux -- Python 3.7.12, pytest-6.1.2, py-1.11.0, pluggy-0.13.1
rootdir: /home/runner/work/fundmapper-webapp/fundmapper-webapp
collected 0 items / 2 errors

==================================== ERRORS ====================================
__________________ ERROR collecting tests/test_format_data.py __________________
ImportError while importing test module '/home/runner/work/fundmapper-webapp/fundmapper-webapp/tests/test_format_data.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_format_data.py:1: in <module>
    from utils.get_data import format_data, get_data
E   ModuleNotFoundError: No module named 'utils'
___________________ ERROR collecting tests/test_get_data.py ____________________
ImportError while importing test module '/home/runner/work/fundmapper-webapp/fundmapper-webapp/tests/test_get_data.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_get_data.py:1: in <module>
    from utils.get_data import get_data
E   ModuleNotFoundError: No module named 'utils'

The whole project is here. This is my workflow file:

# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: fundmapper webapp CICD

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.7
      uses: actions/setup-python@v2
      with:
        python-version: "3.7"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Lint with flake8
      run: |
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
    - name: Test with pytest
      run: |
        pytest
safex
  • 2,398
  • 17
  • 40

1 Answers1

0

Short answer: Change the last statement to python -m pytest

Generally though, one should probably follow a different approach using __init__.py when packaging the web app. Find a good read here

safex
  • 2,398
  • 17
  • 40