Test Driven Python 懶人包

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
python-logo.gif

關於我 Mii

2733.jpg

http://limodou.51boo.com/book/8/

我的 OSS 專案

  • gsquickstart
  • tgcrud
  • TemplateBrowser
  • TurboDjango
  • TurboJinja
  • jquery
  • PlotKit
  • tg2exe
  • tgmigrate

http://www.python.org/pypi

Test Driven Python 懶人包

什麼是懶人包?

-> 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 框架

Questions for Test

選擇適合自己的工具

  • How often the test gets in your way?

  • How well the test supports
    • Writing good programs?
    • Writing maintainable programs?

Questions for Language

也可以順便問問自己:

  • How often the language gets in your way.

  • How well the language supports
    • Writing good programs?
    • Writing maintainable programs?

進入主題

miapoint.png

測試的概念

  • 什麼是測試?
  • 測試是為了什麼?
  • 為什麼要測試?

測試是探索世界的方式

測試的迷思

matta.png

測試的迷思

學習測試時遇到什麼問題?

書本是簡化的知識,

要回答這些問題之前...得先瞭解

測試是什麼

測試是探索世界的方式

廣義,

測試的概念

什麼是測試?

測試像是摸著石頭過河, 測來安心的

走河床可能只走一次, 軟工則是不停地重複過河的過程.

走在遍佈碎石的河床或山間, 走一步, 頓一下, 伸出腳踏踏前方石頭, 確定穩了再走.

讓理論物理學家告訴你

  • 我們自出生以來,所有經驗都明白告訴我們時間以恆定速率流逝, 然而這個信念並不真。

  • 我們已經知道:我們以常識理解這個世界,不免會犯錯。

    -- 愛因斯坦

理論物理學家

愛因斯坦推翻了流行了幾個世紀的想法:在科學中,實證研究與經驗並不佔首要地位。

測試的概念

什麼是測試?

  • 測試可以讓你事先思考
  • 少做白工

測試的概念

igiari.png

測試的概念

什麼是測試?

  • 測試可以事先警告你
  • 減少錯誤

測試的概念

什麼是測試?

  • 測試是為寫文件

    • 友好文件可吸引開發者
    • 單元測試數量
    • 測試覆蓋比率
    • 公信力

OSS 受歡迎/吸引開發者的專案原因是有友好文件

Cheesecake

http://www.python.org/pypi/Cheesecake/

Conslusion: Test for what?

  • Test for savety
  • Test for report
  • Test for maintainance
  • Test for better document

自動測試

什麼是懶人測試?

  • 自動測試
  • 好維護
  • 減少錯誤

測試金字塔搶鮮看

  • 單元測試
  • 功能測試
  • 應用測試(UI, smoke)
pyramid.gif

Jason “Selenium” Huggins

Develepment Procedure

軟體開發階段與工具

  • 規劃 (research, doc, mind map)
  • 設計 (UML)
  • 編寫 (c/java, php, python)
  • 測試 (? ? ?)

大部分有寫過軟體經驗的人都可以明瞭, 但其中還是有些新/難以理解的概念存在

很難說明的概念

很難說明的概念/工具排行榜

  • CMMI (who care?)
  • 函數編程
  • 版本控制工具
  • 測試驅動

它們為什麼對你有用?

CMMI

richer.jpg

http://blog.xuite.net/big.max/Polo/9890544?p=1~M

函數編程

  • 用像在寫數學方程式那樣的方式寫程式

小遊戲, 演算法

版本控制工具

  • 有問題了可以回頭
  • 方便追蹤修改
  • 方便替換程式碼

測試工具

  • 測試金字塔
    • 單元測試 大量
    • 功能測試 中等
    • 應用測試 適量
pyramid.gif

你應該學習的武器

承平時, 不拿也不會死

戰時......

  • 概念
  • 工具
  • 網頁開發

你應該學習的武器 1

概念:

  • GTD (Getting Things Done)
  • 測試開發

你應該學習的武器 2

工具:

  • 版本控制工具
  • RST 結構化文本
  • 心智圖

你應該學習的武器 3

網頁開發:

  • 一門語言
  • MVC
  • ORM
  • javascript library

Tools for test-driven Python

亂入: Python 語言的測試驅動工具....

頗為不少, 有人列了表:

http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy

懶人測試的邊疆

仔細看看下面列表中少了什麼?

測試驅動開發

什麼是測試驅動開發?

測試驅動開發心理測驗

  • 地城冒險 Dungeon Quest
  • 動作解謎冒險 Legend of Zelda
  • 都喜歡

你喜歡哪種形式的冒險呢?

Test Driven Development

測試驅動開發

  • 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

  • Python 語言
  • 實例
  • nose 測試工具
  • doctest

Who uses Python?

What's the Difference in Python?

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.

EVERYTHING is an object in Python

>>> someNumber = 1

>>> isinstance(someNumber, object)
True

Build-in types

  • list, tuple, set
  • dict

為什麼用 Python

  • 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

怎麼測? test the 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

Lets Go brief: unittest

message = "Hello world"

def show():
    return message

def test_show():
    assert 'Hello world' in show()

Lets Go brief: test result

# nosetests teashop.py
.
----------------------------------
Ran 1 test in 0.010s

OK

Lets Go brief 2: doctest

message = "Hello world"

def show():
    """
    >>> show()
    'Hello world'
    """
    return message

Lets Go brief 2: test result

# 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 Python 懶人包

End