Jupyter Snippet CB2nd 07_test
Jupyter Snippet CB2nd 07_test
Writing unit tests with py.test
%%writefile first.py
def first(l):
return l[0]
Overwriting first.py
%%writefile -a first.py
# This is appended to the file.
def test_first():
assert first([1, 2, 3]) == 1
Appending to first.py
%cat first.py
def first(l):
return l[0]
# This is appended to the file.
def test_first():
assert first([1, 2, 3]) == 1
!pytest first.py
============= test session starts ==============
platform linux -- Python 3.6.3, pytest-3.2.1, py-1.4.34
rootdir: ~/git/cookbook-2nd/chapter02_best_practices:
plugins: cov-2.5.1
collecting 0 items
collecting 1 item
collected 1 item
first.py .
=========== 1 passed in 0.00 seconds ===========
%%writefile first.py
def first(l):
return l[0]
def test_first():
assert first([1, 2, 3]) == 1
assert first([]) is None
Overwriting first.py
!pytest first.py
============= test session starts ==============
platform linux -- Python 3.6.3, pytest-3.2.1, py-1.4.34
rootdir: ~/git/cookbook-2nd/chapter02_best_practices:
plugins: cov-2.5.1
collecting 0 items
collecting 1 item
collected 1 item
first.py F
=================== FAILURES ===================
__________________ test_first __________________
def test_first():
assert first([1, 2, 3]) == 1
> assert first([]) is None
first.py:6:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
l = []
def first(l):
> return l[0]
E IndexError: list index out of range
first.py:2: IndexError
=========== 1 failed in 0.02 seconds ===========
%%writefile first.py
def first(l):
return l[0] if l else None
def test_first():
assert first([1, 2, 3]) == 1
assert first([]) is None
Overwriting first.py
!pytest first.py
============= test session starts ==============
platform linux -- Python 3.6.3, pytest-3.2.1, py-1.4.34
rootdir: ~/git/cookbook-2nd/chapter02_best_practices:
plugins: cov-2.5.1
collecting 0 items
collecting 1 item
collected 1 item
first.py .
=========== 1 passed in 0.00 seconds ===========