Pytest 還有他的快樂夥伴

48
pytest 還有他的 魯蛇喬 @ Taipei.py 2014.08

description

Taipei.py: http://www.meetup.com/Taipei-py/ MOSUT: https://www.facebook.com/groups/mosut/ Tainan.py: http://www.meetup.com/Tainan-py-Python-Tainan-User-Group/

Transcript of Pytest 還有他的快樂夥伴

Page 1: Pytest 還有他的快樂夥伴

pytest 還有他的快樂夥伴

魯蛇喬 @ Taipei.py 2014.08

Page 2: Pytest 還有他的快樂夥伴

⼀一般來講「快樂夥伴」是不重要的配⾓角們的簡稱 …

Page 3: Pytest 還有他的快樂夥伴

他們是值得學習的今天相反

Page 4: Pytest 還有他的快樂夥伴

等等會聊到 …

• JUnit

• MiniTest

• PyUnit (a.k.a. unittest)

• pytest

Page 5: Pytest 還有他的快樂夥伴

先假設你看得懂下述魔法:

Page 6: Pytest 還有他的快樂夥伴

早就變成潮男(⼥女)了!JUnit

年紀最⼤大的快樂夥伴:

Page 7: Pytest 還有他的快樂夥伴

起源

Kent Beck Erich Gamma

Page 8: Pytest 還有他的快樂夥伴

最新版的 JUnit

Page 9: Pytest 還有他的快樂夥伴

看⼀一下現在怎麼⽤用 JUnit

Page 10: Pytest 還有他的快樂夥伴

⼤大量使⽤用 @ annotation

Output:

Page 11: Pytest 還有他的快樂夥伴

甚⾄至有 @Rule 可取代 before, after ..

Page 12: Pytest 還有他的快樂夥伴
Page 13: Pytest 還有他的快樂夥伴

–肯定句

「現在正在進⾏行 Python 聚會。」

Page 14: Pytest 還有他的快樂夥伴

但是 JUnit 還有好多功能 …

@theory … 與測試環境/IDE的整合 …

Page 15: Pytest 還有他的快樂夥伴

我就是強!

潮男無誤

Page 16: Pytest 還有他的快樂夥伴

朝全⽅方位藝⼈人發展MiniTest

最閃亮的快樂夥伴:

Page 17: Pytest 還有他的快樂夥伴

為什麼選他?

2. Ruby 1.9.x bundles minitest not Test::Unit.1. ⼀一定程度相容 RSpec

Page 18: Pytest 還有他的快樂夥伴

有何特⾊色?

Unit tests Specs

Page 19: Pytest 還有他的快樂夥伴

還有?

Benchmark

Mock

Page 20: Pytest 還有他的快樂夥伴

點到為⽌止、我最簡單

全⽅方位發展

Page 21: Pytest 還有他的快樂夥伴

也就是 Python STD 裡⾯面的 unittestPyUnit

最親密的快樂夥伴:

Page 22: Pytest 還有他的快樂夥伴

依循著 JUnit 早期的脈絡發展• 以 3.4.1 為例,基本款功能有:

• hooks: setUp/tearDown …

• test discovery 機制

• @unittest.skip /skipif / skipUnless 機制 • 進階款功能:

• subtests

• 內建 mock

Page 23: Pytest 還有他的快樂夥伴

不潮、不多功能

宅男無誤

Page 24: Pytest 還有他的快樂夥伴

潮不潮不重要,重點是能夠更好嗎?

少了什麼?

⼀一起想⼀一下:

Page 25: Pytest 還有他的快樂夥伴

打得贏 除了unittest以外的 凶殘夥伴嗎?pytest

主⾓角登場:

Page 26: Pytest 還有他的快樂夥伴

under certain condition

sure

Page 27: Pytest 還有他的快樂夥伴
Page 28: Pytest 還有他的快樂夥伴

No Boilerplate

5.

4.

3.2.1.

Page 29: Pytest 還有他的快樂夥伴

Assertion: No API & More Pythonic

Page 30: Pytest 還有他的快樂夥伴

setUp/tearDown … ?

伊⼈人何處尋

Page 31: Pytest 還有他的快樂夥伴

改⽤用 fixture 系統,擁抱 dependency injection

新⼈人換舊⼈人

Page 32: Pytest 還有他的快樂夥伴

破除不顯著的時序耦合

1.

4.3.

2.

我看 報紙才 參數列就知道發⽣生什麼事!

#很像 JUnit 的 @Rule 對吧!

Page 33: Pytest 還有他的快樂夥伴

可控 fixture 的 scope

Page 34: Pytest 還有他的快樂夥伴

清理 fixture

Page 35: Pytest 還有他的快樂夥伴

可控 fixture 的 scope

Page 36: Pytest 還有他的快樂夥伴

以 fixture 注⼊入另⼀一個 fixture

Page 37: Pytest 還有他的快樂夥伴

依照參數⽣生成各種 fixture 且⾃自動注⼊入測試

Page 38: Pytest 還有他的快樂夥伴

fixture x fixture

Page 39: Pytest 還有他的快樂夥伴

加強版 setUp …

Page 40: Pytest 還有他的快樂夥伴

mark 系統

Page 41: Pytest 還有他的快樂夥伴

plugins

Page 42: Pytest 還有他的快樂夥伴

請參閱官網

tl;r

# 或之前在 Tainan.py 分享的 slides

Page 43: Pytest 還有他的快樂夥伴

⾃自從有了 pytest 後⽤用 Python 寫 tests 可以更快樂

其實

Page 44: Pytest 還有他的快樂夥伴

主⾓角打不打得過快樂夥伴不重要 …

結論

# 是否從此對 Testing Framework 更有想法?

Page 45: Pytest 還有他的快樂夥伴

Trueassert QA.time == 0

Page 46: Pytest 還有他的快樂夥伴

⼯工商服務:台南.py

# 歡迎路過 Tainan.py !

Page 47: Pytest 還有他的快樂夥伴

九⽉月⼆二⼗十七⽇日 (六)

下次聚會

# 歡迎路過 Tainan.py !

Page 48: Pytest 還有他的快樂夥伴

感謝聆聽

歡迎投稿

# 歡迎路過 Tainan.py !