I need to create a unit-test for some python class. I have a database of inputs and expected results which should be generated by the UUT for those inputs.
Here is the pseudo-code of what I want to do:
for i=1 to NUM_TEST_CASES:Load input for test case iexecute UUT on the input and save output of runLoad expected result for test case iCompare output of run with the expected result
Can I achieve this using the unittest package or is there some better testing package for this purpose?
The way you describe testing is an odd match for Unit Testing in general. Unit testing does not -- typically -- load test data or rest results from external files. Generally, it's simply hard-coded in the unit test.
That's not to say that your plan won't work. It's just to say that it's atypical.
You have two choices.
(What we do). Write a little script that does the "Load input for test case i", and "Load expected result for test case i". Use this to generate the required unittest code. (We use Jinja2 templates to write Python code from source files.)
Then delete the source files. Yes, delete them. They'll only confuse you.
What you have left is proper Unittest files in the "typical" form with static data for the test case and expected results.
Write your setUp
method to do the "Load input for test case i", and "Load expected result for test case i". Write your test
method to exercise the UUT.
It might look like this.
class OurTest( unittest.TestCase ):def setUp( self ):self.load_data()self.load_results()self.uut = ... UUT ...def runTest( self ):... exercise UUT with source data ...... check results, using self.assertXXX methods ...
Want to run this many times? One way it to do something like this.
class Test1( OurTest ):source_file = 'this'result_file = 'that'class Test2( OutTest ):source_file= 'foo'result_file= 'bar'
This will allow the unittest main program to find and run your tests.