This package enables the creation and execution of unit tests based on text-based files, also known as golden testing. Its primary function is to facilitate the generation of text files (golden files) that are regenerated and compared against whenever there are changes in the codebase.
Install this package using pip by executing the following commands in any active conda environment:
cd file_unittest/
pip install .Derived from unittest.TestCase, this class allows file-based unit testing. Users confirm the correctness of output data by writing to files and verifying that subsequent outputs remain unchanged despite codebase modifications.
Instead of inheriting from unittest.TestCase, extend file_unittest.TestCase and define test functions prefixed with test_. For example:
import unittest
import file_unittest
class MyTest(file_unittest.TestCase):
def test_case(self):
self.output("hello")
if __name__ == '__main__':
unittest.main()This approach eliminates the need for explicit assertions like self.assertTrue or self.assertFalse. The class handles discrepancies in output internally.
Output files are stored in the following directory structure:
{derived_class_filepath}/test_results/
{derived_class_filename}.{derived_class_name}.{test_name}.txt
If the expected output file is missing or differences are detected, the output is saved with a .new postfix. The user must verify these .new files and, if correct, rename them to .txt to serve as new benchmarks.
Once the output .txt files meet expectations, commit them to your version control system to establish baselines for future tests.
Due to potential minor discrepancies caused by code changes (like operation order modifications), it's advisable to control output precision when generating files:
for i in range(len(midpoint_dates)):
self.output(f'{midpoint_dates[i]:.6f}, {optWs[i]:.6e}, {residuals[i]:.6e}')To prevent test results from displaying on the screen, adjust the setUp() method:
class MyTest(file_unittest.TestCase):
def setUp(self):
self.print_to_screen = False