Reputation: 1471
Is there any library that allows you to diff files in C++ unit tests? Ideally this would work with Boost Unit tests.
I was thinking of a function along the lines of:
CHECK_FILES_EQUAL('output.txt', 'reference.txt');
Which would then fail the test if the files were equal (possibly showing the line that it failed on).
Thanks
Upvotes: 6
Views: 1746
Reputation: 37707
Well behaving unit test should not write anything in filesystem. If you are using STL streams then you should use abstraction std::ostream
and then when testing feed std::ostringstream
in place of this abstraction.
Just read whole file into a string and compare it. GTest will notice large strings and will print proper diff.
Upvotes: 0
Reputation: 1603
You can use ApprovalTests to create tests which compare some output file to some approved output. If they differ it can display it in a diff tool e.g. kdiff, and you can choose to accept the changes and update your approved output or reject the test.
Upvotes: 0
Reputation: 7940
I assume you want something more intelligent that just checking if the files equal byte-by-byte. I would use google-diff-match-patch, a powerful library which can (among other functionality) calculate a diff between two files. A C++ implementation is available, along with other languages. You'll need to handle file IO yourself, though.
Upvotes: 2