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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Renato dos Santos Oliveira
July 08, 2015
Technology
180
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Pythonic tests with Py.test
Talk presented at Geek Night Recife @ ThoughtWorks
Renato dos Santos Oliveira
July 08, 2015
More Decks by Renato dos Santos Oliveira
See All by Renato dos Santos Oliveira
Django Views: Boas Práticas
renatooliveira
0
320
Testes pythonicos com Py.test
renatooliveira
0
190
Refactoring Django Applications (pt-BR)
renatooliveira
2
120
Django Migrations v1.0
renatooliveira
2
85
Other Decks in Technology
See All in Technology
AIソロプレナー時代に2ヶ月で20人増員した事業創造会社の開発組織の話
miyatakoji
0
640
データサイエンスを価値につなげるプロジェクト設計 〜 DS一年目が現場で得た気づき 〜
ysd113
1
220
連合学習と機密コンピューティング
lycorptech_jp
PRO
0
110
作って終わりにしない タイミーのセマンティックレイヤー育成の現在地
chanyou0311
4
2.3k
失敗を資産に変えるClaude Code
shinyasaita
0
610
RAG を使わないという選択肢
tatsutaka
1
220
非エンジニアがClaudeと挑んだ「1ヶ月間プロダクト30本ノック」
askokc
0
420
2026TECHFRESH畢業分享會 - AI 時代的人生存檔點
line_developers_tw
PRO
0
930
AIっぽい文章を採点して人間らしく直すアプリを作ってみた
yama3133
2
140
Claude Code の Sandbox 機能を Anthropic Sandbox Runtime(srt) で試そう!/lets-play-anthropic-sandbox-runtime
tomoki10
1
560
2026TECHFRESH畢業分享會 - 原生還是跨平台? App 開發踩坑實錄
line_developers_tw
PRO
0
930
新しいVibe Codingと”自走”について
watany
6
310
Featured
See All Featured
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
23k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
140
Faster Mobile Websites
deanohume
310
31k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Visualization
eitanlees
152
17k
Balancing Empowerment & Direction
lara
6
1.2k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
360
The agentic SEO stack - context over prompts
schlessera
0
820
Optimising Largest Contentful Paint
csswizardry
37
3.7k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
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