【问题标题】:Github Actions Python Unit Test -> Missing ModuleGithub Actions Python 单元测试 -> 缺少模块
【发布时间】:2021-09-19 11:12:06
【问题描述】:

我有一个 python 项目,其中一个 src 模块中的工作代码和另一个模块中的单元测试。当我在本地工作站上使用 Visual Studio 代码运行单元测试时,测试运行良好。但是,当我将代码签入 GitHib 并使用 GitHub 操作时,单元测试无法找到包含所需模块的 src 目录。我在 .yml 文件上尝试了不同的配置,但到目前为止没有任何效果。

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

这是错误

运行 python -m unittest test_hypedsearch E ==================================================== ==================== 错误:test_hypedsearch (unittest.loader._FailedTest) -------------------------------------------------- -------------------- ImportError:导入测试模块失败:test_hypedsearch Traceback (最近一次通话最后):文件 “/usr/lib/python3.8/unittest/loader.py”,第 154 行,在 loadTestsFromName module = import(module_name) 文件 "/home/runner/work/hypedsearch/hypedsearch/test/test_hypedsearch.py​​", 第 5 行,在 from src import runner, utils, database, objects ModuleNotFoundError: No module named 'src'

这是引发错误的单元测试代码的顶部:

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

是否需要在 github 上添加不同的引用以指向 src 文件夹?这是目录大纲:

【问题讨论】:

    标签: python unit-testing github github-actions


    【解决方案1】:

    原来让它在 GitHub 上运行的方法是删除对“src”文件夹的引用:

    import runner, utils, database, objects
    

    现在的挑战是让这条线在 VSCode 中工作#sigh

    【讨论】:

      【解决方案2】:

      我使用以下工作流实现(适应您的上下文)使其在类似的存储库上工作:

      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
      

      我不知道原因,但我也无法使用 working-directory 变量使其工作。所以这是一个可能的解决方法......

      这是成功运行的工作流:https://github.com/GuillaumeFalourd/hypedsearch/runs/3032440691?check_suite_focus=true

      如果你想检查,这里是我用来测试的存储库:https://github.com/GuillaumeFalourd/hypedsearch

      注意:我不知道这个 repo 是否与你的相关,但至少它具有相似的结构。

      【讨论】:

      • 感谢您查看此内容。你能分叉我们正在处理的回购吗? github.com/ryanlayerlab/hypedsearch。另一个是我们迁移到实验室的旧的
      • 我确实添加了 cd $GITHUB_WORKSPACE 行,但这并没有帮助......
      • 嗨!我分叉了这个 repo 并使用我在上面共享的same implementation 使工作流工作here(我只需要创建requirements.txt 文件,因为它丢失了)。是否要我向存储库打开合并请求?
      猜你喜欢
      • 2023-02-02
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2014-12-27
      • 1970-01-01
      • 2018-10-09
      相关资源
      最近更新 更多