Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Pythonic tests with Py.test
Search
Renato dos Santos Oliveira
July 08, 2015
Technology
0
160
Pythonic tests with Py.test
Talk presented at Geek Night Recife @ ThoughtWorks
Renato dos Santos Oliveira
July 08, 2015
Tweet
Share
More Decks by Renato dos Santos Oliveira
See All by Renato dos Santos Oliveira
Django Views: Boas Práticas
renatooliveira
0
310
Testes pythonicos com Py.test
renatooliveira
0
180
Refactoring Django Applications (pt-BR)
renatooliveira
2
110
Django Migrations v1.0
renatooliveira
2
77
Other Decks in Technology
See All in Technology
Iceberg Meetup Japan #1 : Iceberg and Databricks
databricksjapan
0
370
RayでPHPのデバッグをちょっと快適にする
muno92
PRO
0
190
依存パッケージの更新はコツコツが勝つコツ! / phpcon_nagoya2025
blue_goheimochi
3
220
【詳説】コンテンツ配信 システムの複数機能 基盤への拡張
hatena
0
260
クラウド関連のインシデントケースを収集して見えてきたもの
lhazy
8
1.1k
AWS Well-Architected Frameworkで学ぶAmazon ECSのセキュリティ対策
umekou
2
150
OSS構成管理ツールCMDBuildを使ったAWSリソース管理の自動化
satorufunai
0
650
急成長する企業で作った、エンジニアが輝ける制度/ 20250227 Rinto Ikenoue
shift_evolve
0
130
アジャイルな開発チームでテスト戦略の話は誰がする? / Who Talks About Test Strategy?
ak1210
1
600
Amazon Q Developerの無料利用枠を使い倒してHello worldを表示させよう!
nrinetcom
PRO
2
120
Amazon Aurora のバージョンアップ手法について
smt7174
2
150
生成AI×財務経理:PoCで挑むSlack AI Bot開発と現場巻き込みのリアル
pohdccoe
1
730
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Optimizing for Happiness
mojombo
376
70k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
114
50k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
A better future with KSS
kneath
238
17k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
We Have a Design System, Now What?
morganepeng
51
7.4k
Thoughts on Productivity
jonyablonski
69
4.5k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Transcript
Pythonic tests with py.test Renato Oliveira
Who?! ❏ Renato Oliveira ❏ Labcodes Software Studio ❏ Django
Software Foundation ❏ Recently migrated to Py.test
Sumary ❏ Introduction ❏ Basic Usage ❏ Test discovery ❏
Markers ❏ Fixtures ❏ Plugins
FIX IN PROD
Tests in prod
Tests in prod
Introduction “Pytest is a mature full featured Python testing tool
that helps you to write better programs.”
Introduction - Assert with assert statement def sum(a, b): return
a + b def test_sum(): assert sum(1, 1) == 2
Basic usage - Running - Failure report
Test discovery and runner Simply run your tests with $
py.test
Test discovery and runner Verbosely $ py.test -v
Test discovery and runner Pytest recursively searches your folders looking
for test files test_*.py or *_test.py
Test discovery and runner You can test just a file
$ py.test test_foo.py
Test discovery and runner You can also execute just one
test $ py.test -k test_foo
Migrating Unittest code Code before import unittest def to_upper(text): return
text.upper() class TestToUpper(unittest.TestCase): def test_to_upper(self): self.assertEqual(to_upper("pug"), "PUG")
Migrating Unittest code Code after def to_upper(text): return text.upper() def
test_to_upper(): return to_upper("pug") == "PUG"
Markers Gives you the ability to add some extra metadata
to your tests. ❏ skipif ❏ xfail ❏ parametrize ❏ your owns
skipif @pytest.mark.skipif(sys.version_info < (3, 3), reason="requires python3.3") def test_function(): assert
1 == 1
xfail @pytest.mark.xfail(reason="1 is not 2") def test_fail(): assert 1 ==
2
parametrize @pytest.mark.parametrize("input,expected", [ ("3+5", 8), ("2+4", 6), pytest.mark.xfail(("6*9", 42)), ])
def test_eval(input, expected): assert eval(input) == expected
slow test, fast test @pytest.mark.slow def test_slow(): assert 1 ==
1 @pytest.mark.fast def test_fast(): assert 1 == 1
Fixtures The purpose of test fixtures is to provide a
fixed baseline upon which tests can reliably and repeatedly execute.
Fixtures Pytest fixtures allows you to decouple your test suite
from the context you want them to run.
Fixtures @pytest.fixture() def answer(): return 42 def test_the_ultimate_question_about_life_the_universe_and_everything(answer): assert answer
== 42
@pytest.fixture() def webdriver(request): driver = Firefox() request.addfinalizer(driver.quit) return driver def
test_pug_website_title(webdriver): webdriver.get("http://pycon.pug.pe/XXXVIII/") assert "Encontro do PUG-PE" in webdriver.title def test_python_website_title(webdriver): webdriver.get("http://python.org/") assert "Python" in webdriver.title
Share fixtures - pytest.mark.usefixtures - conftest.py - “usefixtures” param in
pytest.ini
Useful plugins - pytest-django - pytest-mock - http://pytest. org/latest/plugins_index/index.html
Questions?
Obrigado! :)
[email protected]
@_renatooliveira github/renatooliveira www.labcodes.com.br