简体   繁体   中英

Github pytest Action failing to import from tests/ folder

I have a GitHub action setup to run pytest. This worked a month or so ago, but now suddenly I get the following error:

==================================== 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

for all my test files, this is an example with names replaced for readability.

It has something to do with where my directories are placed, but I have no idea why it changed like this? For some reason it's looking at my tests/ folder in /home/runner/work/my_module/my_module/src/my_module/ instead of /home/runner/work/my_module/qiskit-quantum-knn/ . I have never added a src/ folder to my project. I also checked if I accidentaly committed the __pycache__ or .pytest_cache/ , but that is not the case.

I compared the current logs to my logs from a couple of months back (which succeeded) and it seems to do exactly the same during the job setup and action setup.

When running pytest on my local machine, it works just fine. My project is structured as follows:

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

Added hyperlinks for the person who really wants to dive into the logs.

It had to do something with the version of pip and pytest, which I did not have pinned in my workflow. The solution was found by a good friend of mine, and is fixed in this pull request .

It meant adding a pytest.ini to the root of my project which contains the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM