H
Helge Stenstroem
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?
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?