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
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
310
Pythonic tests with Py.test
renatooliveira
0
160
Refactoring Django Applications (pt-BR)
renatooliveira
2
110
Django Migrations v1.0
renatooliveira
2
77
Other Decks in Technology
See All in Technology
AIとともに進化するエンジニアリング / Engineering-Evolving-with-AI_final.pdf
lycorptech_jp
PRO
0
140
事業成長の裏側:エンジニア組織と開発生産性の進化 / 20250703 Rinto Ikenoue
shift_evolve
PRO
1
130
モバイル界のMCPを考える
naoto33
0
350
AI導入の理想と現実~コストと浸透〜
oprstchn
0
150
Tech-Verse 2025 Global CTO Session
lycorptech_jp
PRO
0
1.1k
作曲家がボカロを使うようにPdMはAIを使え
itotaxi
0
390
fukabori.fm 出張版: 売上高617億円と高稼働率を陰で支えた社内ツール開発のあれこれ話 / 20250704 Yoshimasa Iwase & Tomoo Morikawa
shift_evolve
PRO
1
110
本が全く読めなかった過去の自分へ
genshun9
0
710
Tokyo_reInforce_2025_recap_iam_access_analyzer
hiashisan
0
140
KubeCon + CloudNativeCon Japan 2025 Recap
ren510dev
1
300
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
26k
Understanding_Thread_Tuning_for_Inference_Servers_of_Deep_Models.pdf
lycorptech_jp
PRO
0
150
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Raft: Consensus for Rubyists
vanstee
140
7k
Stop Working from a Prison Cell
hatefulcrawdad
270
20k
The Invisible Side of Design
smashingmag
300
51k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.4k
What's in a price? How to price your products and services
michaelherold
246
12k
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