2013-12-09 164 views
0

我有一個python函數somefunc(),它必須返回一個像這樣的數組。[1, 2, 3]。現在我正在寫一個單元測試來檢查一下這個函數返回:單元測試(Python),測試函數是否返回數組

class TestingFunctionsTest(unittest.TestCase): 
    def test_somefunc(self): 
     #what to write here ? 

我在Python測試新的,請幫助。

回答

1

你可以寫:

from mymodule import somefunc 

class TestingFunctionsTest(unittest.TestCase): 
    def test_somefunc(self): 
     self.assertEqual(somefunc(), [1, 2, 3]) 

這調用該函數並斷言返回值必須等於[1, 2, 3]

+0

非常感謝,你能告訴如何檢查函數是否返回整數例如?或者,如果可以的話,給一些鏈接說明如何使用assertEqual()? – user3057314

+0

@ user3057314你可以參考http://stackoverflow.com/q/3371255/1265154 – alko