SlideShare una empresa de Scribd logo
1 de 36
Descargar para leer sin conexión
Testing in Python 2.7.3
Wen 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 doctest
def 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 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 待測物
# 續上頁
def setUp(self):
print("Setup")
def tearDown(self):
print("Tear down")
if __name__ == "__main__":
unittest.main()
TestCase class開始測試前會呼叫
TestCase class結束測試後會呼叫
確認setUp和tearDown呼叫時機
$ python ./unit_test.py
Setup
Test begin
Tear 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.7
https://docs.python.org/2/library/unittest.html#module-
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-
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

Más contenido relacionado

La actualidad más candente

Phpunit入门 r2
Phpunit入门 r2Phpunit入门 r2
Phpunit入门 r2Baohua Cai
 
Bash入门基础篇
Bash入门基础篇Bash入门基础篇
Bash入门基础篇Zhiyao Pan
 
改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法crasysatan
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试lydiafly
 
Keep your code clean
Keep your code cleanKeep your code clean
Keep your code cleanmacrochen
 
所谓闭包
所谓闭包所谓闭包
所谓闭包youzitang
 
Python learn guide
Python learn guidePython learn guide
Python learn guiderobin yang
 
Bash shell script 教學
Bash shell script 教學Bash shell script 教學
Bash shell script 教學Ming-Sian Lin
 
Power shell – scriptblock
Power shell – scriptblockPower shell – scriptblock
Power shell – scriptblockLearningTech
 
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘Liu Allen
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1Sheng-Han Su
 
常見設計模式介紹
常見設計模式介紹常見設計模式介紹
常見設計模式介紹Jace Ju
 
Programming python - part 1
Programming python - part 1Programming python - part 1
Programming python - part 1Che-Cheng Hsu
 
所谓闭包
所谓闭包所谓闭包
所谓闭包ilovey4
 
Ecma script3
Ecma script3 Ecma script3
Ecma script3 gniavaj
 
LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLin Yo-An
 

La actualidad más candente (19)

Phpunit入门 r2
Phpunit入门 r2Phpunit入门 r2
Phpunit入门 r2
 
Bash入门基础篇
Bash入门基础篇Bash入门基础篇
Bash入门基础篇
 
改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试
 
Keep your code clean
Keep your code cleanKeep your code clean
Keep your code clean
 
Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用 Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用
 
Python變數與資料運算
Python變數與資料運算Python變數與資料運算
Python變數與資料運算
 
所谓闭包
所谓闭包所谓闭包
所谓闭包
 
Python learn guide
Python learn guidePython learn guide
Python learn guide
 
Bash shell script 教學
Bash shell script 教學Bash shell script 教學
Bash shell script 教學
 
Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業
 
Power shell – scriptblock
Power shell – scriptblockPower shell – scriptblock
Power shell – scriptblock
 
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
课题一:PHP5.3、PHP5.4的特性介绍与深度挖掘
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1
 
常見設計模式介紹
常見設計模式介紹常見設計模式介紹
常見設計模式介紹
 
Programming python - part 1
Programming python - part 1Programming python - part 1
Programming python - part 1
 
所谓闭包
所谓闭包所谓闭包
所谓闭包
 
Ecma script3
Ecma script3 Ecma script3
Ecma script3
 
LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHP
 

Destacado

PyConAPAC2014 BoF Introduction
PyConAPAC2014 BoF IntroductionPyConAPAC2014 BoF Introduction
PyConAPAC2014 BoF IntroductionChun-Yu Tseng
 
Testing in Python @ Kaosiung.py 2014.05.26
Testing in Python @ Kaosiung.py 2014.05.26Testing in Python @ Kaosiung.py 2014.05.26
Testing in Python @ Kaosiung.py 2014.05.26Chun-Yu Tseng
 
當資訊科技撞上臺南牛肉湯的那些事兒 @ 2014 NCKU EDiT 科技啓蒙
當資訊科技撞上臺南牛肉湯的那些事兒 @ 2014 NCKU EDiT 科技啓蒙當資訊科技撞上臺南牛肉湯的那些事兒 @ 2014 NCKU EDiT 科技啓蒙
當資訊科技撞上臺南牛肉湯的那些事兒 @ 2014 NCKU EDiT 科技啓蒙小均 張
 
PyConAPAC2014 Tainan.py 介紹
PyConAPAC2014 Tainan.py 介紹PyConAPAC2014 Tainan.py 介紹
PyConAPAC2014 Tainan.py 介紹Chun-Yu Tseng
 
Rubypython - 用 ruby 愛上 python
Rubypython - 用 ruby 愛上 pythonRubypython - 用 ruby 愛上 python
Rubypython - 用 ruby 愛上 pythonHonda Dai
 
Guide to GStreamer Application Development Manual: CH1 to CH10
Guide to GStreamer Application Development Manual: CH1 to CH10Guide to GStreamer Application Development Manual: CH1 to CH10
Guide to GStreamer Application Development Manual: CH1 to CH10Wen Liao
 
GNU gettext簡介 - 以C語言為範例
GNU gettext簡介 - 以C語言為範例GNU gettext簡介 - 以C語言為範例
GNU gettext簡介 - 以C語言為範例Wen Liao
 
故事:自由軟體和 Richard Stallman
故事:自由軟體和 Richard Stallman故事:自由軟體和 Richard Stallman
故事:自由軟體和 Richard StallmanWen Liao
 
開放街圖 自助旅行的好幫手
開放街圖   自助旅行的好幫手開放街圖   自助旅行的好幫手
開放街圖 自助旅行的好幫手Wen Liao
 
Hello world在那邊?背景說明
Hello world在那邊?背景說明Hello world在那邊?背景說明
Hello world在那邊?背景說明Wen Liao
 
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介Wen Liao
 
GNU AS簡介
GNU AS簡介GNU AS簡介
GNU AS簡介Wen Liao
 
UPnP 1.0 簡介
UPnP 1.0 簡介UPnP 1.0 簡介
UPnP 1.0 簡介Wen Liao
 
GNU ld的linker script簡介
GNU ld的linker script簡介GNU ld的linker script簡介
GNU ld的linker script簡介Wen Liao
 
從組裝軟體中談談軟體發展管理
從組裝軟體中談談軟體發展管理從組裝軟體中談談軟體發展管理
從組裝軟體中談談軟體發展管理Wen Liao
 
軟體組裝心得分享
軟體組裝心得分享軟體組裝心得分享
軟體組裝心得分享Wen Liao
 
MLDM Monday -- Optimization Series Talk
MLDM Monday -- Optimization Series TalkMLDM Monday -- Optimization Series Talk
MLDM Monday -- Optimization Series TalkJerry Wu
 

Destacado (18)

PyConAPAC2014 BoF Introduction
PyConAPAC2014 BoF IntroductionPyConAPAC2014 BoF Introduction
PyConAPAC2014 BoF Introduction
 
Testing in Python @ Kaosiung.py 2014.05.26
Testing in Python @ Kaosiung.py 2014.05.26Testing in Python @ Kaosiung.py 2014.05.26
Testing in Python @ Kaosiung.py 2014.05.26
 
當資訊科技撞上臺南牛肉湯的那些事兒 @ 2014 NCKU EDiT 科技啓蒙
當資訊科技撞上臺南牛肉湯的那些事兒 @ 2014 NCKU EDiT 科技啓蒙當資訊科技撞上臺南牛肉湯的那些事兒 @ 2014 NCKU EDiT 科技啓蒙
當資訊科技撞上臺南牛肉湯的那些事兒 @ 2014 NCKU EDiT 科技啓蒙
 
Tip for Editors
Tip for EditorsTip for Editors
Tip for Editors
 
PyConAPAC2014 Tainan.py 介紹
PyConAPAC2014 Tainan.py 介紹PyConAPAC2014 Tainan.py 介紹
PyConAPAC2014 Tainan.py 介紹
 
Rubypython - 用 ruby 愛上 python
Rubypython - 用 ruby 愛上 pythonRubypython - 用 ruby 愛上 python
Rubypython - 用 ruby 愛上 python
 
Guide to GStreamer Application Development Manual: CH1 to CH10
Guide to GStreamer Application Development Manual: CH1 to CH10Guide to GStreamer Application Development Manual: CH1 to CH10
Guide to GStreamer Application Development Manual: CH1 to CH10
 
GNU gettext簡介 - 以C語言為範例
GNU gettext簡介 - 以C語言為範例GNU gettext簡介 - 以C語言為範例
GNU gettext簡介 - 以C語言為範例
 
故事:自由軟體和 Richard Stallman
故事:自由軟體和 Richard Stallman故事:自由軟體和 Richard Stallman
故事:自由軟體和 Richard Stallman
 
開放街圖 自助旅行的好幫手
開放街圖   自助旅行的好幫手開放街圖   自助旅行的好幫手
開放街圖 自助旅行的好幫手
 
Hello world在那邊?背景說明
Hello world在那邊?背景說明Hello world在那邊?背景說明
Hello world在那邊?背景說明
 
GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介GNU Make, Autotools, CMake 簡介
GNU Make, Autotools, CMake 簡介
 
GNU AS簡介
GNU AS簡介GNU AS簡介
GNU AS簡介
 
UPnP 1.0 簡介
UPnP 1.0 簡介UPnP 1.0 簡介
UPnP 1.0 簡介
 
GNU ld的linker script簡介
GNU ld的linker script簡介GNU ld的linker script簡介
GNU ld的linker script簡介
 
從組裝軟體中談談軟體發展管理
從組裝軟體中談談軟體發展管理從組裝軟體中談談軟體發展管理
從組裝軟體中談談軟體發展管理
 
軟體組裝心得分享
軟體組裝心得分享軟體組裝心得分享
軟體組裝心得分享
 
MLDM Monday -- Optimization Series Talk
MLDM Monday -- Optimization Series TalkMLDM Monday -- Optimization Series Talk
MLDM Monday -- Optimization Series Talk
 

Similar a Testing in python 2.7.3

PHPUnit slide formal
PHPUnit slide formalPHPUnit slide formal
PHPUnit slide formaljameslabs
 
Java script测试之js unit ut
Java script测试之js unit utJava script测试之js unit ut
Java script测试之js unit utfangdeng
 
Foundation of software development 1
Foundation of software development 1Foundation of software development 1
Foundation of software development 1netdbncku
 
2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發AgileCommunity
 
使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)Max Lai
 
Junit使用指南及作业规范
Junit使用指南及作业规范Junit使用指南及作业规范
Junit使用指南及作业规范dong jiang
 
Java单元测试
Java单元测试Java单元测试
Java单元测试darlingshan
 
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013Hazem Saleh
 
Part04 软件测试方法论
Part04 软件测试方法论Part04 软件测试方法论
Part04 软件测试方法论aellaw
 
嵌入式測試驅動開發
嵌入式測試驅動開發嵌入式測試驅動開發
嵌入式測試驅動開發hugo lu
 
常见软件测试术语集锦
常见软件测试术语集锦常见软件测试术语集锦
常见软件测试术语集锦ben00570
 
Introduction to software quality assurance and its implementation
Introduction to software quality assurance and its implementationIntroduction to software quality assurance and its implementation
Introduction to software quality assurance and its implementationYung-Chun Chang
 
Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)Fong Liou
 
Hands on data analysis 101
Hands on data analysis 101Hands on data analysis 101
Hands on data analysis 101FEG
 

Similar a Testing in python 2.7.3 (20)

PHPUnit
PHPUnitPHPUnit
PHPUnit
 
PHPUnit slide formal
PHPUnit slide formalPHPUnit slide formal
PHPUnit slide formal
 
Java script测试之js unit ut
Java script测试之js unit utJava script测试之js unit ut
Java script测试之js unit ut
 
Foundation of software development 1
Foundation of software development 1Foundation of software development 1
Foundation of software development 1
 
2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發
 
使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)
 
Junit使用指南及作业规范
Junit使用指南及作业规范Junit使用指南及作业规范
Junit使用指南及作业规范
 
Java单元测试
Java单元测试Java单元测试
Java单元测试
 
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
 
Python系列1
Python系列1Python系列1
Python系列1
 
Part04 软件测试方法论
Part04 软件测试方法论Part04 软件测试方法论
Part04 软件测试方法论
 
Sun java
Sun javaSun java
Sun java
 
嵌入式測試驅動開發
嵌入式測試驅動開發嵌入式測試驅動開發
嵌入式測試驅動開發
 
PHP 单元测试
PHP 单元测试PHP 单元测试
PHP 单元测试
 
常见软件测试术语集锦
常见软件测试术语集锦常见软件测试术语集锦
常见软件测试术语集锦
 
單元測試
單元測試單元測試
單元測試
 
jasmine入门指南
jasmine入门指南jasmine入门指南
jasmine入门指南
 
Introduction to software quality assurance and its implementation
Introduction to software quality assurance and its implementationIntroduction to software quality assurance and its implementation
Introduction to software quality assurance and its implementation
 
Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)
 
Hands on data analysis 101
Hands on data analysis 101Hands on data analysis 101
Hands on data analysis 101
 

Más de Wen Liao

Hello world 的一生
Hello world 的一生Hello world 的一生
Hello world 的一生Wen Liao
 
Notes on oracle solaris 11.3 linkers and libraries guide chapter one
Notes on oracle solaris 11.3 linkers  and libraries guide  chapter oneNotes on oracle solaris 11.3 linkers  and libraries guide  chapter one
Notes on oracle solaris 11.3 linkers and libraries guide chapter oneWen Liao
 
A successful git branching model 導讀
A successful git branching model 導讀A successful git branching model 導讀
A successful git branching model 導讀Wen Liao
 
Trace 程式碼之皮
Trace 程式碼之皮Trace 程式碼之皮
Trace 程式碼之皮Wen Liao
 
淺談Debian套件打包
淺談Debian套件打包淺談Debian套件打包
淺談Debian套件打包Wen Liao
 
Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command lineWen Liao
 

Más de Wen Liao (6)

Hello world 的一生
Hello world 的一生Hello world 的一生
Hello world 的一生
 
Notes on oracle solaris 11.3 linkers and libraries guide chapter one
Notes on oracle solaris 11.3 linkers  and libraries guide  chapter oneNotes on oracle solaris 11.3 linkers  and libraries guide  chapter one
Notes on oracle solaris 11.3 linkers and libraries guide chapter one
 
A successful git branching model 導讀
A successful git branching model 導讀A successful git branching model 導讀
A successful git branching model 導讀
 
Trace 程式碼之皮
Trace 程式碼之皮Trace 程式碼之皮
Trace 程式碼之皮
 
淺談Debian套件打包
淺談Debian套件打包淺談Debian套件打包
淺談Debian套件打包
 
Introduce to Linux command line
Introduce to Linux command lineIntroduce to Linux command line
Introduce to Linux command line
 

Testing in python 2.7.3