请教:如何运行 Python 单元测试文件

2017-08-14 14:25:13 +08:00
 yujianwjj

项目结构如下:

├── app
│   ├── __init__.py
│   └── utils.py
├── tests
│   ├── test_app.py

app/utils的内容如下:

class A:
	pass

tests/test_app内容如下:

from app.utils import A
def test_app():
	pass

直接运行py.test tests/test_app.py,会报找不到 app 这个模块,请问需要怎么修改,才能运行 tests 下面的测试。

2348 次点击
所在节点    Python
7 条回复
pekingzcc
2017-08-14 14:52:54 +08:00
lz 可以去搜一下 python 包管理 相对路径 绝对路径 ,搜完之后看看能不能解决~
lukic
2017-08-14 15:03:07 +08:00
app 的 父目录里运行 `python -m unittest tests.test_app`
messense
2017-08-14 15:05:52 +08:00
PYTHONPATH=. pytest tests/test_app.py
cloudyplain
2017-08-14 15:08:11 +08:00
py.test
BiggerLonger
2017-08-14 16:42:25 +08:00
直接上 nose
guyskk
2017-08-14 19:06:11 +08:00
yujianwjj
2017-08-14 19:25:34 +08:00
@guyskk 感谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/382799

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX