functions: test_passthrough_functions


test_passthrough_functions

Passthrough functions return their input as output

red: make it fail

I add a failing test to the TestFunctions class in test_functions.py

def test_passthrough_functions(self):
    self.assertEqual(functions.passthrough(False), False)

the terminal shows AttributeError

green: make it pass

  • I add a function definition to functions.py

    def passthrough():
        return None
    

    the terminal shows TypeError

    TypeError: passthrough() takes 0 positional arguments but 1 was given
    

    because the definition for passthrough does not allow inputs and the test sends False as input

  • I add the error to the list of Exceptions encountered

    # Exceptions Encountered
    # AssertionError
    # ModuleNotFoundError
    # AttributeError
    # TypeError
    
  • then I make passthrough in functions.py to take 1 positional argument

    def passthrough(argument):
        return None
    

    and the terminal shows AssertionError

    AssertionError: None != False
    

    because the result of calling functions.passthrough with False as input is None which is not equal to the expected result (False)

  • I change the definition of passthrough to make the test pass

    def passthrough(argument):
        return False
    

    the terminal shows passing tests. I am genius!

refactor: make it better

Wait a minute! Something is not quite right here. The definition for a passthrough function was that it returned the same thing it was given, the test passes when False is given as input, will it still pass when another value is given or will it always return False? Time to write a test

  • I add a new assertion to test_passthrough_functions

    def test_passthrough_functions(self):
        self.assertEqual(functions.passthrough(False), False)
        self.assertEqual(functions.passthrough(True), True)
    

    the terminal shows AssertionError

    AssertionError: False != True
    

    the function returns False instead of True in the second case, I am not all the way genius, yet

  • I change the definition of passthrough in functions.py

    def passthrough(argument):
        return argument
    

    the terminal shows passing tests. I have more confidence that the passthrough function will return its input.

  • I add more tests for good measure using the other Python data structures

    def test_passthrough_functions(self):
        self.assertEqual(functions.passthrough(False), False)
        self.assertEqual(functions.passthrough(True), True)
        self.assertEqual(functions.passthrough(None), False)
        self.assertEqual(functions.passthrough(int), False)
        self.assertEqual(functions.passthrough(str), False)
        self.assertEqual(functions.passthrough(tuple), False)
        self.assertEqual(functions.passthrough(list), False)
        self.assertEqual(functions.passthrough(set), False)
        self.assertEqual(functions.passthrough(dict), False)
    

    the terminal shows AssertionError for each line until I make the input match the output, proving that the passthrough function I have defined returns the input it is given. Hooray! I am genius again


review

From the tests I know

Would you like to test_functions_w_positional_arguments?


functions: tests and solutions