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
EMConf JP 2025 懇親会LT / EMConf JP 2025 social gathering
sugamasao
2
200
株式会社Awarefy(アウェアファイ)会社説明資料 / Awarefy-Company-Deck
awarefy
3
11k
AWS Well-Architected Frameworkで学ぶAmazon ECSのセキュリティ対策
umekou
2
150
Snowflake ML モデルを dbt データパイプラインに組み込む
estie
0
100
IAMのマニアックな話2025
nrinetcom
PRO
4
700
Two Blades, One Journey: Engineering While Managing
ohbarye
4
2k
AWSではじめる Web APIテスト実践ガイド / A practical guide to testing Web APIs on AWS
yokawasa
8
710
Fraxinus00tw assembly manual
fukumay
0
110
コンピュータビジョンの社会実装について考えていたらゲームを作っていた話
takmin
1
610
30→150人のエンジニア組織拡大に伴うアジャイル文化を醸成する役割と取り組みの変化
nagata03
0
180
DevinでAI AWSエンジニア製造計画 序章 〜CDKを添えて〜/devin-load-to-aws-engineer
tomoki10
0
130
遷移の高速化 ヤフートップの試行錯誤
narirou
6
1.2k
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
BBQ
matthewcrist
87
9.5k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
Raft: Consensus for Rubyists
vanstee
137
6.8k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
10
510
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Building an army of robots
kneath
303
45k
Faster Mobile Websites
deanohume
306
31k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Building a Scalable Design System with Sketch
lauravandoore
461
33k
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