mardi 21 juin 2016

how to write a proper unit test for file transformation

I'm not very familiar with unit testing, I just start with learning how to do it. Maybe I haven't really understood the principles of unit testing and TDD and that's the reason why I'm feeling stuck.

The code to test looks like this:

class DevelopingMyClass:
    def __init__(self):
        self.firstAttribute = None

    def openGivenFile(self, filename):
        f = open(filename, 'r')
        return f

    def transformInput(self, filename):
        res = self.openGivenFile(filename)
        for line in res.readlines():
            newLine = line.replace('a', 'z')
            print(newLine)

# start
myClass = DevelopingMyClass()
myClass.transformInput('testfile.txt')

Of course, this program does not make any sense. I was just trying to figure out:

  • is this testable? and if yes, what exactly makes it testable?
  • do I have to write two test cases, one for the opening method and one for the transformation?
  • do I have to mock the opening, so that I am independent of the existence of a testfile?

Aucun commentaire:

Enregistrer un commentaire