簡體   English   中英

unittest.main()以運行包中的所有測試模塊

[英]unittest.main() to run all test modules in a package

我有幾個測試模塊保存在\\ tests中。 然后,在運行主程序之前,我通過指定以下內容將它們加載到主類中

from tests.ClassTests1 import *
from tests.ClassTests2 import *
...

unittest.main()

有什么方法可以指示unittest.main()在\\ tests中運行所有文件,而無需經歷如上所述的無休止的重復導入? 例如,我from tests import *嘗試過,但是它不起作用。

編輯:我是在程序化發現之后而不是通過命令行發現。

非常感謝!

感謝John Gordon的鏈接。 這就是它對我有用的原因:

from tests.ClassTests1 import *

# Run test cases first
suite = unittest.TestLoader().discover('tests', pattern='ClassTests*.py')
result = unittest.TextTestRunner(verbosity=2).run(suite)

不過,我仍然需要進口至少一個測試模塊,我不知道如果我這樣做的權利,因為它發現的整個測試層次tests

https://docs.python.org/2/library/unittest.html ,第25.3.3節。 測試發現:

Unittest支持簡單的測試發現。 為了與測試發現兼容,所有測試文件都必須是可從項目頂層目錄導入的模塊或包(這意味着它們的文件名必須是有效的標識符)。

測試發現是在TestLoader.discover()中實現的,但也可以從命令行使用它。 基本的命令行用法是:

cd project_directory

python -m unittest discover

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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