Created
September 20, 2015 17:13
-
-
Save abele/ee049b1fdf7e4a1af71a to your computer and use it in GitHub Desktop.
Abstract Test or Contract Test with pytest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
def square(num): | |
return num * num | |
def recursive_map(f, _list): | |
"""Recusive map implementation.""" | |
if not _list: | |
return [] | |
else: | |
head, *tail = _list | |
h = [f(head)] | |
h.extend(recursive_map(f, tail)) | |
return h | |
class MapContract(object): | |
@pytest.fixture | |
def a_map(self): | |
raise NotImplementedError('Not Implemented Yet') | |
def test_can_handle_small_list(self, a_map): | |
assert list(a_map(square, [1,3,4])) == [1, 9, 16] | |
def test_can_handle_large_list(self, a_map): | |
num = 10000 | |
assert len(list(a_map(square, range(num)))) == num | |
class TestSTDLibContract(MapContract): | |
@pytest.fixture | |
def a_map(self): | |
return map | |
class TestRecursiveMap(MapContract): | |
@pytest.fixture | |
def a_map(self): | |
return recursive_map |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks... I was puzzeled on how to implement contact tests in pytest ... clear and simple