Skip to content

Instantly share code, notes, and snippets.

@ljvmiranda921
Forked from abele/test_map_contract.py
Created September 12, 2018 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljvmiranda921/5e50ecfde941f43d4c356e96ab826a10 to your computer and use it in GitHub Desktop.
Save ljvmiranda921/5e50ecfde941f43d4c356e96ab826a10 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
@bittrance
Copy link

❤️

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