Top Banner
Testing in Python 2.7.3 Wen Liao
36

Testing in python 2.7.3

Jan 15, 2015

Download

Technology

Wen Liao

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Testing in python 2.7.3

Testing in Python 2.7.3Wen Liao

Page 2: Testing in python 2.7.3

目標

簡介測試、Python測試相關語法、以及測試元件

Page 3: Testing in python 2.7.3

Outline

● 關於測試● Python 測試元件 & DEMO● 參考資料

Page 4: Testing in python 2.7.3

● 萌典:測ㄘㄜˋ 試ㄕˋ○ 對機械、儀器等的性能和安全進行測量。

■ 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

Page 5: Testing in python 2.7.3

Software testing (Wikipedia)

● 調查產品或服務的品質如何○ 符合規格?○ 行為正確?

http://en.wikipedia.org/wiki/Software_testing

Page 6: Testing in python 2.7.3

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×

Page 7: Testing in python 2.7.3

Testing levels

● Unit testing○ Function○ Class method○ Module○ ...

● Integration testing● System testing● Acceptance testing

Page 8: Testing in python 2.7.3

測試種類

● 確認功能正確● 確認能處理錯誤

Page 9: Testing in python 2.7.3

Outline

● 關於測試● Python 測試元件 & DEMO● 參考資料

Page 10: Testing in python 2.7.3

Test Environment

● Ubuntu 12.04.4 LTS● Python 2.7.3

Page 11: Testing in python 2.7.3

Assertion

● assert 成立條件, “發生錯誤訊息”○ assert buf_len > 10, “buffer length too large”

● disable assertion○ python -O

Page 12: Testing in python 2.7.3

What is Document

def my_add(x, y):‘’’

This is so called document use help(my_add) will show‘’’

return x + y

Page 13: Testing in python 2.7.3

doctest module (1)

● Usage:在document中夾入○ >>> 要執行的敘述○ 預期輸出

Page 14: Testing in python 2.7.3

doctest module (2)

import doctestdef my_add(x, y):

‘’’ >>> my_add(1, 2)

3‘’’return x + y

預期結果

執行測試

import doctest

Page 15: Testing in python 2.7.3

執行 doctest

doctest.testmod()

Page 16: Testing in python 2.7.3

DEMO

1. 直接執行測試程式2. 修改錯誤的assertion執行測試程式3. 關掉debug 執行測試程式4. 直接執行測試程式5. 直接執行測試程式加上 -v參數

Page 17: Testing in python 2.7.3

unittest module

● 用途○ 測試自動化○ 共用測試碼

■ 初始化■ 結束

○ 整合測試項目○ 和被測試對象藕合性最小化

Page 18: Testing in python 2.7.3

測試對象

● Function(s)● Class(es)

Page 19: Testing in python 2.7.3

使用方式

● import unittest● 建立class,繼承unittest.TestCase● 寫測試案例● 開始測試

○ unittest.main()

Page 20: Testing in python 2.7.3

my_cal.py

def my_add(x, y): return x + y

Page 21: Testing in python 2.7.3

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 待測物

Page 22: Testing in python 2.7.3

# 續上頁

def setUp(self): print("Setup")

def tearDown(self): print("Tear down")

if __name__ == "__main__": unittest.main()

Constructor

Destructor

Page 23: Testing in python 2.7.3

確認constructor/destructor

$ python ./unit_test.py SetupTest beginTear down

Page 26: Testing in python 2.7.3

DEMO 1

● 直接執行程式● 故意設定錯誤條件,觀察assertion情況

Page 27: Testing in python 2.7.3

Test cases

● unittest class裏面放的是○ Test cases

● 我能不能跑部份的?或是點菜?

○ Yes you can!

Page 28: Testing in python 2.7.3

TestSuite class: 設定方式之一

suite = unittest.TestSuite()suite.addTest(testcase_class(‘test_case_name’))

範例:

suite = unittest.TestSuite() suite.addTest(test_func('test_my_add'))

Page 29: Testing in python 2.7.3

TestSuite class: 執行方式之一

● unittest.TextTestRunner().run(suite)

Page 30: Testing in python 2.7.3

DEMO 2

1. 跑test case程式2. 加入一個test case再執行

Page 31: Testing in python 2.7.3

Outline

● 關於測試● Python 測試元件 & DEMO● 參考資料

Page 32: Testing in python 2.7.3

參考資料 (1)

● Software Testing○ http://en.wikipedia.org/wiki/Software_testing

● Test Driven Development Tutorial○ http://www.slideshare.net/Skud/test-driven-

development-tutorial

Page 33: Testing in python 2.7.3

參考資料 (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

Page 34: Testing in python 2.7.3

參考資料 (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/

Page 35: Testing in python 2.7.3

參考資料 (4)

● The Python Standard Library -> 25.3. unittest — Unit testing framework○ https://docs.python.org/2/library/unittest.

html#module-unittest

Page 36: Testing in python 2.7.3

Q & A