2. 2 de 13
ambiente
● sem boilerplate
● sem classes
● roda doctests, unittests e nosetests
$ pip install pytest
3. 3 de 13
# test_f.py
from modulo import f
def test_f():
assert f(1) == 1
simplesmente teste
# test_f.py
import pytest
from modulo import um
def test_um():
with pytest.raises(ValueError):
um(2)
4. 4 de 13
convenções
● test_*.py ou *_test.py
● recursedirs
● class Test* # sem def __init__()
● def test_*()
● configuráveis via .ini
5. 5 de 13
mensagens de erro
$ pytest -v
======================== test session starts
=========================
platform linux2 -- Python 2.7.3 -- pytest-2.3.5
collected 1 items
test_f.py:10: test_f1_eh_1 FAILED
============================== FAILURES
==============================
____________________________ test_f1_eh_1
____________________________
def test_f1_eh_1():
> assert f(1) == 0
E assert 1 == 0
E + where 1 = f(1)
test_f.py:11: AssertionError
====================== 1 failed in 0.01 seconds
======================
6. 6 de 13
erro em lista
============================== FAILURES
==============================
________________________ test_lista_completa
_________________________
def test_lista_completa():
> assert "a b".split() == lista()
E assert ['a', 'b'] == ['a', 'b', 'c']
E Right contains more items, first extra item: 'c'
test_f.py:8: AssertionError
====================== 1 failed in 0.01 seconds
======================
7. 7 de 13
erro em dicionário
__________________________ test_dicionario
___________________________
def test_dicionario():
> assert dict(i=20, n="pedro", f="junior") == dicionario()
E assert {'f': 'junior... 'n': 'pedro'} == {'i': 20, 'n':
'pedro'}
E Hiding 2 identical items, use -v to show
E Left contains more items:
E {'f': 'junior'}
test_f.py:10: AssertionError
================= 1 failed, 1 passed in 0.02 seconds
=================
10. 10 de 13
escopo de fixtures
@pytest.fixture(scope="module")
● setUp e tearDown
● request.addfinalizer()
● podem receber parâmetros do módulo de
teste
11. 11 de 13
fixtures automáticas
@pytest.fixture(autouse=True)
● setUp e tearDown on steroids