unittest of file-reading function

Helge Stenstroem helge.stenstrom.NOSPAM at NOSPAMericsson.com
Tue Oct 18 06:48:49 EDT 2005


Say I have a function

def f(filename):
    result = openFileAndProcessContents(filename)
    return result

Can that function be unit tested without having a real file as input?
Something along the lines of

import unittest
class tests(unittest.TestCase):
    def test1(self):
        fileContents = "bla bla bla\nmore bla bla bla"
        ???   # make f read this string instead of opening a file
        expected = expectedResult
        result = f(filename)
        self.assertEqual(result, expected)

One possibility would be to make the unit test write to a temporary
file. Are there better alternatives?
        

-- 
Helge Stenström



More information about the Python-list mailing list