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
Renato dos Santos Oliveira
July 08, 2015
Technology
0
160
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
310
Testes pythonicos com Py.test
renatooliveira
0
180
Refactoring Django Applications (pt-BR)
renatooliveira
2
110
Django Migrations v1.0
renatooliveira
2
77
Other Decks in Technology
See All in Technology
強化されたAmazon Location Serviceによる新機能と開発者体験
dayjournal
2
210
~宇宙最速~2025年AWS Summit レポート
satodesu
1
1.8k
Observability infrastructure behind the trillion-messages scale Kafka platform
lycorptech_jp
PRO
0
140
SalesforceArchitectGroupOsaka#20_CNX'25_Report
atomica7sei
0
150
Snowflake Summit 2025全体振り返り / Snowflake Summit 2025 Overall Review
mtpooh
2
390
AWS CDK 実践的アプローチ N選 / aws-cdk-practical-approaches
gotok365
6
740
A2Aのクライアントを自作する
rynsuke
1
170
標準技術と独自システムで作る「つらくない」SaaS アカウント管理 / Effortless SaaS Account Management with Standard Technologies & Custom Systems
yuyatakeyama
3
1.2k
Oracle Audit Vault and Database Firewall 20 概要
oracle4engineer
PRO
3
1.7k
GitHub Copilot の概要
tomokusaba
1
130
Postman AI エージェントビルダー最新情報
nagix
0
110
Amazon ECS & AWS Fargate 運用アーキテクチャ2025 / Amazon ECS and AWS Fargate Ops Architecture 2025
iselegant
16
5.5k
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Designing for Performance
lara
609
69k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.3k
For a Future-Friendly Web
brad_frost
179
9.8k
Facilitating Awesome Meetings
lara
54
6.4k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
A Tale of Four Properties
chriscoyier
160
23k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.9k
Why You Should Never Use an ORM
jnunemaker
PRO
57
9.4k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
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