Python testing framework for codingbat tutorial

Working through the codingbat.com Python tutorial, I created this testing framework for problems that return a value (do they all??). This demonstrates using it with a problem that returns a boolean:

### DO NOT EDIT...start

### Global variables
totalCount = 0    #: The total number of tests attempted
successCount = 0  #: The number of tests having the exepected response

def printIfExpectedNotActual(expected, actual, param_debugging):
    """
        If the expected and actual responses are equal, this prints
        nothing. Otherwise this prints the expected value, plus the
        names and values of all parameters.

        This also increments the number of total tests and successful
        tests (when expected equals actual), as stored in the global
        variables ``successCount`` and ``totalCount``.

        expected
            The value that should be returned by the function.
        actual
            The value actually returned by the function.
        param_debugging
            A string of all parameter names and values, in the format
            ``"name1=" + value1 + ", name2=" + value2 + ...``. When the
            expected and actual values are equal, this is ignored.
    """
    global totalCount; totalCount+=1
    if(actual == expected):
        global successCount; successCount+=1
        return
    print("FAILURE: expected='", expected, "', actual='", actual, "', ",\
        param_debugging, sep="")

### DO NOT EDIT...end

"""
    EDIT THIS: Middleman between unit assertion and the primary function.
"""
            #EDIT LINE: The parameters after
            #      expected
def testExpectedWithParams(expected, n):

            #EDIT LINE: actual = [THE FUNCTION CALL]
    actual = pos_neg(n)

            #EDIT LINE: each parameter: "x=" + str(x) + ", y=" + y ...
    paramDebugging = "n=" + str(n)

            #DO NOT edit next line:
    printIfExpectedNotActual(expected, actual, paramDebugging)


"""
    EDIT THIS: The function to test
               This is what you paste into codingbat!
"""
def near_hundred(n):
    return  ((abs(100 - n) <= 10)  or  (abs(200 - n) <= 10))

"""
    EDIT THIS: Test calls
"""
testExpectedWithParams(False, 89)
for i in range(90, 111):
    testExpectedWithParams(True, i)
testExpectedWithParams(False, 111)
testExpectedWithParams(False, 189)
for i in range(190, 211):
    testExpectedWithParams(True, i)
testExpectedWithParams(False, 211)

"""
#Examples from codingbat:
#Copied directly from coding bat:
near_hundred(93) ? True
near_hundred(90) ? True
near_hundred(89) ? False
"""

#Examples from codingbat:
#Function name changed to testExpectedWithParams, expected value as
#1st param
testExpectedWithParams(True, 93)
testExpectedWithParams(True, 90)
testExpectedWithParams(False, 89)

#Extra tests:

### DO NOT EDIT the next line
print("total tests=", totalCount, ", correct=", successCount, \
    ", incorrect=", (totalCount - successCount), sep="")

Success response:

total tests=49, correct=49, incorrect=0

Example failure response, after changing

  • testExpectedWithParams(True, 90) to testExpectedWithParams(False, 90), and
  • testExpectedWithParams(False, 89) to testExpectedWithParams(True, 89)

FAILURE: expected='False', actual='True', n=90
FAILURE: expected='True', actual='False', n=89
total tests=49, correct=47, incorrect=2


And another demonstrating a problem that returns a string:

"""
    EDIT THIS: Middleman between unit assertion and the primary function.
"""
            #EDIT LINE: The parameters after
            #      expected
def testExpectedWithParams(expected, str2, n):

            #EDIT LINE: actual = [THE FUNCTION CALL]
    actual = not_string(str2, n)

            #EDIT LINE: each parameter: "x=" + str(x) + ", y=" + str(y) ...
    paramDebugging = "str2=" + str(str2) + ", n=" + str(n)

            #Do not edit next line:
    printIfExpectedNotActual(expected, actual, paramDebugging)

"""
    EDIT THIS: The function to test
               This is what you paste into codingbat!
"""
def not_string(str2, n):  #Actually named "str", which masks the built-in "str"
    return  str2[0:n] + str2[(n + 1):len(str2)]

"""
    EDIT THIS: Test calls
"""
testExpectedWithParams('itten', 'kitten', 0)
testExpectedWithParams('ktten', 'kitten', 1)
testExpectedWithParams('kiten', 'kitten', 2)
testExpectedWithParams('kiten', 'kitten', 3)
testExpectedWithParams('kittn', 'kitten', 4)
testExpectedWithParams('kitte', 'kitten', 5)

#Examples from codingbat:
testExpectedWithParams('ktten', 'kitten', 1)  #missing_char('kitten', 1) ? 'ktten'
testExpectedWithParams('itten', 'kitten', 0)  #missing_char('kitten', 0) ? 'itten'
testExpectedWithParams('kittn', 'kitten', 4)  #missing_char('kitten', 4) ? 'kittn'

Success response:

total tests=9, correct=9, incorrect=0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s