Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Pythonic tests with Py.test
Search
Renato dos Santos Oliveira
July 08, 2015
Technology
190
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
Transcript
29 slides
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
More Decks by Renato dos Santos Oliveira
See All by Renato dos Santos Oliveira
Django Views: Boas Práticas
renatooliveira
0
330
Testes pythonicos com Py.test
renatooliveira
0
200
Refactoring Django Applications (pt-BR)
renatooliveira
2
130
Django Migrations v1.0
renatooliveira
2
96
Other Decks in Technology
See All in Technology
AWS Blocksで AIエージェント作れるの知ってます?
kmiya84377
1
170
AWS Well-Arhitected Frameworkセキュリティの柱 について
miu_crescent
PRO
2
260
メルカリの小規模チーム「Pod」で、 エンジニアがデータアナリストなしで、 AIを使ってデータ分析の自走を目指す取り組み
jackojacko_
1
360
攻撃と守りは何が変わり、何が変わらないのか~最近の不正アクセスから考える、リスク評価と標準の使いどころ
oidfj
2
360
統制されたID・権限をどう利用するか : DPoP/FAPIによるAPIセキュリティ強化
oidfj
2
330
フルTypeScript × AI駆動開発で開発量が数倍に (JSConf JP 2026 スポンサーLT:NCDC株式会社)
keitaibaraki
0
330
Taking back control of your AI development
inesmontani
PRO
0
320
OpenTelemetryで変わった 新時代のCloudWatchメトリクス監視
tsukuboshi
0
510
IVRy における AI 時代のデータプロダクトと事業開発
ivry_presentationmaterials
0
120
目を覚ませ!僕らの認証情報が何者かに侵略されてるぞ
tkito
2
490
社内アプリ基盤「Quartz」による、 非エンジニアとAIに本番デプロイを 任せても壊れないプラットフォームの設計
staka121
PRO
1
240
AWS DevOps Agentの構造から読み解く運用 AI エージェントの設計原則/aws-devops-agent-design-principles
tomoki10
0
300
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
85
9.8k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
320
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.3k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
490
The Curse of the Amulet
leimatthew05
4
17k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
680
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
2
450
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
640
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
790
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
590
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
610