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
Pythonic tests with Py.test
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Renato dos Santos Oliveira
July 08, 2015
Technology
0
170
Pythonic tests with Py.test
Talk presented at Geek Night Recife @ ThoughtWorks
Renato dos Santos Oliveira
July 08, 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
Testes pythonicos com Py.test
renatooliveira
0
180
Refactoring Django Applications (pt-BR)
renatooliveira
2
110
Django Migrations v1.0
renatooliveira
2
80
Other Decks in Technology
See All in Technology
SREのプラクティスを用いた3領域同時 マネジメントへの挑戦 〜SRE・情シス・セキュリティを統合した チーム運営術〜
coconala_engineer
2
620
AWS Network Firewall Proxyを触ってみた
nagisa53
0
200
20260204_Midosuji_Tech
takuyay0ne
1
140
学生・新卒・ジュニアから目指すSRE
hiroyaonoe
2
580
Tebiki Engineering Team Deck
tebiki
0
24k
AI駆動PjMの理想像 と現在地 -実践例を添えて-
masahiro_okamura
1
110
ファインディの横断SREがTakumi byGMOと取り組む、セキュリティと開発スピードの両立
rvirus0817
1
1.2k
Frontier Agents (Kiro autonomous agent / AWS Security Agent / AWS DevOps Agent) の紹介
msysh
3
160
使いにくいの壁を突破する
sansantech
PRO
1
130
Bill One 開発エンジニア 紹介資料
sansan33
PRO
4
17k
Oracle Cloud Observability and Management Platform - OCI 運用監視サービス概要 -
oracle4engineer
PRO
2
14k
Codex 5.3 と Opus 4.6 にコーポレートサイトを作らせてみた / Codex 5.3 vs Opus 4.6
ama_ch
0
110
Featured
See All Featured
Balancing Empowerment & Direction
lara
5
880
30 Presentation Tips
portentint
PRO
1
210
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
64
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
280
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
117
110k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
170
Statistics for Hackers
jakevdp
799
230k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.1k
Faster Mobile Websites
deanohume
310
31k
First, design no harm
axbom
PRO
2
1.1k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
200
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
0
310
Transcript
Pythonic tests with py.test Renato Oliveira
Who?! ❏ Renato Oliveira ❏ Labcodes Software Studio ❏ Django
Software Foundation ❏ Recently migrated to Py.test
Sumary ❏ Introduction ❏ Basic Usage ❏ Test discovery ❏
Markers ❏ Fixtures ❏ Plugins
FIX IN PROD
Tests in prod
Tests in prod
Introduction “Pytest is a mature full featured Python testing tool
that helps you to write better programs.”
Introduction - Assert with assert statement def sum(a, b): return
a + b def test_sum(): assert sum(1, 1) == 2
Basic usage - Running - Failure report
Test discovery and runner Simply run your tests with $
py.test
Test discovery and runner Verbosely $ py.test -v
Test discovery and runner Pytest recursively searches your folders looking
for test files test_*.py or *_test.py
Test discovery and runner You can test just a file
$ py.test test_foo.py
Test discovery and runner You can also execute just one
test $ py.test -k test_foo
Migrating Unittest code Code before 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 Code after def to_upper(text): return text.upper() def
test_to_upper(): return to_upper("pug") == "PUG"
Markers Gives you the ability to add some extra metadata
to your tests. ❏ skipif ❏ xfail ❏ parametrize ❏ your owns
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 The purpose of test fixtures is to provide a
fixed baseline upon which tests can reliably and repeatedly execute.
Fixtures Pytest fixtures allows you to decouple your test suite
from the context you want them to run.
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
Share fixtures - pytest.mark.usefixtures - conftest.py - “usefixtures” param in
pytest.ini
Useful plugins - pytest-django - pytest-mock - http://pytest. org/latest/plugins_index/index.html
Questions?
Obrigado! :)
[email protected]
@_renatooliveira github/renatooliveira www.labcodes.com.br