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
Testes pythonicos com Py.test
Search
Renato dos Santos Oliveira
November 11, 2015
Technology
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Testes pythonicos com Py.test
Talk presented at Python Brasil [11] - In portuguese
Renato dos Santos Oliveira
November 11, 2015
More Decks by Renato dos Santos Oliveira
See All by Renato dos Santos Oliveira
Django Views: Boas Práticas
renatooliveira
0
330
Pythonic tests with Py.test
renatooliveira
0
190
Refactoring Django Applications (pt-BR)
renatooliveira
2
120
Django Migrations v1.0
renatooliveira
2
89
Other Decks in Technology
See All in Technology
1万名の社員が使う認証基盤で どう信頼性を担保するか?
kairim0
0
100
10年欲しかった音楽管理アプリを、AIと一緒に作りはじめた
judau
1
200
IR Today: Theory, Practice, and Agents
dtunkelang
0
300
Databricksメトリクスビューはじめてのもくもく会
taka_aki
0
150
20260930_Gemma4_Hands-on
tsho
0
190
大阪オフィスに Unitree Go2 がやってきたので Physical AI やってみた
dafujii
0
200
Execution in the Kingdom of Agents: Reflections on Abstraction and Complexity
bcantrill
0
580
カンファレンスに参加した後の浮遊感とセルフケア
pauli
0
260
猫でもわかるKiro Web
kentapapa
1
160
Meet AgentCore Identity Consent Portal
hironobuiga
2
140
The seven pitfalls of AI (revised version)
ufried
0
160
AIに会社の文脈を理解させる技術~上流工程・非エンジニアにも広げるハーネスエンジニアリング実践~
ochtum
0
200
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
Embracing the Ebb and Flow
colly
88
5.2k
For a Future-Friendly Web
brad_frost
183
10k
Prompt Engineering for Job Search
mfonobong
0
470
Fireside Chat
paigeccino
43
4k
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
520
How to Ace a Technical Interview
jacobian
280
24k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
930
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
550
Unsuck your backbone
ammeep
672
58k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
570
Transcript
Testes Pythonicos com Py. test Renato Oliveira
Oush, quem?! ❏ Renato Oliveira ❏ 4ª Python Brasil ❏
Labcodes Software Studio ❏ Django Software Foundation ❏ Diretor (por mais algumas horas) de Tecnologia da APyB ❏ Organizador da Python Brasil[10]
Sumário ❏ Introdução ❏ Uso básico ❏ Test discovery ❏
Markers ❏ Fixtures ❏ Plugins
FIX IN PROD
Tests in prod
Tests in prod
“Pytest is a mature full featured Python testing tool that
helps you to write better programs.” Introdução
Introduction - Asserts com a palavra reservada assert def sum(a,
b): return a + b def test_sum(): assert sum(1, 1) == 2
Test discovery Simply run your tests with $ py.test
Test discovery Verbosely $ py.test -v
Test discovery Pytest recursivamente procura nas suas pastas por arquivos
de teste. test_*.py or *_test.py
Test discovery Você pode testar só um arquivo $ py.test
test_foo.py
Test discovery Você também pode executar só um teste $
py.test -k test_foo
Migrando código unittest Antes 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 Depois def to_upper(text): return text.upper() def test_to_upper():
return to_upper("pug") == "PUG"
Markers Habilidade de adicionar metadata extra aos seus testes ❏
skipif ❏ xfail ❏ parametrize ❏ você também pode criar
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 @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
Compartilhe fixtures - pytest.mark.usefixtures - conftest.py - “usefixtures” no pytest.ini
Plugins úteis - pytest-django - pytest-mock - http://pytest. org/latest/plugins_index/index.html
Perguntas?
Obrigado! :)
[email protected]
@_renatooliveira github/renatooliveira www.labcodes.com.br