Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python nose test example

I'm just starting to get into test development and I'm struggling to understand what to test. There are a lot of foobar examples out there, but I'm having difficulty knowing how to test my project units. For example, take this function which simple returns the lines of a text file as a list:

def getLines(filename):
    try:
        f = open(filename,'rb')
        lines = f.readlines()
        f.close()
    except:
        break
    return lines

If this was your function, what would you test for? You don't need to write the code, just tell me in broad terms if you like.

Thanks

like image 995
MFB Avatar asked Jun 12 '26 20:06

MFB


1 Answers

So your function would return an empty list if the filename is invalid and would returns a list with all your lines if filename is valid

You could define a KnownValues dictionary with a filenames and number of lines in the file, like so,

file1 -> 20 
file2 -> 30
file3 -> 0 // invalid entry

Then you could write an assertEqual to count the number of items in the list that the function is returning

like image 187
nitin Avatar answered Jun 18 '26 00:06

nitin