pytest cannot find module [duplicate]

流过昼夜 提交于 2019-11-28 12:44:54

Just put an empty conftest.py file in the project root directory:

$ pwd
/home/usr/tmp/junk
$ touch conftest.py

Your project structure should become:

junk
├── conftest.py
├── junk
│   ├── __init__.py
│   └── ook.py
└── tests
    └── test_ook.py

What happens here: when pytest discovers a conftest.py, it modifies sys.path so it can import stuff from the conftest module. So, since now an empty conftest.py is found in rootdir, pytest will be forced to append it to sys.path. The side effect of this is that your junk module becomes importable.

Simply add __init__.py to the tests directory, and to all directories recursively inside it that contain test files.

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