[英]Python's unittest framework TestCase curiousity
我正在嘗試在python 3.3.2中運行一個TestCase,它有幾個測試方法:
class ttt(unittest.TestCase):
def setUp(self):
...
def tearDown(self):
...
def test_test1(self):
...
def test_test2(self):
...
if __name__ == "__main__":
instance = ttt()
instance.run()
文檔說明如下:
每個TestCase實例都將運行一個基本方法:名為methodName的方法。 但是,默認methodName,runTest()的標准實現將以test作為單獨測試開始運行每個方法,並相應地計算成功和失敗。 因此,在TestCase的大多數用法中,既不會更改methodName,也不會重新實現默認的runTest()方法。
但是,當我運行代碼時,我得到以下內容:
'ttt' object has no attribute 'runTest'
我想問:這是一個錯誤嗎? 如果不是為什么沒有runTest方法呢? 難道我做錯了什么?
當單元測試框架運行測試用例時,它會為每個測試創建一個測試類的實例。
即模擬單元測試框架需要做什么:
if __name__ == "__main__":
for testname in ["test_test1", "test_test2"]:
instance = ttt(testname)
instance.run()
在模塊中運行單元測試的正確方法是:
if __name__ == "__main__":
unittest.main()
......但我猜你已經知道了。
關於runTest
: unittest.TestCase.__init__
簽名和docstring是:
def __init__(self, methodName='runTest'):
"""Create an instance of the class that will use the named test
method when executed. Raises a ValueError if the instance does
not have a method with the specified name.
"""
這意味着如果未在構造函數中指定測試名稱,則默認為runTest
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.