简体   繁体   中英

'python -m unittest' specifying tests and/or modules instead of discover

I am attempting to run a subset of tests as part of a build; but I've been unable to run individual tests with python3 -m unittest <test> .

I have tried to follow the documentation and also read through: Running unittest with typical test directory structure , but I am still having no luck.

The directory structure is like this:

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

Running python3 -m unittest discover at the new_project level runs all tests fine; but trying to run a subset fails now matter how I try to cut it:

python3 -m unittest regression_tests

With the result: 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

With the Errors: AttributeError: 'module' object has no attribute 'test_pipeline'

Finally for completeness this also fails (as I would expect as the PYTHONPATH would be incorrectly set):

cd regression_tests
python3 -m unittest test_pipeline

With the Error: ImportError: No module named 'regression_tests' (The error is thrown on the line from regression_tests.helpers import helper_method .)

I don't think I'm doing anything that's non-standard here. Why is it that unittest discover works, but that unittest <test> fails?

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

python -m unittest discover -s directoryname -p "test_*"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM