Testing in Python 2.7.3 Wen Liao
Testing in Python 2.7.3Wen Liao
目標
簡介測試、Python測試相關語法、以及測試元件
Outline
● 關於測試● Python 測試元件 & DEMO● 參考資料
● 萌典:測ㄘㄜˋ 試ㄕˋ○ 對機械、儀器等的性能和安全進行測量。
■ https://www.moedict.tw/#%E6%B8%AC%E8%A9%A6
● Longman: test○ a process used to discover whether equipment or a
product works correctly, or to discover more about it■ 用於確認產品或是設備是否正常動作的流程或■ 用於探索產品或是設備行為的流程
● http://www.ldoceonline.com/dictionary/test_1
Software testing (Wikipedia)
● 調查產品或服務的品質如何○ 符合規格?○ 行為正確?
http://en.wikipedia.org/wiki/Software_testing
Software testing (Wikipedia)
http://en.wikipedia.org/wiki/Software_testing
Cost to fix a defect Time detected
Requirements Architecture Construction System test Post-release
Time introduced
Requirements 1× 3× 5–10× 10× 10–100×
Architecture – 1× 10× 15× 25–100×
Construction – – 1× 10× 10–25×
Testing levels
● Unit testing○ Function○ Class method○ Module○ ...
● Integration testing● System testing● Acceptance testing
測試種類
● 確認功能正確● 確認能處理錯誤
Outline
● 關於測試● Python 測試元件 & DEMO● 參考資料
Test Environment
● Ubuntu 12.04.4 LTS● Python 2.7.3
Assertion
● assert 成立條件, “發生錯誤訊息”○ assert buf_len > 10, “buffer length too large”
● disable assertion○ python -O
What is Document
def my_add(x, y):‘’’
This is so called document use help(my_add) will show‘’’
return x + y
doctest module (1)
● Usage:在document中夾入○ >>> 要執行的敘述○ 預期輸出
doctest module (2)
import doctestdef my_add(x, y):
‘’’ >>> my_add(1, 2)
3‘’’return x + y
預期結果
執行測試
import doctest
執行 doctest
doctest.testmod()
DEMO
1. 直接執行測試程式2. 修改錯誤的assertion執行測試程式3. 關掉debug 執行測試程式4. 直接執行測試程式5. 直接執行測試程式加上 -v參數
unittest module
● 用途○ 測試自動化○ 共用測試碼
■ 初始化■ 結束
○ 整合測試項目○ 和被測試對象藕合性最小化
測試對象
● Function(s)● Class(es)
使用方式
● import unittest● 建立class,繼承unittest.TestCase● 寫測試案例● 開始測試
○ unittest.main()
my_cal.py
def my_add(x, y): return x + y
import unittestimport my_add from my_calcclass test_func(unittest.TestCase): def test_my_add(self): print("Test begin") res = my_add(1, 2) self.assertEqual(res, 3) # 接下頁
操作待測物
一定要test開頭,拿掉unittest不會測
繼承class
import unittest
預期結果
import 待測物
# 續上頁
def setUp(self): print("Setup")
def tearDown(self): print("Tear down")
if __name__ == "__main__": unittest.main()
Constructor
Destructor
確認constructor/destructor
$ python ./unit_test.py SetupTest beginTear down
Method Checks that New in
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b 2.7
assertIsNot(a, b) a is not b 2.7https://docs.python.org/2/library/unittest.html#module-unittest
Assertions in unittest
Method Checks that New in
assertIsNone(x) x is None 2.7
assertIsNotNone(x) x is not None 2.7
assertIn(a, b) a in b 2.7
assertNotIn(a, b) a not in b 2.7
assertIsInstance(a, b) isinstance(a, b) 2.7
assertNotIsInstance(a, b) not isinstance(a, b) 2.7
Assertions in unittest
https://docs.python.org/2/library/unittest.html#module-unittest
DEMO 1
● 直接執行程式● 故意設定錯誤條件,觀察assertion情況
Test cases
● unittest class裏面放的是○ Test cases
● 我能不能跑部份的?或是點菜?
○ Yes you can!
TestSuite class: 設定方式之一
suite = unittest.TestSuite()suite.addTest(testcase_class(‘test_case_name’))
範例:
suite = unittest.TestSuite() suite.addTest(test_func('test_my_add'))
TestSuite class: 執行方式之一
● unittest.TextTestRunner().run(suite)
DEMO 2
1. 跑test case程式2. 加入一個test case再執行
Outline
● 關於測試● Python 測試元件 & DEMO● 參考資料
參考資料 (1)
● Software Testing○ http://en.wikipedia.org/wiki/Software_testing
● Test Driven Development Tutorial○ http://www.slideshare.net/Skud/test-driven-
development-tutorial
參考資料 (2)
● Python Tutorial 第五堂(3)使用 assert 與 doctest○ http://www.codedata.com.tw/python/python-tutorial-
the-5th-class-3-assert-doctest/● The Python Standard Library -> 25.2.
doctest — Test interactive Python examples○ https://docs.python.org/2/library/doctest.
html#module-doctest
參考資料 (3)
● Python 單元測試(Unit Testing)○ http://imsardine.wordpress.com/tech/unit-testing-in-
python/● Python Tutorial 第六堂(1)使用 unittest 單元
測試○ http://www.codedata.com.tw/python/python-tutorial-
the-6th-class-1-unittest/
參考資料 (4)
● The Python Standard Library -> 25.3. unittest — Unit testing framework○ https://docs.python.org/2/library/unittest.
html#module-unittest
Q & A