SlideShare una empresa de Scribd logo
1 de 48
Descargar para leer sin conexión
pytest 還有他的 
快樂夥伴 
魯蛇喬 @ Taipei.py 2014.08
⼀一般來講 
「快樂夥伴」是不重要 
的配⾓角們的簡稱 …
今天相反 
他們是值得學習的
等等會聊到 … 
• JUnit 
• MiniTest 
• PyUnit (a.k.a. unittest) 
• pytest
先假設你看得懂下述魔法:
年紀最⼤大的快樂夥伴: 
JUnit 
早就變成潮男(⼥女)了!
起源 
Kent Beck Erich Gamma
最新版的 JUnit
看⼀一下現在怎麼⽤用 JUnit
⼤大量使⽤用 @ annotation 
Output:
甚⾄至有 @Rule 可取代 
before, after ..
「現在正在進⾏行 Python 聚會。」 
–肯定句
但是 JUnit 還有好多功能 … 
@theory … 與測試環境/IDE的整合 …
潮男無誤 
我就是強!
最閃亮的快樂夥伴: 
MiniTest 
朝全⽅方位藝⼈人發展
為什麼選他? 
1. ⼀一定程度相容 RSpec 
2. Ruby 1.9.x bundles minitest not Test::Unit.
有何特⾊色? 
Unit tests Specs
還有? 
Mock 
Benchmark
全⽅方位發展 
點到為⽌止、我最簡單
最親密的快樂夥伴: 
PyUnit 
也就是 Python STD 裡⾯面的 unittest
依循著 JUnit 早期的脈絡發展 
• 以 3.4.1 為例,基本款功能有: 
• hooks: setUp/tearDown … 
• test discovery 機制 
• @unittest.skip /skipif / skipUnless 機制 
• 進階款功能: 
• subtests 
• 內建 mock
宅男無誤 
不潮、不多功能
⼀一起想⼀一下: 
少了什麼? 
潮不潮不重要,重點是能夠更好嗎?
pytest 
主⾓角登場: 
打得贏 除了unittest以外的 凶殘夥伴嗎?
sure 
under certain condition
No Boilerplate 
1. 
4. 
5. 
3. 
2.
Assertion: No API & More Pythonic
伊⼈人何處尋 
setUp/tearDown … ?
新⼈人換舊⼈人 
改⽤用 fixture 系統,擁抱 dependency 
injection
破除不顯著的時序耦合 
我看 報紙才 參數列就知道發⽣生什麼事! 
1. 
4. 
3. 
2. 
#很像 JUnit 的 @Rule 對吧!
可控 fixture 的 scope
清理 fixture
可控 fixture 的 scope
以 fixture 注⼊入另⼀一個 fixture
依照參數⽣生成各種 fixture 
且⾃自動注⼊入測試
fixture x fixture
加強版 setUp …
mark 系統
plugins
tl;r 
請參閱官網 
# 或之前在 Tainan.py 分享的 slides
其實 
⾃自從有了 pytest 後 
⽤用 Python 寫 tests 可以更快樂
結論 
主⾓角打不打得過快樂夥伴不重要 … 
# 是否從此對 Testing Framework 更有想法?
assert QA.time == 0 
True
⼯工商服務:台南.py 
# 歡迎路過 Tainan.py !
下次聚會 
九⽉月⼆二⼗十七⽇日 (六) 
# 歡迎路過 Tainan.py !
歡迎投稿 
感謝聆聽 
# 歡迎路過 Tainan.py !

Más contenido relacionado

Más de Chun-Yu Tseng

Build and Host Real-world Machine Learning Services from Scratch @ pycontw2019
Build and Host Real-world Machine Learning Services from Scratch @ pycontw2019 Build and Host Real-world Machine Learning Services from Scratch @ pycontw2019
Build and Host Real-world Machine Learning Services from Scratch @ pycontw2019 Chun-Yu Tseng
 
from ai.backend import python @ pycontw2018
from ai.backend import python @ pycontw2018from ai.backend import python @ pycontw2018
from ai.backend import python @ pycontw2018Chun-Yu Tseng
 
Essential TDD @ pycontw2016
Essential TDD @ pycontw2016Essential TDD @ pycontw2016
Essential TDD @ pycontw2016Chun-Yu Tseng
 
從蟒蛇到神龍 - 從 1 接關繼續打造爬蟲程式
從蟒蛇到神龍 - 從 1 接關繼續打造爬蟲程式從蟒蛇到神龍 - 從 1 接關繼續打造爬蟲程式
從蟒蛇到神龍 - 從 1 接關繼續打造爬蟲程式Chun-Yu Tseng
 
Agile x API x Documentation @ NGO [[MOPCON2015]]
Agile x API x Documentation @ NGO [[MOPCON2015]]Agile x API x Documentation @ NGO [[MOPCON2015]]
Agile x API x Documentation @ NGO [[MOPCON2015]]Chun-Yu Tseng
 
快快樂樂成為 Coding Ninja (by pytest) @ PyConAPAC2015
快快樂樂成為 Coding Ninja (by pytest) @ PyConAPAC2015快快樂樂成為 Coding Ninja (by pytest) @ PyConAPAC2015
快快樂樂成為 Coding Ninja (by pytest) @ PyConAPAC2015Chun-Yu Tseng
 
程式 x 設計 @ MOPCON 2014
程式 x 設計 @ MOPCON 2014程式 x 設計 @ MOPCON 2014
程式 x 設計 @ MOPCON 2014Chun-Yu Tseng
 
OO x Python @ Tainan.py x MOSUT x FP 2014.09.27
OO x Python  @ Tainan.py x MOSUT x FP 2014.09.27OO x Python  @ Tainan.py x MOSUT x FP 2014.09.27
OO x Python @ Tainan.py x MOSUT x FP 2014.09.27Chun-Yu Tseng
 
用路人小幫手 x 回報大改造 @ 2014 台南黑客松
用路人小幫手 x 回報大改造 @ 2014 台南黑客松用路人小幫手 x 回報大改造 @ 2014 台南黑客松
用路人小幫手 x 回報大改造 @ 2014 台南黑客松Chun-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
 
PyConAPAC2014 BoF Introduction
PyConAPAC2014 BoF IntroductionPyConAPAC2014 BoF Introduction
PyConAPAC2014 BoF IntroductionChun-Yu Tseng
 
PyConAPAC2014 Tainan.py 介紹
PyConAPAC2014 Tainan.py 介紹PyConAPAC2014 Tainan.py 介紹
PyConAPAC2014 Tainan.py 介紹Chun-Yu Tseng
 
暖場 @ Tainan.py 2013.11.30
暖場 @ Tainan.py 2013.11.30 暖場 @ Tainan.py 2013.11.30
暖場 @ Tainan.py 2013.11.30 Chun-Yu Tseng
 
API 文件大亂鬥 @ MOPCON 2013
API 文件大亂鬥 @ MOPCON 2013API 文件大亂鬥 @ MOPCON 2013
API 文件大亂鬥 @ MOPCON 2013Chun-Yu Tseng
 
暖場與 Web 相關的小玩具 @ Tainan.py 2013.09.28
暖場與 Web 相關的小玩具 @ Tainan.py 2013.09.28暖場與 Web 相關的小玩具 @ Tainan.py 2013.09.28
暖場與 Web 相關的小玩具 @ Tainan.py 2013.09.28Chun-Yu Tseng
 

Más de Chun-Yu Tseng (17)

Build and Host Real-world Machine Learning Services from Scratch @ pycontw2019
Build and Host Real-world Machine Learning Services from Scratch @ pycontw2019 Build and Host Real-world Machine Learning Services from Scratch @ pycontw2019
Build and Host Real-world Machine Learning Services from Scratch @ pycontw2019
 
from ai.backend import python @ pycontw2018
from ai.backend import python @ pycontw2018from ai.backend import python @ pycontw2018
from ai.backend import python @ pycontw2018
 
導讀 Clean Code
導讀 Clean Code導讀 Clean Code
導讀 Clean Code
 
Essential TDD @ pycontw2016
Essential TDD @ pycontw2016Essential TDD @ pycontw2016
Essential TDD @ pycontw2016
 
從蟒蛇到神龍 - 從 1 接關繼續打造爬蟲程式
從蟒蛇到神龍 - 從 1 接關繼續打造爬蟲程式從蟒蛇到神龍 - 從 1 接關繼續打造爬蟲程式
從蟒蛇到神龍 - 從 1 接關繼續打造爬蟲程式
 
Agile x API x Documentation @ NGO [[MOPCON2015]]
Agile x API x Documentation @ NGO [[MOPCON2015]]Agile x API x Documentation @ NGO [[MOPCON2015]]
Agile x API x Documentation @ NGO [[MOPCON2015]]
 
快快樂樂成為 Coding Ninja (by pytest) @ PyConAPAC2015
快快樂樂成為 Coding Ninja (by pytest) @ PyConAPAC2015快快樂樂成為 Coding Ninja (by pytest) @ PyConAPAC2015
快快樂樂成為 Coding Ninja (by pytest) @ PyConAPAC2015
 
程式 x 設計 @ MOPCON 2014
程式 x 設計 @ MOPCON 2014程式 x 設計 @ MOPCON 2014
程式 x 設計 @ MOPCON 2014
 
OO x Python @ Tainan.py x MOSUT x FP 2014.09.27
OO x Python  @ Tainan.py x MOSUT x FP 2014.09.27OO x Python  @ Tainan.py x MOSUT x FP 2014.09.27
OO x Python @ Tainan.py x MOSUT x FP 2014.09.27
 
用路人小幫手 x 回報大改造 @ 2014 台南黑客松
用路人小幫手 x 回報大改造 @ 2014 台南黑客松用路人小幫手 x 回報大改造 @ 2014 台南黑客松
用路人小幫手 x 回報大改造 @ 2014 台南黑客松
 
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
 
Tip for Editors
Tip for EditorsTip for Editors
Tip for Editors
 
PyConAPAC2014 BoF Introduction
PyConAPAC2014 BoF IntroductionPyConAPAC2014 BoF Introduction
PyConAPAC2014 BoF Introduction
 
PyConAPAC2014 Tainan.py 介紹
PyConAPAC2014 Tainan.py 介紹PyConAPAC2014 Tainan.py 介紹
PyConAPAC2014 Tainan.py 介紹
 
暖場 @ Tainan.py 2013.11.30
暖場 @ Tainan.py 2013.11.30 暖場 @ Tainan.py 2013.11.30
暖場 @ Tainan.py 2013.11.30
 
API 文件大亂鬥 @ MOPCON 2013
API 文件大亂鬥 @ MOPCON 2013API 文件大亂鬥 @ MOPCON 2013
API 文件大亂鬥 @ MOPCON 2013
 
暖場與 Web 相關的小玩具 @ Tainan.py 2013.09.28
暖場與 Web 相關的小玩具 @ Tainan.py 2013.09.28暖場與 Web 相關的小玩具 @ Tainan.py 2013.09.28
暖場與 Web 相關的小玩具 @ Tainan.py 2013.09.28
 

Pytest 還有他的快樂夥伴