# testgym.py # Lillian Lee (LJL2), Steve Marschner (SRM2), Walker M. White (wmw2) # Feb 4, 2014 """Unit test for the module gym. This module illustrates what a unit test should look like. It is doing some slightly fancier stuff than what we saw in lecture, to give you an idea of how using test cases in a unit test framework can allow you to do somewhat more complexly things in a succinct fashion.""" import gym # file containing function to test import cornelltest # assert functions # using a variable to hold the function being tested # may look weird, but it makes it easier to test two functions # that are meant to do the exact same thing. def test_dscore(testfn): """Test procedure for testfn, assumed to be a 'difficulty score' extractor in the sense presented in lecture 04. """ # no spaces cornelltest.assert_floats_equal(6.7,testfn('RAISMAN,6.7,9.0,0')) # irregular spacing, decimal numbers cornelltest.assert_floats_equal(6.2,testfn('PONOR , 6.2 , 9.0 , 0')) # more irregular spacing, 'int-looking' numbers cornelltest.assert_floats_equal(7.0,testfn(' SMITH , 7, 9 , 0')) # name with space in it, lower-case cornelltest.assert_floats_equal(7.0,testfn(' Van Buren , 7, 2.3 , 0')) # hyphenated name cornelltest.assert_floats_equal(8.0,testfn(' Berners-Lee , 8, 9 , 0')) # This function does not return a value. # Application code if __name__ == '__main__': # This 'try/except' statement allows us to proceed even if an error # occurs. This is NOT what CS1110 students should use early in the # semester because we haven't explained this construct; but it # does allow this test unit to test both a wrong and a "right" # implementation. #try: # testfn = gym.dscore # test_dscore(testfn) # print 'All test cases for function ' + testfn.__name__ + ' passed' #except: # print 'An error occurred testing ' +testfn.__name__ + '.' # print 'This is expected for the "buggy" implementation.' testfn = gym.dscore test_dscore(testfn) print 'All test cases for function ' + testfn.__name__ + ' passed' testfn = gym.dscore_readable test_dscore(testfn) print 'All test cases for function ' + testfn.__name__ + ' passed' print 'Module gym has been tested'