I have a module which parses different file formats like CSV, XLS, HTML, etc.
The different formats contains exactly the same information, so I made different classes for parsing different file types. These classes have the exact same public API, and obviously have the same base class like this:
class BaseParser(object):
def countTotalValues(self):
pass
def countItems(self):
pass
class CSVParser(BaseParser):
def __init__(self):
"""Init data, check for errors, etc.."""
def parse(self):
""" parses the data and set the result dictionaries like self.values, self.items, etc."""
class XLSParser(BaseParser):
""" Ha exactly the same public API as CSVParser and set the same inner values. """
pass
Is it a good idea to unit test each of the classes at once with the same values ? I have been doing this:
class TestParserClasses(unittest.TestCase):
def setUp(self):
self.instances = []
for class_, file_ in PARSER_CLASSES:
self.instances.append(class_(file_))
def tearDown(self):
del self.instances
and define every unit test this way:
def test_count_total_values_without_parameter(self):
for parser in self.instances:
parser.parse()
self.assertEqual(Decimal('9216.84'), parser.countTotalValues())
def test_count_items_without_parameter(self):
for parser in self.instances:
parser.parse()
self.assertEqual(128, parser.countItems())
Is it okay to do this? If yes, how to unit test all of them with the same TestCases at once without
for parser in self.instances:
parser.parse()
So I want to write unittest like this:
def test_count_total_values_without_parameter(self):
self.assertEqual(Decimal('9216.84'), parser.countTotalValues())
def test_count_items_without_parameter(self):
self.assertEqual(128, parser.countItems())
applied to every class. Is it possible ?