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
170
Refactoring Django Applications (pt-BR)
renatooliveira
2
110
Django Migrations v1.0
renatooliveira
2
77
Other Decks in Technology
See All in Technology
今日から始めるAWSセキュリティ対策 3ステップでわかる実践ガイド
yoshidatakeshi1994
0
130
S3アクセス制御の設計ポイント
tommy0124
3
210
LLMを搭載したプロダクトの品質保証の模索と学び
qa
1
1.1k
「全員プロダクトマネージャー」を実現する、Cursorによる仕様検討の自動運転
applism118
22
12k
はじめてのOSS開発からみえたGo言語の強み
shibukazu
4
1k
データ分析エージェント Socrates の育て方
na0
8
2.9k
メルカリIBISの紹介
0gm
0
640
TS-S205_昨年対比2倍以上の機能追加を実現するデータ基盤プロジェクトでのAI活用について
kaz3284
1
230
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
10
75k
LLM時代のパフォーマンスチューニング:MongoDB運用で試したコンテキスト活用の工夫
ishikawa_pro
0
170
slog.Handlerのよくある実装ミス
sakiengineer
4
480
Rustから学ぶ 非同期処理の仕組み
skanehira
1
150
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
188
55k
Faster Mobile Websites
deanohume
309
31k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
3k
The Cult of Friendly URLs
andyhume
79
6.6k
Typedesign – Prime Four
hannesfritz
42
2.8k
Thoughts on Productivity
jonyablonski
70
4.8k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.9k
Making Projects Easy
brettharned
117
6.4k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
A designer walks into a library…
pauljervisheath
207
24k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
590
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
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