Kent Beck
No se escribe código sin antes escribir una prueba unitaria que especifica su objetivo y comprueba que se cumple.
def test_add():
assert add() == 8
calc.py
$ pytest calc.py
=================================== test session starts ====================================
platform linux -- Python 3.6.6rc1, pytest-3.6.1, py-1.5.3, pluggy-0.6.0
rootdir: /data/Documentos/proyectos-python/demo-pytest, inifile:
collected 1 item
calc.py F [100%]
=================================== FAILURES ==============================================
___________________________________ test_add ______________________________________________
def test_add():
> assert add() == 8
E NameError: name 'add' is not defined
calc.py:2: NameError
================================= 1 failed in 0.08 seconds ================================
def add():
return 8
def test_add():
assert add() == 8
calc.py
$ pytest calc.py
================================= test session starts =================================
platform linux -- Python 3.6.6rc1, pytest-3.6.1, py-1.5.3, pluggy-0.6.0
rootdir: /data/Documentos/proyectos-python/demo-pytest, inifile:
collected 1 item
calc.py . [100%]
================================= 1 passed in 0.07 seconds ============================
def add():
return 8
def test_add_equals_to_8():
assert add() == 8
def test_add_equals_to_5():
assert add() == 5
calc.py
$ pytest calc.p
============================= test session starts ================================
platform linux -- Python 3.6.6rc1, pytest-3.6.1, py-1.5.3, pluggy-0.6.0
rootdir: /data/Documentos/proyectos-python/demo-pytest, inifile:
collected 2 items
calc.py .F [100%]
============================= FAILURES ===========================================
_____________________________ test_add_equals_to_5 _______________________________
def test_add_equals_to_5():
> assert add() == 5
E assert 8 == 5
E + where 8 = add()
calc.py:8: AssertionError
============================= 1 failed, 1 passed in 0.11 seconds =================
def add(first_number, second_number):
return first_number + second_number
def test_add_equals_to_8():
assert add(5, 3) == 8
def test_add_equals_to_5():
assert add(2, 3) == 5
calc.py
$ pytest calc.py
============================== test session starts ================================
platform linux -- Python 3.6.6rc1, pytest-3.6.1, py-1.5.3, pluggy-0.6.0
rootdir: /data/Documentos/proyectos-python/demo-pytest, inifile:
collected 2 items
calc.py .. [100%]
============================== 2 passed in 0.07 seconds ============================