how to make all assertions in a unit test execute

J

jimgardener

hi
I was testing a function using unittest.

def get_filenames(dirname,innerstring):
....
return filenames_containing_innerstring


class FileNamesTest(unittest.TestCase):
def setUp(self):
self.dirname='/home/me/data'

def test_get_filenames(self):
innerstr='tom'
matching_names=get_filenames(self.dirname,innerstr)
self.assertEquals(1,len(matching_names))

innerstr='myself'
matching_names=get_filenames(self.dirname,innerstr)
self.assertEquals(0,len(matching_names))

innerstr='jerry'
matching_names=get_filenames(self.dirname,innerstr)
self.assertEquals(3,len(matching_names))

when I run the unittest, if the test fails at the first assertion,the
other assertions are not executed at all..How do I organize the test
so that all assertions can run?

thanks
jim
 
P

Peter Otten

jimgardener said:
I was testing a function using unittest.

def get_filenames(dirname,innerstring):
....
return filenames_containing_innerstring
class FileNamesTest(unittest.TestCase):
def setUp(self):
self.dirname='/home/me/data'
when I run the unittest, if the test fails at the first assertion,the
other assertions are not executed at all..How do I organize the test
so that all assertions can run?

Keep it simple ;)

def test_get_filenames_single_match(self):
innerstr='tom'
matching_names=get_filenames(self.dirname,innerstr)
self.assertEquals(1,len(matching_names))
def test_get_filenames_no_match(self):
innerstr='myself'
matching_names=get_filenames(self.dirname,innerstr)
self.assertEquals(0,len(matching_names))
def test_get_filenames_multi_match(self):
innerstr='jerry'
matching_names=get_filenames(self.dirname,innerstr)
self.assertEquals(3,len(matching_names))

Peter
 
T

Terry Reedy

A side point unrelated to the question: your code will be easier to read
if you follow PEP 8, specifically its recommendations regarding spaces
around syntax (like ‘=’ and ‘,’).


Yes, that's by design. Each function in a TestCase subclass is a test
case; the function goes about testing *one thing*, and ends as soon as
the test passes or fails.


Every test case should test exactly one true-or-false question. Name
each test case after the assertion it's making, so the unittest output
makes sense.

class get_filenames_TestCase(unittest.TestCase):
def setUp(self):
self.dirname = '/home/me/data'

def test_one_match_returns_one_item(self):
innerstr = 'tom'
matching_names = get_filenames(self.dirname, innerstr)
self.assertEquals(1, len(matching_names))

def test_no_match_returns_empty_sequence(self):
innerstr = 'myself'
matching_names = get_filenames(self.dirname, innerstr)
self.assertEquals(0, len(matching_names))

def test_three_matches_returns_three_items(self):
innerstr = 'jerry'
matching_names = get_filenames(self.dirname, innerstr)
self.assertEquals(3, len(matching_names))

The temptation to write unfactored duplicate code like this is a
negative of unittest. The question is whether get_filenames gets the
proper number of names. One way to do it with unittest is something like

def test_get_filenames_return_number(self):
inputs = ('myself', 'tom', 'jerry')
outputs= ( 0, 1, 3)
self.assertEquals(outputs,
[len(get_filenames(self.dirname,f) for f in inputs])

Listing inputs and outputs first makes it easy to check pairs and revise
if needed.

With hundreds or thousands of i/o pairs, something different would be
needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top