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
Je teste mon code avec py.test
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Ronan Amicel
October 25, 2014
Programming
370
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Je teste mon code avec py.test
Présentation à la conférence PyCon FR à Lyon le 25 octobre 2014.
Ronan Amicel
October 25, 2014
More Decks by Ronan Amicel
See All by Ronan Amicel
Développeur : ce qu’on ne m’a pas appris à l’école
ronnix
1
410
Refactoring: la méthode Mikado
ronnix
0
300
Techniques de test avancées en Python
ronnix
0
370
Product Development in a Startup
ronnix
2
280
Performance des frameworks web : Python vs The World (v1.1)
ronnix
1
8.4k
Performance des frameworks web : Python vs The World
ronnix
0
480
Introduction au Customer Development
ronnix
1
170
Rendez votre code Python plus beau !
ronnix
1
590
Trompez-vous, et vite !
ronnix
2
330
Other Decks in Programming
See All in Programming
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.5k
XHTMLが残したもの
yosuke_furukawa
PRO
2
440
Oxlintはいいぞ(続)
yug1224
1
560
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
340
Seeing Through Serverless: Observability for AWS Lambda with ADOT and CloudWatch Application Signals
seike460
PRO
1
120
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
640
ALB ログから Trace を気合で繋げる技術
fohte
7
880
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
4
1.1k
標準パッケージに uuid が追加された 背景から見る Go らしい意思決定 / go_127_uuid_decision
convto
3
2.7k
AIエージェント時代のコードレビューを設計する
nogu66
6
2.5k
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
110
コンパウンドプロダクト開発のためのローカルプロセスマネージャー再発明 #layerxgo
izumin5210
0
660
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Crafting Experiences
bethany
1
300
The World Runs on Bad Software
bkeepers
PRO
72
12k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
310
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
A Modern Web Designer's Workflow
chriscoyier
698
190k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
500
The Cost Of JavaScript in 2023
addyosmani
55
10k
Mind Mapping
helmedeiros
1
350
WENDY [Excerpt]
tessaabrams
12
39k
Transcript
Je teste mon code avec py.test Ronan Amicel @amicel PyCon
FR — 25 octobre 2014 — Lyon
Ronan Amicel Fondateur @ Pocket Sensei Hacker en résidence @
The Family
Préambule
None
C’est quoi py.test ?
$ py.test • Un outil en ligne de commande pour
lancer vos tests : • découverte • exécution • affichage des résultats
import pytest • Un framework pour écrire vos tests •
avec très peu de bla bla • avec un mécanisme de fixtures très flexible
Le plan • Écrire ses tests avec py.test • Moins
de bla bla • Utiliser les fixtures • Lancer ses tests avec py.test • Comment passer facilement à py.test
Écrire ses tests avec py.test
Moins de bla bla
Rappel : unittest import unittest def fact(n): return 1 if
n <= 1 else n * fact(n - 1) class TestFact(unittest.TestCase): def test_three(self): self.assertEqual(fact(3), 6) if __name__ == '__main__': unittest.main()
Avec py.test def fact(n): return 1 if n <= 1
else n * fact(n - 1) def test_fact(self): assert fact(3) == 6
Démo assertions
Les fixtures
Setup from selenium import webdriver import pytest @pytest.fixture def browser():
return webdriver.Firefox() def test_pycon(browser): browser.get('http://pycon.fr/') assert 'Bienvenue' in browser.title
Durée de vie @pytest.fixture(scope='function') ... @pytest.fixture(scope='module') ... @pytest.fixture(scope='session') ...
Teardown (1/2) @pytest.fixture def browser(request): b = webdriver.Firefox() def teardown():
b.quit() request.addfinalizer(teardown) return b
Teardown (2) @pytest.yield_fixture def browser(): b = webdriver.Firefox() yield b
b.quit()
Fixtures paramétrées @pytest.fixture(scope='session', params=['firefox', 'chrome']) def browser(request): if request.param ==
'firefox': return webdriver.Firefox() elif request.param == 'chrome': return webdriver.Chrome() else: raise ValueError('Unknown browser')
Trouver et lancer ses tests avec py.test
Rappel : Python ≥ 2.7 $ python -m unittest discover
py.test $ py.test
Découverte • Parcours récursif des répertoires • Fichiers test_*.py ou
*_test.py • Classes qui commencent par Test • Fonctions et méthodes qui commencent par test_ • Reconnait les tests unittest et nose existants
Exécuter seulement certains tests $ py.test tata/ $ py.test toto.py
$ py.test --pyargs mon.module
Collecter les tests uniquement $ py.test --collect-only
Un test bien précis $ py.test fichier.py::fonction $ py.test fichier.py::Classe::methode
Seulement les tests dont le nom contient un mot-clé $
py.test -k toto $ py.test -k 'toto or tata'
Exécution
S’arrêter en cas d'erreur $ py.test -x $ py.test --maxfail=3
Lancer le debugger en cas d’erreur $ py.test --pdb $
py.test -x --pdb
Contrôle de l'affichage
Afficher le nom des tests $ py.test -v
Ne pas capturer la sortie standard $ py.test -s $
py.test --capture=no
Niveau de détail des tracebacks $ py.test --tb=auto # défaut
$ py.test --tb=long $ py.test --tb=short $ py.test --tb=line $ py.test --tb=native $ py.test --tb=no
Afficher les valeurs des variables locales $ py.test --showlocals $
py.test -l
Afficher un résumé à la fin $ py.test -rf #
failures
Afficher les tests les plus lents $ py.test --durations=10
Plugins
Lancer ses tests en parallèle $ pip install pytest-xdist $
py.test -n4
Couverture de tests $ pip install pytest-cov $ py.test --cov
myproj
Django $ pip install pytest-django $ py.test --ds=proj.settings
BDD $ pip install pytest-bdd $ py.test --ds=proj.settings
Passer facilement à py.test
Recette facile en 2 étapes 1. Utilisez py.test pour lancer
vos tests existants (unittest, doctest, nose, Django) 2. Commencez à (ré)écrire vos tests dans le style py.test
En savoir plus http://pytest.org/
Questions ?
Merci !