As part of some Python tests using the unittest
framework, I need to compare two relatively short text files, where the one is a test output file and the other is a reference file.
The immediate approach is:
import filecmp ... self.assertTrue(filecmp.cmp(tst_path, ref_path, shallow=False))
It works fine if the test passes, but in the even of failure, there is not much help in the output:
AssertionError: False is not true
Is there a better way of comparing two files as part of the unittest
framework, so some useful output is generated in case of mismatch?
#1) cmp: This command is used to compare two files character by character. Example: Add write permission for user, group and others for file1. #2) comm: This command is used to compare two sorted files. One set of options allows selection of 'columns' to suppress.
To get a report of which line has a difference, and a printout of that line, use assertListEqual
on the contents, e.g
import io self.assertListEqual( list(io.open(tst_path)), list(io.open(ref_path)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With