GitHub最热操作无法从测试/文件夹导入 [英] Github pytest Action failing to import from tests/ folder

查看:3
本文介绍了GitHub最热操作无法从测试/文件夹导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GitHub操作设置来运行pytest。这在大约一个月前有效,但现在我突然收到以下错误:

==================================== ERRORS ====================================
_____________ ERROR collecting tests/test_encoding/test_encode.py ______________
import file mismatch:
imported module 'test_method' has this __file__ attribute:
  /home/runner/work/my_module/my_module/src/my_module/tests/test_package/test_method.py
which is not the same as the test file we want to collect:
  /home/runner/work/my_module/my_module/tests/test_package/test_method.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

对于我的所有测试文件,这是一个替换了名称以提高可读性的示例。

与我的目录放置位置有关,但我不知道它为什么会发生这样的变化?由于某种原因,它正在/home/runner/work/my_module/my_module/src/my_module/而不是/home/runner/work/my_module/qiskit-quantum-knn/中查看我的tests/文件夹。我从未将src/文件夹添加到我的项目中。我还检查了是否意外地提交了__pycache__.pytest_cache/,但情况并非如此。

我将current logsa couple of months back(成功)中的日志进行了比较,在作业设置和操作设置期间,它似乎执行了完全相同的操作。

在我的本地计算机上运行pytest时,它工作得很好。 我的项目结构如下:

my_module
├── docs/
├── my_module
│   ├── package1/
│   ├── package2/
└── tests/
    ├── test_package1/
    │   └── test_method1.py
    └── test_package2/
        └── test_method2.py

为真正想深入查看日志的人添加了超链接。

推荐答案

它必须处理PIP和PYTEST的版本,而我没有将其固定在我的工作流中。解决方案是我的一个好朋友找到的,并固定在这个pull request中。

这意味着在我的项目根目录中添加一个pytest.ini,其中包含以下内容:

# pytest.ini
[pytest]
testpaths =
    tests
python_files = test_*.py
addopts = -rf --import-mode=importlib

这篇关于GitHub最热操作无法从测试/文件夹导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆