pytest - 从父目录导入python
问题描述
我正在尝试使用以下目录结构对 filea.py 运行 pytest 测试
test_filea.py
from filea import *
def test_one_p_one():
r = one_p_one()
assert r == 2
文件.py
def one_p_one():
return 1 + 1
当我必须遵循目录结构时,一切正常。
├── filea.py
├── test_filea.py
但是当我将我的测试移动到这样的子目录中时
├── filea.py
└── tests
└── test_filea.py
我得到错误:
test_filea.py:1: in <module>
from filea import *
E ModuleNotFoundError: No module named 'filea'
我的编辑器似乎表明子目录中的文件中的导入是可以的..(没有读取波浪线)
但是当我使用“pytest”运行它时
我收到上面指出的错误。
解决方案
根据有关测试发现的 pytest 文档,尝试如下:
- 在目录中添加一个空
__init__.py
文件tests
; - 确保在运行时和
pytest .
的父目录是当前工作目录。filea.py
tests
推荐阅读
- javascript - 如何根据networkD3的对角网络中的值调整节点大小
- mysql - 在 MySQL 查询中将字符串格式化为日期
- jquery - 将 Vimeo 链接更改为 Vimeo 嵌入 jQuery
- java - Firebase crashlytics 仪表板不断加载
- git - Bitbucket ssh 公钥被拒绝
- robotframework - 如何在 Json Robot Framework 中获取值的类型
- android - 任何导航器都未处理未定义有效负载的“导航”操作。(反应原生)
- ios - '&' 和 ',' 在 swift 中关于协议组合有什么区别
- amazon-web-services - AWS CodePipeline 使用的 CIDR 是什么?
- ruby-on-rails - 全局窗口实例上的 Vuex