Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do unit testing of functions writing files using Python's 'unittest'

I have a Python function that writes an output file to disk.

I want to write a unit test for it using Python's unittest module.

How should I assert equality of files? I would like to get an error if the file content differs from the expected one + list of differences. As in the output of the Unix diff command.

Is there an official or recommended way of doing that?

like image 466
jan Avatar asked Oct 15 '10 13:10

jan


People also ask

How do you write a unit test file in Python?

Unit tests are usually written as a separate code in a different file, and there could be different naming conventions that you could follow. You could either write the name of the unit test file as the name of the code/unit + test separated by an underscore or test + name of the code/unit separated by an underscore.

Which function in Unittest will run all your tests in Python?

TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.


2 Answers

I prefer to have output functions explicitly accept a file handle (or file-like object), rather than accept a file name and opening the file themselves. This way, I can pass a StringIO object to the output function in my unit test, then .read() the contents back from that StringIO object (after a .seek(0) call) and compare with my expected output.

For example, we would transition code like this

##File:lamb.py import sys   def write_lamb(outfile_path):     with open(outfile_path, 'w') as outfile:         outfile.write("Mary had a little lamb.\n")   if __name__ == '__main__':     write_lamb(sys.argv[1])    ##File test_lamb.py import unittest import tempfile  import lamb   class LambTests(unittest.TestCase):     def test_lamb_output(self):         outfile_path = tempfile.mkstemp()[1]         try:             lamb.write_lamb(outfile_path)             contents = open(tempfile_path).read()         finally:             # NOTE: To retain the tempfile if the test fails, remove             # the try-finally clauses             os.remove(outfile_path)         self.assertEqual(contents, "Mary had a little lamb.\n") 

to code like this

##File:lamb.py import sys   def write_lamb(outfile):     outfile.write("Mary had a little lamb.\n")   if __name__ == '__main__':     with open(sys.argv[1], 'w') as outfile:         write_lamb(outfile)    ##File test_lamb.py import unittest from io import StringIO  import lamb   class LambTests(unittest.TestCase):     def test_lamb_output(self):         outfile = StringIO()         # NOTE: Alternatively, for Python 2.6+, you can use         # tempfile.SpooledTemporaryFile, e.g.,         #outfile = tempfile.SpooledTemporaryFile(10 ** 9)         lamb.write_lamb(outfile)         outfile.seek(0)         content = outfile.read()         self.assertEqual(content, "Mary had a little lamb.\n") 

This approach has the added benefit of making your output function more flexible if, for instance, you decide you don't want to write to a file, but some other buffer, since it will accept all file-like objects.

Note that using StringIO assumes the contents of the test output can fit into main memory. For very large output, you can use a temporary file approach (e.g., tempfile.SpooledTemporaryFile).

like image 140
gotgenes Avatar answered Sep 25 '22 05:09

gotgenes


The simplest thing is to write the output file, then read its contents, read the contents of the gold (expected) file, and compare them with simple string equality. If they are the same, delete the output file. If they are different, raise an assertion.

This way, when the tests are done, every failed test will be represented with an output file, and you can use a third-party tool to diff them against the gold files (Beyond Compare is wonderful for this).

If you really want to provide your own diff output, remember that the Python stdlib has the difflib module. The new unittest support in Python 3.1 includes an assertMultiLineEqual method that uses it to show diffs, similar to this:

    def assertMultiLineEqual(self, first, second, msg=None):         """Assert that two multi-line strings are equal.          If they aren't, show a nice diff.          """         self.assertTrue(isinstance(first, str),                 'First argument is not a string')         self.assertTrue(isinstance(second, str),                 'Second argument is not a string')          if first != second:             message = ''.join(difflib.ndiff(first.splitlines(True),                                                 second.splitlines(True)))             if msg:                 message += " : " + msg             self.fail("Multi-line strings are unequal:\n" + message) 
like image 24
Ned Batchelder Avatar answered Sep 22 '22 05:09

Ned Batchelder