Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Testes pythonicos com pytest

4,569 views

Published on

Se eu não preciso criar classes para muitas coisas em Python, por que eu preciso delas para fazer testes automatizados? Py.test resolve a questão.

Published in: Technology
  • Be the first to comment

Testes pythonicos com pytest

  1. 1. 1 de 13 py.test testes pythonicos com Vinicius Assef - @viniciusban
  2. 2. 2 de 13 ambiente ● sem boilerplate ● sem classes ● roda doctests, unittests e nosetests $ pip install pytest
  3. 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. 4 de 13 convenções ● test_*.py ou *_test.py ● recursedirs ● class Test* # sem def __init__() ● def test_*() ● configuráveis via .ini
  5. 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. 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. 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 =================
  8. 8. 8 de 13 # conftest.py import pytest @pytest.fixture def texto(): return "abc" fixtures # test_tudo.py def test_abc(texto): assert texto == "abc"
  9. 9. 9 de 13 # conftest.py import pytest @pytest.fixture def xyz(): return "x" @pytest.fixture def texto(xyz): return "abc %s" % xyz fixtures consomem fixtures
  10. 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. 11 de 13 fixtures automáticas @pytest.fixture(autouse=True) ● setUp e tearDown on steroids
  12. 12. 12 de 13 plugins ● pytest-django ● pytest-xdist ● pytest-pep8 ● pytest-cov ● oejskit ● Executados automaticamente
  13. 13. 13 de 13 viniciusban twitter gmail github skype

×