import unittest
import filterList
class TestFilterList(unittest.TestCase):
""" docstring for TestFilterList
"""
def setUp(self):
self._filterby = 'B'
def test_checkListItem(self):
self.flObj = filterList.FilterList(['hello', 'Boy', 1], self._filterby)
self.assertRaises(filterList.ItemNotString, self.flObj.checkListItem)
def test_filterList(self):
self.flObj = filterList.FilterList(['hello', 'Boy'], self._filterby)
self.assertEquals(['Boy'], self.flObj.filterList())
if __name__ == '__main__':
unittest.main()
my above test test_checkListItem() fails , for the below filterList.py module
import sys
import ast
class ItemNotString(Exception):
pass
class FilterList(object):
""" docstring for FilterList
"""
def __init__(self, lst, filterby):
super(FilterList, self).__init__()
self.lst = lst
self._filter = filterby
self.checkListItem()
def checkListItem(self):
for index, item in enumerate(self.lst):
if type(item) == str:
continue
else:
raise ItemNotString("%i item '%s' is not of type string" % (index+1, item))
print self.filterList()
return True
def filterList(self):
filteredList = []
for eachItem in self.lst:
if eachItem.startswith(self._filter):
filteredList.append(eachItem)
return filteredList
if __name__ == "__main__":
try:
filterby = sys.argv[2]
except IndexError:
filterby = 'H'
flObj = FilterList(ast.literal_eval(sys.argv[1]), filterby)
why does the test fail with the error:
E['Boy']
.
======================================================================
ERROR: test_checkListItem (__main__.TestFilterList)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_filterList.py", line 13, in test_checkListItem
self.flObj = filterList.FilterList(['hello', 'Boy', 1], self._filterby)
File "/Users/sanjeevkumar/Development/python/filterList.py", line 16, in __init__
self.checkListItem()
File "/Users/sanjeevkumar/Development/python/filterList.py", line 23, in checkListItem
raise ItemNotString("%i item '%s' is not of type string" % (index+1, item))
ItemNotString: 3 item '1' is not of type string
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (errors=1)
also, is the approach of the filterList.py module correct ?