Python package not being found on Pytest

我们两清 提交于 2019-12-11 04:29:15

问题


I have the following project structure:

foo
│
├── foo
│   ├── cli
│   │    ├── tests
│   │    └── __init__.py
│   ├── core
│   │    ├── tests
│   │    └── __init__.py
│   ├── tests
│   │    ├── __init__.py
│   │    └── test_foo.py
│   ├── __init__.py
│   └── foo.py
├── kube
├── requirements
├── ...any-other-non-source-related
└── README.md

Inside the foo/foo.py file I have the following placeholder code:

import cli
import core


def say_hi():
    print('hi')


if __name__ == '__main__':
    say_hi()

And on the file foo/tests/test_foo.py I have the following:

from foo import foo


def test_foo():
    foo.say_hi()

    assert False

Then, in shell, I'm getting the following:

$ python foo/foo.py 
hi
$ pipenv run pytest
===================== test session starts =====================
platform linux -- Python 3.7.4, pytest-5.3.1, py-1.8.0, pluggy-0.13.1
rootdir: /xxx/xxx/xxx/xxx/python-project-folder-structures
collected 0 items / 1 error                                                                                                                                                                                         

===================== ERRORS =====================
_____ ERROR collecting foo/tests/test_foo.py _____
ImportError while importing test module '/xxx/xxx/xxx/xxx/python-project-folder-structures/foo/tests/test_foo.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
foo/tests/test_foo.py:1: in <module>
    from foo import foo
foo/foo.py:1: in <module>
    import cli
E   ModuleNotFoundError: No module named 'cli'
!!!!!!! Interrupted: 1 error during collection !!!!!!!
===================== 1 error in 0.05s =====================

Can anyone helps? Why Pytest is not founding my imports? I've tried a bunch of approachs, even with tests folder outside the foo folder (on root level) but the error still persists.

The only way that I've found to fix that is putting a conftest.py file on the code level, with that it would be discovered by pytest. I really don't want to do that, since I want to decouple my testing from my source code.


回答1:


If anyone strugles with this, I was able to find a solution. I removed the __init__.py file from the foo directoty, giving this structure:

foo
│
├── foo
│   ├── cli
│   │    ├── tests
│   │    └── __init__.py
│   ├── core
│   │    ├── tests
│   │    └── __init__.py
│   ├── tests
│   │    ├── __init__.py
│   │    └── test_foo.py
│   └── foo.py
├── kube
├── requirements
├── ...any-other-non-source-related
└── README.md

Them, on mt test, I got the following now:

import foo


def test_foo():
    foo.say_hi()

    assert False

I noticed that the behaviour of Pytest is to go from the test until the root dir, identifying packages. Since foo was a package (with a __init__.py file) it was adding it as one on my PATH :)

IMPORTANT Notice that the import foo statement is regarding the foo.py file, not the foo directory. So, if your file is called boo.py inside the foo directory, the import should be: import boo



来源:https://stackoverflow.com/questions/59120154/python-package-not-being-found-on-pytest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!