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 i
execute UUT on the input and save output of run
Load expected result for test case i
Compare 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
setUpmethod to do the “Load input for test case i”, and “Load expected result for test case i”. Write yourtestmethod to exercise the UUT.It might look like this.
Want to run this many times? One way it to do something like this.
This will allow the unittest main program to find and run your tests.