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
Testes pythonicos com Py.test
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Renato dos Santos Oliveira
November 11, 2015
Technology
0
180
Testes pythonicos com Py.test
Talk presented at Python Brasil [11] - In portuguese
Renato dos Santos Oliveira
November 11, 2015
Tweet
Share
More Decks by Renato dos Santos Oliveira
See All by Renato dos Santos Oliveira
Django Views: Boas Práticas
renatooliveira
0
320
Pythonic tests with Py.test
renatooliveira
0
170
Refactoring Django Applications (pt-BR)
renatooliveira
2
110
Django Migrations v1.0
renatooliveira
2
80
Other Decks in Technology
See All in Technology
M&A 後の統合をどう進めるか ─ ナレッジワーク × Poetics が実践した組織とシステムの融合
kworkdev
PRO
1
400
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
3k
Claude_CodeでSEOを最適化する_AI_Ops_Community_Vol.2__マーケティングx_AIはここまで進化した.pdf
riku_423
2
420
変化するコーディングエージェントとの現実的な付き合い方 〜Cursor安定択説と、ツールに依存しない「資産」〜
empitsu
4
1.3k
今日から始めるAmazon Bedrock AgentCore
har1101
4
390
ClickHouseはどのように大規模データを活用したAIエージェントを全社展開しているのか
mikimatsumoto
0
190
オープンウェイトのLLMリランカーを契約書で評価する / searchtechjp
sansan_randd
3
650
データの整合性を保ちたいだけなんだ
shoheimitani
7
2.9k
プロポーザルに込める段取り八分
shoheimitani
0
160
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1k
広告の効果検証を題材にした因果推論の精度検証について
zozotech
PRO
0
100
(金融庁共催)第4回金融データ活用チャレンジ勉強会資料
takumimukaiyama
0
120
Featured
See All Featured
The Curious Case for Waylosing
cassininazir
0
230
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
The Invisible Side of Design
smashingmag
302
51k
The browser strikes back
jonoalderson
0
360
A better future with KSS
kneath
240
18k
Testing 201, or: Great Expectations
jmmastey
46
8k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
200
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
180
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
440
The Language of Interfaces
destraynor
162
26k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.3k
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