python 的 unittest 无法读取到测试脚本?
目录结构如下:
(image_search_engine) ╭─pon@T4GPU ~/code/work/ponponon/image_search_engine ‹master*›
╰─➤ tree testing
testing
├── __init__.py
├── resource
│ ├── emoji001.jpg
│ ├── emoji002.jpg
│ ├── emoji003.jpg
│ └── s.jpg
├── test_meta.py
└── test_sample.py
运行测试命令
╰─➤ python -m unittest testing
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
unittest 会读取所有 test 开头的 .py 文件,但是我的两个 test_meta.py 和 test_sample.py 居然没有被读取?
但是写成 python -m unittest testing.test_sample
和 python -m unittest testing.test_meta
是可以的,直接 python -m unittest testing
却不行,为什么?
回复
1个回答

test
2024-06-26
Note
As a shortcut,
python -m unittest
is the equivalent ofpython -m unittest discover
. If you want to pass arguments to test discovery the discover sub-command must be used explicitly.
看文档,一旦加参数就必须用 python -m unittest discover
的形式才能自动查找测试文件。如果要指定目录的话,可能应该是: python -m unittest discover testing
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容