• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Re-upload at 12:20 am to fix wrong description at page 22 and 23
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
637
On Slideshare
0
From Embeds
0
Number of Embeds
10

Actions

Shares
Downloads
11
Comments
1
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Testing in Python 2.7.3 Wen Liao
  • 2. 目標 簡介測試、Python測試相關語法、以及測試元件
  • 3. Outline ● 關於測試 ● Python 測試元件 & DEMO ● 參考資料
  • 4. ● 萌典:測ㄘㄜˋ 試ㄕˋ ○ 對機械、儀器等的性能和安全進行測量。 ■ 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
  • 5. Software testing (Wikipedia) ● 調查產品或服務的品質如何 ○ 符合規格? ○ 行為正確? http://en.wikipedia.org/wiki/Software_testing
  • 6. 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×
  • 7. Testing levels ● Unit testing ○ Function ○ Class method ○ Module ○ ... ● Integration testing ● System testing ● Acceptance testing
  • 8. 測試種類 ● 確認功能正確 ● 確認能處理錯誤
  • 9. Outline ● 關於測試 ● Python 測試元件 & DEMO ● 參考資料
  • 10. Test Environment ● Ubuntu 12.04.4 LTS ● Python 2.7.3
  • 11. Assertion ● assert 成立條件, “發生錯誤訊息” ○ assert buf_len > 10, “buffer length too large” ● disable assertion ○ python -O
  • 12. What is Document def my_add(x, y): ‘’’ This is so called document use help(my_add) will show ‘’’ return x + y
  • 13. doctest module (1) ● Usage:在document中夾入 ○ >>> 要執行的敘述 ○ 預期輸出
  • 14. doctest module (2) import doctest def my_add(x, y): ‘’’ >>> my_add(1, 2) 3 ‘’’ return x + y 預期結果 執行測試 import doctest
  • 15. 執行 doctest doctest.testmod()
  • 16. DEMO 1. 直接執行測試程式 2. 修改錯誤的assertion執行測試程式 3. 關掉debug 執行測試程式 4. 直接執行測試程式 5. 直接執行測試程式加上 -v參數
  • 17. unittest module ● 用途 ○ 測試自動化 ○ 共用測試碼 ■ 初始化 ■ 結束 ○ 整合測試項目 ○ 和被測試對象藕合性最小化
  • 18. 測試對象 ● Function(s) ● Class(es)
  • 19. 使用方式 ● import unittest ● 建立class,繼承unittest.TestCase ● 寫測試案例 ● 開始測試 ○ unittest.main()
  • 20. my_cal.py def my_add(x, y): return x + y
  • 21. import unittest import my_add from my_calc class 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 待測物
  • 22. # 續上頁 def setUp(self): print("Setup") def tearDown(self): print("Tear down") if __name__ == "__main__": unittest.main() TestCase class開始測試前會呼叫 TestCase class結束測試後會呼叫
  • 23. 確認setUp和tearDown呼叫時機 $ python ./unit_test.py Setup Test begin Tear down
  • 24. 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.7 https://docs.python.org/2/library/unittest.html#module- Assertions in unittest
  • 25. 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-
  • 26. DEMO 1 ● 直接執行程式 ● 故意設定錯誤條件,觀察assertion情況
  • 27. Test cases ● unittest class裏面放的是 ○ Test cases ● 我能不能跑部份的?或是點菜? ○ Yes you can!
  • 28. TestSuite class: 設定方式之一 suite = unittest.TestSuite() suite.addTest(testcase_class(‘test_case_name’)) 範例: suite = unittest.TestSuite() suite.addTest(test_func('test_my_add'))
  • 29. TestSuite class: 執行方式之一 ● unittest.TextTestRunner().run(suite)
  • 30. DEMO 2 1. 跑test case程式 2. 加入一個test case再執行
  • 31. Outline ● 關於測試 ● Python 測試元件 & DEMO ● 參考資料
  • 32. 參考資料 (1) ● Software Testing ○ http://en.wikipedia.org/wiki/Software_testing ● Test Driven Development Tutorial ○ http://www.slideshare.net/Skud/test-driven- development-tutorial
  • 33. 參考資料 (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
  • 34. 參考資料 (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/
  • 35. 參考資料 (4) ● The Python Standard Library -> 25.3. unittest — Unit testing framework ○ https://docs.python.org/2/library/unittest. html#module-unittest
  • 36. Q & A