簡體   English   中英

'python -m unittest' 指定測試和/或模塊而不是發現

[英]'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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM