Skip to content

Instantly share code, notes, and snippets.

@davidsonbrsilva
Created June 10, 2019 01:06
Show Gist options
  • Save davidsonbrsilva/2ad4bbf3c02ff038b9b04a98e419e6a4 to your computer and use it in GitHub Desktop.
Save davidsonbrsilva/2ad4bbf3c02ff038b9b04a98e419e6a4 to your computer and use it in GitHub Desktop.
Generic unit test function
from sample import reverse, add, mult, is_adult, compare
from unittest import test
# Testing reverse function
test("olleh", reverse, "hello")
test("ababa", reverse, "hello")
# Testing add function
test(10, add, 2, 3, 5)
test(10, add, 2, 3, 4)
test(10, add, 4, 6)
test(10, add, 4, 5)
# Testing mult function
test(12, mult, 2, 3, 2)
test(12, mult, 2, 3, 3)
test(12, mult, 3, 4)
test(12, mult, 3, 3)
# Testing is_adult function
test(True, is_adult, 19)
test(False, is_adult, 19)
# Testing compare function
test(True, compare, "house", "house", "house")
test(True, compare, "house", "house", "home")
def reverse(*args):
return args[0][::-1]
def add(*args):
result = 0
for num in args:
result += num
return result
def mult(*args):
result = 1
for num in args:
result *= num
return result
def is_adult(*args):
return args[0] >= 18
def compare(*args):
first = args[0]
for arg in args:
if not first == arg:
return False
return True
def test(expected, function, *args):
print("Testing ", function.__name__, args, sep="")
print("Return: ", function(*args))
print("Expected: ", expected)
print("Result: ", end=" ")
try:
assert function(*args) == expected
print("passed")
except AssertionError:
print("not passed")
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment