Test Driven Python for Dummies
Author: | Fred Lin (gasolin) |
---|---|
Date: | 2007-04-14 |
email: | gasolin@gmail.com |
blog: | inet6.blogspot.com |
wiicode: | 3692-8229-2743-3621 |
- gsquickstart
- tgcrud
- TemplateBrowser
- TurboDjango
- TurboJinja
- jquery
- PlotKit
- tg2exe
- tgmigrate
什麼是懶人包?
-> web 2.0 講的 Mashup 概念?
put related things together
在這個 session 中你可以學到
- 測試的概念.
- Test Driven programming 簡介.
- 動態語言(Python)如何作測試.
- 實際撰寫 Console 和 web 程式.
http://wiki.python.org.tw/ http://groups.google.com/group/pythontw
懶人用的測試概念: 測試既不麻煩也不用特地學, 寫 Test Case 和寫一般函式沒兩樣. 程式註釋裡的範例片段也可以拿來當 Test Case
- 邊測邊學 Python 語言
- 簡單單元測試: 使用 Nose 測試工具
- 用寫文件的方式寫測試: 使用 DocTest
- 寫個 console 程式
- 寫個網頁程式: 使用 TurboGears 框架
選擇適合自己的工具
How often the test gets in your way?
- How well the test supports
- Writing good programs?
- Writing maintainable programs?
也可以順便問問自己:
How often the language gets in your way.
- How well the language supports
- Writing good programs?
- Writing maintainable programs?
- 什麼是測試?
- 測試是為了什麼?
- 為什麼要測試?
測試是探索世界的方式
學習測試時遇到什麼問題?
書本是簡化的知識,
要回答這些問題之前...得先瞭解
測試是探索世界的方式
廣義,
什麼是測試?
測試像是摸著石頭過河, 測來安心的
走河床可能只走一次, 軟工則是不停地重複過河的過程.
走在遍佈碎石的河床或山間, 走一步, 頓一下, 伸出腳踏踏前方石頭, 確定穩了再走.
我們自出生以來,所有經驗都明白告訴我們時間以恆定速率流逝, 然而這個信念並不真。
我們已經知道:我們以常識理解這個世界,不免會犯錯。
-- 愛因斯坦
理論物理學家
愛因斯坦推翻了流行了幾個世紀的想法:在科學中,實證研究與經驗並不佔首要地位。
什麼是測試?
- 測試可以讓你事先思考
- 少做白工
什麼是測試?
- 測試可以事先警告你
- 減少錯誤
什麼是測試?
測試是為寫文件
- 友好文件可吸引開發者
- 單元測試數量
- 測試覆蓋比率
- 公信力
OSS 受歡迎/吸引開發者的專案原因是有友好文件
- Test for savety
- Test for report
- Test for maintainance
- Test for better document
什麼是懶人測試?
- 自動測試
- 好維護
- 減少錯誤
- 單元測試
- 功能測試
- 應用測試(UI, smoke)
Jason “Selenium” Huggins
軟體開發階段與工具
- 規劃 (research, doc, mind map)
- 設計 (UML)
- 編寫 (c/java, php, python)
- 測試 (? ? ?)
大部分有寫過軟體經驗的人都可以明瞭, 但其中還是有些新/難以理解的概念存在
很難說明的概念/工具排行榜
- CMMI (who care?)
- 函數編程
- 版本控制工具
- 測試驅動
它們為什麼對你有用?
- 用像在寫數學方程式那樣的方式寫程式
小遊戲, 演算法
- 有問題了可以回頭
- 方便追蹤修改
- 方便替換程式碼
- 測試金字塔
- 單元測試 大量
- 功能測試 中等
- 應用測試 適量
承平時, 不拿也不會死
戰時......
- 概念
- 工具
- 網頁開發
概念:
- GTD (Getting Things Done)
- 測試開發
工具:
- 版本控制工具
- RST 結構化文本
- 心智圖
網頁開發:
- 一門語言
- MVC
- ORM
- javascript library
亂入: Python 語言的測試驅動工具....
頗為不少, 有人列了表:
仔細看看下面列表中少了什麼?
什麼是測試驅動開發?
- 地城冒險 Dungeon Quest
- 動作解謎冒險 Legend of Zelda
- 都喜歡
你喜歡哪種形式的冒險呢?
測試驅動開發
- short cycle repeated many times
- write a test
- watch it fail
- make it compile
- make it pass
- refactor the code
- refactor the test (and elaborate)
- rinse and repeat
from Kevin Dahlhausen's presentation
stop point 1
- 有互動介面 Interactive interpreter
- 邊寫可以邊測試
我怎麼相信這些例子是真實的?
-> 測試
- Python 語言
- 實例
- nose 測試工具
- doctest
It fits in your brain
print "hello world!"
a.k.a: What's the Difference in Python?
c-like, dynamic, strong type, battery included, interactive, readable, procedure, object-oriented, functional, language
educational, production
Type less and keep less syntax in mind
That you don't need to consult documentation every time you've spent time away from it and want to do basic tasks like reading a file, or doing some text manipulation.
>>> someNumber = 1
>>> isinstance(someNumber, object)
True
- list, tuple, set
- dict
- Python is freely available.
- Python is platform-independent.
- Python is easy to learn.
- Python is powerful.
- Python is extensible.
- The Python concepts transfer well to other languages.
先來看一個簡單的函式
Give some input(optional), check it's output
def function_name(input1, input2):
"""do something"""
return output
def test_function_name():
output = function_name(input_sample1, input_sample2)
assert 'output sample' in output
assert 'output sample' == output
assert type(output) == string
message = "Hello world"
def show():
return message
def test_show():
assert 'Hello world' in show()
# nosetests teashop.py
.
----------------------------------
Ran 1 test in 0.010s
OK
message = "Hello world"
def show():
"""
>>> show()
'Hello world'
"""
return message
# nosetests teashop.py --with-doctest
.
------------------------------------------
Ran 1 test in 0.020s
OK
- Test should never do anything that would cause other tests to fail
自動測試工具的特性
If it looks like a test, it's a test: duck typing
- Test Driven Development in Python - Kevin Dahlhausen
- http://matt-good.net/2007/02/28/pycon-trac-presentation/
- 簡介 http://share.zoomquiet.org/classes/classes2007/070322-introPy/
- Google pythontw Group http://groups.google.com/group/pythontw
- www.python.org.tw
End