[英]'python -m unittest' specifying tests and/or modules instead of discover
我正在尝试运行一部分测试作为构建的一部分; 但我一直无法使用python3 -m unittest <test>
运行单独的测试。
我试图按照文档并通读: 使用典型的测试目录结构运行单元测试,但我仍然没有运气。
目录结构是这样的:
new_project
├── new_project
├──── __init__.py
├──── pipeline.py
├── regression_tests
├──── __init__.py
├──── helpers.py
├──── test_pipeline.py
└──── test_connectivity.py
在new_project
级别运行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 -m unittest discover -s directoryname -p "test_*"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.