“python -m unittest”指定测试和/或模块而不是发现

问题描述 投票:0回答:1

我正在尝试运行测试子集作为构建的一部分;但我无法使用

python3 -m unittest <test>
运行单独的测试。

我尝试遵循文档并通读:使用典型的测试目录结构运行单元测试,但我仍然没有运气。

目录结构是这样的:

new_project
├── new_project
├──── __init__.py
├──── pipeline.py
├── regression_tests
├──── __init__.py
├──── helpers.py
├──── test_pipeline.py
└──── test_connectivity.py

python3 -m unittest discover
级别运行
new_project
可以正常运行所有测试;但尝试运行一个子集现在失败了,无论我如何尝试剪切它:

python3 -m unittest regression_tests

结果:

Ran 0 tests in 0.000s

python3 -m unittest regression_tests.test_pipeline
python3 -m unittest regression_tests.test_pipeline.TestClass
python3 -m unittest regression_tests.test_pipeline.TestClass.test_method
--- or ---
python3 -m unittest regression_tests/test_pipeline.py

有错误:

AttributeError: 'module' object has no attribute 'test_pipeline'

最后为了完整性,这也失败了(正如我所期望的,因为 PYTHONPATH 设置不正确):

cd regression_tests
python3 -m unittest test_pipeline

错误:

ImportError: No module named 'regression_tests'
(错误在线上抛出
from regression_tests.helpers import helper_method
。)

我不认为我在这里做了任何不标准的事情。为什么

unittest discover
有效,但
unittest <test>
失败?

python python-unittest
1个回答
0
投票

要成功运行子目录测试用例,您需要使用以下命令指定目录。

python -m unittest discover -s directoryname -p "test_*"
© www.soinside.com 2019 - 2024. All rights reserved.