Sunteți pe pagina 1din 3

Deadline: May 21 , 23:59 (that's the night between Monday and Tuesday) In this exercise you're going to write

object oriented code in python. Oh joy!!! :) Following the lessons of the previous exercise, you're going to be writing a [really] simple testing system. You need to write two classes, Test and TestManager (in the corresponding files test.py and test_manager.py) and a main file called tester.py Their APIs are as follows: test.py API Test is a class that is meant to test a single line of python code. When instantiating Test, the user knows which line he/she's going to test. The constructor receives the line to be tested (as a string), an identification number (be careful of overriding built-in functions with your variable names...), and a list of parameter names (strings) that this line relies on. get_id returns the identification number of the test add_test_case receives a tuple/list of the input values for the parameters, and an output parameter, which holds the desired result. setup_test receives the tuple of input values for a single test case, and creates the variables with the right values (How is this done? Try the hints in the end of the exercise. Be careful not to overrun variable, built in functions as so on...) cleanup_test undo-s the mess you made in the setup_test method. This is an important practice, otherwise tests become order sensitive, they cascade on each other and make it hard to really use as decent debugging tools. run_tests iterates over all the test cases, sets up the test and runs the line that needs testing. It returns a list of strings in the following format: Fail on input <input tuple here>: Result was <result> but expected <expected output result>. Success on <input tuple>, output was <expected output result> as expected. The inner workings of this is entirely up to you, you may add additional methods if you see fit (but it doesn't matter test-wise, so do whatever you want).

Python Programming Workshop 2012 Ex2 nd

test_manager.py API TestManager is, surprisingly, a class that manages tests. Specifically, it manages classes of type Test (that means that it has to import that class...). The constructor does not receive any parameters. But it should initialize a few members to help manage the tests and support this API. Which those would be is up to you. append_line receives a number which is the line's id, a string which is a line to be tested and a list of strings which are the names of the arguments that this line relies on. This will create a Test object and append it to whatever structure you think is best. It returns the Test object so that test cases can be added... run_tests runs all the tests in the manager, and prints their results in the following manner:
'#'*60 (run does ;) ) Running test '-'*40 <result ... <result this in the active shell to see what this ID <print the id number here>: of a test case, indented as you see fit> of a test case, indented as you see fit>

tester.py All this file is for, is basically using the TestManager. It receives two filenames as an input arg (again, no need to test for any type of invalid input here, like directories, bad files, etc...) Of course, if someone forgets a parameter, you should print this usage message (spaces in indentation are not important):
Usage: python2.7 tester.py [file to test] [test cases file]

One file includes lines to be tested in the following format:


number| command othernumber| maybe_the_same_command

Meaning, some number, how many ever digits, a pipe symbol '|' and a single python command (note that this goes into an eval() when you run it in its Test class, so it should be a valid input for eval() ). The other file includes the test cases that you wrote in python and pickled (look at the pickle module, ittis cool), so what you create is essentially a big dictionary where the test ID is the key, and the value is a tuple with two fields: (argnames list/tuple, list of test cases) Simple example: pickle this:
d = {1:[('x','y','n'),[([2,5,6],60)] ]}

and test it against the line:


1| x*y*n

Then you feed all this data in your TestManager instance, and feel the tickle in your toes as you get ready to experience test driven development!!! :) (that means, run the tests, among other things). The automatic tester here tests your tarfile and usage message. Nothing more.

Things you should test for, details, comments, hints and (possibly) useful pointers:
Submit your solution as ex2.tar. Your tar file should include test.py and test_manager.py, and tester.py We are using python2.7 If you managed to avoid it thus far, you REALLY have to learn how to use the python interactive shell. Python is duck typed, and you should poll elements on their methods using dir(obj) to speed up development. You may find these useful: globals(), vars(), locals() - read and understand the differences and meanings of each eval() pickle How to write classes in python If you have any questions regarding the exercise, post them in the right place in the forum, after you made sure no one beat you to it.

S-ar putea să vă placă și