Last active
October 17, 2016 00:33
-
-
Save jodoherty/f9b3d5f3c713c43b7bbc28eb74f43010 to your computer and use it in GitHub Desktop.
Example for easily creating and running test cases for sms-tools assignments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ... | |
| import numpy as np | |
| def extractMainLobe(window, M): | |
| # ... | |
| return np.zeros(M) | |
| if __name__ == '__main__': | |
| # The check above ensures the following code only runs if we run this | |
| # module directly | |
| import testTools | |
| # Define our test | |
| @testTools.test('A4', 1) | |
| def part1Test(inputs, output): | |
| window = inputs['window'] | |
| M = inputs['M'] | |
| myOutput = extractMainLobe(window, M) | |
| f, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True) | |
| ax1.set_title('Expected Output') | |
| ax1.scatter(np.arange(0, len(output)), output) | |
| ax2.set_title('My Output') | |
| ax2.scatter(np.arange(0, len(myOutput)), myOutput) | |
| plt.show() | |
| print(myOutput) | |
| if len(myOutput) != len(output): | |
| raise testTools.TestCaseFailure('Output length {0} differs from expected output length {1}'.format(len(myOutput), len(output))) | |
| if np.any(myOutput - eps < output) or np.any(myOutput + eps > output): | |
| raise testTools.TestCaseFailure('Output differs') | |
| # Run our test | |
| part1Test() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright (c) 2016 James O'Doherty <james@odoherty.family> | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the "Software"), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so, subject to the following conditions: | |
| # | |
| # The above copyright notice and this permission notice shall be included in | |
| # all copies or substantial portions of the Software. | |
| # | |
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| # SOFTWARE. | |
| # | |
| import pickle | |
| class TestCaseFailure(Exception): | |
| pass | |
| def test(assignment, partId): | |
| filename = 'testInput{0}.pkl'.format(assignment) | |
| key = '{0}-part-{1}'.format(assignment, partId) | |
| with open(filename, 'r') as f: | |
| testData = pickle.load(f) | |
| def runTestCase(testCase): | |
| def inner(): | |
| for i, inputs in enumerate(testData['exampleInputs'][key]): | |
| try: | |
| testCase(inputs, testData['exampleOutputs'][key][i]) | |
| print('{0}: Test case passed!'.format(i)) | |
| except Exception as e: | |
| print('{0}: Test case failed: {1}'.format(i, type(e))) | |
| print('{0}: \t{1}'.format(i, e)) | |
| return inner | |
| return runTestCase |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you add your test cases to the end of each module in your assignment, then you can test your assignments by running each part directly: