Skip to content

Instantly share code, notes, and snippets.

@the-vampiire
Last active October 28, 2017 07:29
Show Gist options
  • Select an option

  • Save the-vampiire/03f9caf45969c425357e98ad7ab1c4c9 to your computer and use it in GitHub Desktop.

Select an option

Save the-vampiire/03f9caf45969c425357e98ad7ab1c4c9 to your computer and use it in GitHub Desktop.
Simple Unit Testing Environment
# performs a single unit test
# accepts a function to test, a test case input and its corresponding expected output
def unit_test(function_to_test, test_input, expected_output):
try: # try something (whatever is in the code block)
function_output = function_to_test(test_input)
except Exception as error: # if it DOES NOT work then catch the error using the except keyword
# Exception is a general "catch-all" exception
# catch the Exception and store the error message as a variable called error
print("Error occurred with test input: [{0}] value: {1}\nError Message: {2}\nCorrect the error and try again.\n"
.format(type(test_input), test_input, error)) # use a template (format) string to output useful information
else: # if it works move to the else statement block (scope is preserved from the try block)
try: # try confirming that the function's output matches the expected output using the assert keyword
assert function_output == expected_output
print(unit_test_response(True, test_input, function_output, expected_output)) # True is the first argument signifying the test passed
except AssertionError: # in this case the exception (error) that is caught is AssertionError meaning the function's output DID NOT match the expected
print(unit_test_response(False, test_input, function_output, expected_output)) # False is the first argument signifying the test failed
def unit_test_response(correct, test_input, function_output, expected_output): # generates the message output from the completed test (used on lines 16 / 18)
score = "Test Passed" if correct else "Test Failed" # ternary statement determines whether to label the test as Pass or Fail
return "{0}\nInput: {1}\nExpected Output: {2}\nFunction Output: {3}\n".format(score, test_input, expected_output, function_output)
def run_unit_tests(function_to_test, test_list): # iterates through a list of test case pairs (called tuples in Python)
for test_tuple in test_list:
test_input, expected_output = test_tuple # "unpacks" (Python terminology) the tuple into the two variables
# test_input = test_tuple[0] # (can be written this way as well which may look more familiar)
# expected_output = test_tuple[1]
unit_test(function_to_test, test_input, expected_output) # calls the unit_test function for each iteration of the loop
"""
Process Flow:
1) iterate (loop) through the list of test cases
2) store the test input and its corresponding expected output via unpacking of the current test case tuple (that sounded way more scifi that it needed to)
3) for each iteration of the loop call the unit_test function passing in the current test input and expected output
4) follow the try / except path
5) call the unit_test_response function to create the message output
6) repeat until out of test cases
"""
# Uncomment below to try it out using the is_leap function as an example
# try putting in wrong values or even incorrect types (such as a string) and see what happens
# you can use this with codelens or put in print statements throughout the functions to better understand the system
# test_list = [(1900, False), (1956, True), (1600, True)] # each element in this list is a tuple designated by the ()
# def is_leap(year):
# return year % 400 == 0 if year % 100 == 0 else year % 4 == 0
# run_unit_tests(is_leap, test_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment