Github Actions Python Unit Test -> Missing Module

Viewed 1763

I have a python project with working code in a src module and unit tests in another module. When I run the unit test using Visual Studio code on my local workstation, the tests run fine. However, when I check the code into GitHib and use a GitHub action, the unit test cannot find the src directory with the needed modules. I have tried different configurations on the .yml file but nothing has worked so far.

Here is the .yml

jobs:
  on_build_actions:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
        working-directory: src
      - name: Run Tests
        run: python -m unittest test_hypedsearch
        working-directory: test

Here is the error

Run python -m unittest test_hypedsearch E ====================================================================== ERROR: test_hypedsearch (unittest.loader._FailedTest) ---------------------------------------------------------------------- ImportError: Failed to import test module: test_hypedsearch Traceback (most recent call last): File "/usr/lib/python3.8/unittest/loader.py", line 154, in loadTestsFromName module = import(module_name) File "/home/runner/work/hypedsearch/hypedsearch/test/test_hypedsearch.py", line 5, in from src import runner, utils, database, objects ModuleNotFoundError: No module named 'src'

and here is the top of the unit test code that is throwing the error:

import sys, os.path  
import unittest
src_path = (os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) + '/src/')
sys.path.append(src_path)
from src import runner, utils, database, objects

Do I need to add a different reference on github to point to the src folder? Here is the directory outline:

enter image description here

2 Answers

I made it work on a similar repository using the following workflow implementation (adapted to your context):

jobs:
  on_build_actions:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: |
          cd $GITHUB_WORKSPACE
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Run Tests    
        run: python -m unittest test/test_hypedsearch.py

I don't know the reason, but I couldn't make it work using the working-directory variable as well. So this is a possible workaround...

Here is the successful workflow run: https://github.com/GuillaumeFalourd/hypedsearch/runs/3032440691?check_suite_focus=true

And here is the repository I used to test if you want to check: https://github.com/GuillaumeFalourd/hypedsearch

Note: I don't know if this repo is related to yours, but at least it had a similar structure.

Turns out the way to get it to work on GitHub was to remove the reference to the "src" folder:

import runner, utils, database, objects

Now the challenge is to get that line working in VSCode #sigh

Related