I have a class called Helper
which contains a method called create_list
. The tests I've written for the create_list
method include a case where a TypeError
is supposed to be raised if any of the values doesn't match the type expected.
The class is below:
class Helper(object):
def create_list(self, hel_id, hel_name, hel_type, hel_desc):
if not isinstance(hel_id, int):
raise TypeError
elif not isinstance(hel_name, str):
raise TypeError
elif not isinstance(hel_type, str):
raise TypeError
elif not isinstance(hel_desc, str):
raise TypeError
and the test which I've written is below:
import unittest
import Helper
class TestHelp(unittest.TestCase):
"""Unit Tests for the Helper class"""
def setUp(self):
self.helper = Helper()
def test_create_list_raise_type_error(self):
with self.assertRaises(TypeError):
self.helper.create_list('', 'somename', 'sometype', 'somedescription')
self.helper.create_list(1, 1, 'sometype', 'somedescription')
self.helper.create_list(1, 'somename', 1, 'somedescription')
self.helper.create_list(1, 'somename', 'sometype', 1)
The problem is that the tests pass when the first if isinstance
check is added to the Helper
class. Commenting out the other checks doesn't make the test fail. How can I write the test (or the code, if that's the issue) such that it only passes when all the conditions are met i.e. a TypeError
is raised for each invalid input.