Reputation: 19837
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
Upvotes: 3
Views: 2139
Reputation: 1
In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual function or procedure. Unit tests are created by programmers or occasionally by white box testers.
Your function is used to statistical documents the number of rows,Input parameter is a file object,so you can prepare for the different number of rows of the file as input,Then you could write an assertEqual to count the number of items in the list that the function is returning.
In addition you must also be checked exceptions
Upvotes: -2
Reputation: 7358
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
Upvotes: 5