標籤:ack mod response toc 函數 back key assm log
import unittest class UTest(unittest.TestCase): def test_upper(self): self.assertEqual(‘foo‘.upper(), ‘FOO‘) def test_isupper(self): self.assertTrue(‘FOO‘.isupper()) self.assertFalse(‘Foo‘.isupper()) if __name__ == ‘__main__‘:unittest.main()
註:0. unnitest 是 python 內建的庫,不需要額外的安裝即可用 1. 測試案例 (testcase) 都是由 unittest.TestCase 類建立的,對應的 test 開頭的 測試方法, 如上例的 test_upper 2. setUp() and tearDown() 方法用來定義一些初始化和清理的 指令, 這兩個方法分別在 每個測試案例 開始前和結束後執行。如果只要在所有的測試案例之前和之後只執行一次,則用 setUpClass() 和 tearDownClass()如果所有的測試案例都需要執行的一些共同步驟可以放在setUp(), 如果做一次就對所有的測試案例生效的就放在setUpClass(),譬如登陸(coolie對所有的測試案例都可以共用)setupClass() 和 tearDownClass() 必需加上裝飾器 @classmethod, 否則會出錯。 setUp() 和 tearDown() 則不用。 3. unittest.main()提供了一個測試指令碼的命令列介面。 4. 其他途徑運行測試案例有:
suite = unittest.TestLoader().loadTestsFromTestCase(UTest)unittest.TextTestRunner(verbosity=2).run(suite)
5. 命令列: python -m unittest test_module.TestClass 和 python -m unittest test_module.TestClass.test_method如上面的例子:python -m unittest UTest.UTestpython -m unittest UTest.UTest.test_upper 還可以傳一個 -v 標誌 來擷取 更詳細的測試結果:python -m unittest -v test_module如上例: D:\Python>python -m unittest -v UTest結果: test_isupper (UTest.UTest) ... oktest_upper (UTest.UTest) ... ok 6. 要運行一個class 裡的 所有測試案例(所有test開頭的方法),有以下幾種方法:A. unittest.main() 對應的命令列 是 ptyon module.pyB. suite = unittest.TestLoader().loadTestsFromTestCase(UTest)unittest.TextTestRunner(verbosity=2).run(suite)對應的命令列是 python -m unittest test_module.TestClassC. 建立一個TestSuite 執行個體,然後一個一個的把所有的測試案例加到這個測試集,最後通過 TextTestRunner 這個對象的run方法運行測試集如,此方法比較繁瑣,加入有50個測試案例,需要手動的把50個用例一個一個的加到TestSuite 裡
suite = unittest.TestSuite()suite.addTest(UTest(‘test_isupper‘))suite.addTest(UTest(‘test_upper‘))runner = unittest.TextTestRunner()runner.run(suite)
但是,如果是class 裡只有一個測試方法,但測試時需要不同的測試資料,C 的方法派上用場:
a.首先,重寫TestCase類的建構函式,把input 作為建構函式的參數(由於測試方法不能傳參數,所以只能在建構函式裡傳入需要的input),如:
def __init__(self, marketcode, stockcode, stocktype,methodName): super(Comparison, self).__init__(methodName) self.marketcode = marketcode self.stockcode = stockcode self.stocktype = stocktype
b. 然後是測試案例方法對input的引用
def test_getPriceInfo(self): stime = Utils.getTimestamp() mkt_price = ParseResponse.getMarketLastPrice(self.marketcode,self.stockcode,self.stocktype) position_price = ParseResponse.getPositionLastPrice(self.loginKey,self.loginCookie,self.stockcode,self.marketcode) self.infoDir["timestamp"] = stime self.infoDir["ticker"] = self.marketcode + self.stockcode self.infoDir["postionprice"] = position_price self.infoDir["marketprice"] = mkt_price AlertRecorder.cmpMktPosition(self.infoDir) print self.infoDir
c. 最後在class 外定義一個啟動並執行方法,其中 Comparison(stockInfo[0],stockInfo[1],stockInfo[2], "test_getPriceInfo") 即是建立一個測試類別樣本,test_getPriceInfo為測試案例方法名,這個是 unittest.TestCase類需要的參數。
def run(): suite = unittest.TestSuite() stockInfoList = Scheduler.get_mktPostion_stocks() for stockInfo in stockInfoList: suite.addTest(Comparison(stockInfo[0],stockInfo[1],stockInfo[2], "test_getPriceInfo")) runner = unittest.TextTestRunner() runner.run(suite)
綜上,封裝起來為:A. .main(verbosity=2), 其中verbosity=2 是使得結果輸出時更詳細B.
def run(): suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClassName) runner = unittest.TextTestRunner(verbosity=2) runner.run(suite)
C.
def run(): suite = unittest.TestSuite() suite.addTest(testCaseClassName(‘test_method‘)) runner = unittest.TextTestRunner(verbosity=2) runner.run(suite)
注意:上述三個運行測試的方法,在IDE上的輸出結果有點差別。A 和 C 輸出的結果:set up for classtest_isupper (__main__.UTest) ... oktest_sum (__main__.UTest) ... oktest_upper (__main__.UTest) ... ok 而B輸出結果為:test_sum (__main__.UTest) ... oktest_upper (__main__.UTest) ... okset up for classtest_isupper (__main__.UTest) ... ok 優先還是用 unittest.main() 和 unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromTestCase(testCaseClassName)) 7. 如果要跳過測試,則可以用到 裝飾器 @unittest.skip("reason") 和 @unittest.skipIf(condition,‘reason‘). 如果是針對個別測試案例,則在 測試案例方法加裝飾器。如果需要跳過所有的測試(譬如節假日)則在 setUpClass 上加裝飾器。這種情況下得注意兩個裝飾器的先後順序,先@classmethod 後 @unittest.skipIf()。如:@classmethod@unittest.skipIf(True, "To skip the test")def setUpClass(cls):print ‘set up for class‘ 另外,setUp()也可以跳過所有的測試,不過和 setUpClass 有區別: setUpClass 是一次性跳過所有的測試,運行結果顯示運行0個測試: Ran 0 tests in 0.000s OK (skipped=1)但setUp 則是每個測試案例都跳過運行,顯示結果是跑了N 個,跳過N 個:test_isupper (__main__.UTest) ... skipped ‘...reason...‘test_sum (__main__.UTest) ... skipped ‘...reason...‘test_upper (__main__.UTest) ... skipped ‘...reason...‘ 8. 如果要獲得測試結果中啟動並執行測試案例的總數以及成功和失敗的總數,可以從unittest.TestResult 中獲得 失敗的個數和啟動並執行總數
suite = unittest.TestLoader().loadTestsFromTestCase(UTest)runner = unittest.TextTestRunner(verbosity=2)result = runner.run(suite)print result.testsRun #啟動並執行測試案例的總數print len(result.failures) #失敗的測試案例的數目
關於 failures: A list containing 2-tuples of TestCase instances and strings holding formatted tracebacks. Each tuple represents a test where a failure was explicitlysignalled using the TestCase.assert*() methods.
Python unittest 學習