Skip to content

Instantly share code, notes, and snippets.

@abele
Created September 20, 2015 17:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abele/ee049b1fdf7e4a1af71a to your computer and use it in GitHub Desktop.
Save abele/ee049b1fdf7e4a1af71a to your computer and use it in GitHub Desktop.
Abstract Test or Contract Test with pytest
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
@la10736
Copy link

la10736 commented Jul 14, 2016

Thanks... I was puzzeled on how to implement contact tests in pytest ... clear and simple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment