I am trying to do a simple test in Python using ‘unittest‘, to see if a class throws an exception if it gets an unsuitable input for the constructor. The class looks like this:
class SummaryFormula:
def __init__( self, summaryFormula):
self.atoms = {}
for atom in re.finditer( "([A-Z][a-z]{0,2})(\d*)", summaryFormula):
symbol = atom.group(1)
count = atom.group(2)
if pocet != "":
self.atoms[ symbol] = int(count)
else:
self.atoms[ symbol] = 1
My test is the following:
class ConstructorTestCase(unittest.TestCase):
def testEmptyString(self):
self.assertRaises(TypeError, ukol1.SummaryFormula(), "testtest")
if __name__ == '__main__':
unittest.main()
All I want is the test to fail, meaning that the exception of unsuitable input for constructor is not handled.
Instead, I get an error: __init__() takes exactly two arguments (1 given).
What am I missing? What is the second argument I should specify?
Also, what type of Error should I use to handle exception that an input not matchable by my regexp was passed to the constructor?
That’s because your class requires a parameter while instantiating the object.
While you are passing
You should have been passing the parameter summaryFormula to it.
Also the confusion is because your class name is SummaryFormula and the parameter that you pass to
__init__is also SummaryFormulaOr it should this be