In case anyone else is still looking, our company developed an open source Python package to satisfy this need. Hope it helps!
Clone the project
git clone https://github.com/amentumspace/file_unittest
Installation
The package can be installed into any active environment by running:
cd file_unittest/
pip install .
file_unittest.TestCase
This class extends the unittest.TestCase class to allow the user to
run unit tests by writing data to a file and verifying the output
has not changed since the last time the unit test was run.
Once the user is satisfied that the output data is
correct, these unit test simply ensure that the data does not change with changes in the code base.
Usage
Rather than inherit from unittest.TestCase the user should
derive a class from file_unittest.TestCase.
Like the normal unit test, individual test functions
are appended with test_.
Any desired output data is written to file by calling self.output
Example unit test file:
import unittest
import file_unittest
class MyTest(file_unittest.TestCase):
def test_case(self):
self.output("hello")
if __name__ == '__main__':
unittest.main()
Unlike the normal unittest.TestCase the user does not need to
make any assertion calls eg. self.assertTrue
, self.assertFalse
etc.
This class will take care of warning about any differences in output.
Default output location
The default location to output the test results will be:
{derived_class_filepath}/test_results/
{derived_class_filename}.{derived_class_name}.{test_name}.txt
Missing or different results
If the expected output file is missing, or differences are detected,
the output data will be written to the same file, but with .new postfix:
{derived_class_filepath}/test_results/
{derived_class_filename}.{derived_class_name}.{test_name}.new
On the first run, the user will need to inspect the .new file for expected
results;
Otherwise, if differences are detected, the user can diff the
.txt and .new files to investigate and resolve any differences;
In both cases once the user is satisfied with the results, the .new files can
be renamed with .txt extension and the .txt files can be checked into the git repo
Committing test result files to source control
Once the output .txt files have been satisfactorily generated as in the previous step, they should be checked into source control so that they can be used as the benchmark for future runs.