簡體   English   中英

如何使用unittest在Python中編寫測試

[英]How to write tests in Python using unittest

我想用TDD方法編寫我的庫,但是我不知道如何設計目錄結構(或如何使用unittest )。 現在,我創建一個目錄樹,例如:

myproject (directory)
    - tests (directory)
    - src (directory)
    - test.py (file running tests)

src每個類都有其自己的unittest類。 每個目錄都有其自己的__init__.py文件。 我只想從test.py文件運行測試,並且由於此,每個測試都可以from src.modulename import classname然后運行test unittest.main()函數。 不幸的是,它不起作用(運行零測試)。

這是好方法嗎? 我有什么錯

文件test.py的代碼應類似於:

from tests import *
import unittest

if __name__ == '__main__':
    testsuite = unittest.TestLoader().discover('.')
    unittest.TextTestRunner(verbosity=1).run(testsuite)

此代碼從tests目錄復制所有測試,因為它復制了整個程序包。 main方法運行tests包類中包含的所有測試方法。 每個測試文件名都必須以test開頭。

暫無
暫無

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

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