Skip to content

Instantly share code, notes, and snippets.

@timbaileyjones
Created May 9, 2017 16:57
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 timbaileyjones/08547105c462fa85660e250afc1495d1 to your computer and use it in GitHub Desktop.
Save timbaileyjones/08547105c462fa85660e250afc1495d1 to your computer and use it in GitHub Desktop.
Example of a unit test for a list-flattening function
import types
import unittest
def flatten(input, list=[]):
if isinstance(input, types.ListType):
for item in input:
flatten(item, list)
else:
list = list.append(input)
return list
class TestFlatten(unittest.TestCase):
def testFlattenMethod(self):
INPUT=[[1,2,[3],4,[5,[6,7,8]]]]
EXPECTED_OUTPUT=[1,2,3,4,5,6,7,8]
ACTUAL_OUTPUT=flatten(INPUT);
#print "EXPECTED_OUTPUT", EXPECTED_OUTPUT
#print "ACTUAL_OUTPUT", ACTUAL_OUTPUT
self.assertTrue(EXPECTED_OUTPUT, ACTUAL_OUTPUT)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment