Reputation: 11
I am trying to test the calculator program using the python unittest on the following test scenarios. But when I try to instantiate the "CalculatorClass" using the init it gives the following error message in the bottom. I want to use init for instantiating. I am using Visual Studio Code. Could anyone help me with this?
This is the code of "Calculator_test.py"
import unittest
import Calculator_steps as CalculatorClass
class TestCalculator(unittest.TestCase):
def __init__(self):
self.calculator = CalculatorClass.Calculator() # instantiate the CalculatorClass
def test_DecimalValues(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('3.55,8.76')
def test_NonNumeric(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('7,8,e')
def test_MoreThanThreeIntegers(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('5,7,2,8')
def test_MoreThan100(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('109,76,90')
Following is the code of Calculator_steps.py
class Calculator:
def addNumbers(self, numberlist):
total = 0
if numberlist=="":
numberlist ="0"
numbers = numberlist.split(",")
if len(numbers)>3:
raise TypeError("Only three values can be entered")
for i in range(len(numbers)):
if int(numbers[i])>100:
raise TypeError("Only values less than 100 is allowed")
try:
total = total +int(numbers[i])
except ValueError:
TypeError("Only integers can be entered")
return total
Following is the error message
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\mudit> py -m unittest Calculator_tests.py
Traceback (most recent call last):
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\__main__.py", line 18, in <module>
main(module=None)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\main.py", line 100, in __init__
self.parseArgs(argv)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\main.py", line 147, in parseArgs
self.createTests()
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\main.py", line 158, in createTests
self.test = self.testLoader.loadTestsFromNames(self.testNames,
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 220, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 220, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 191, in loadTestsFromName
return self.loadTestsFromModule(obj)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 124, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 93, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\suite.py", line 24, in __init__
self.addTests(tests)
File "C:\Users\mudit\AppData\Local\Programs\Python\Python310\lib\unittest\suite.py", line 57, in addTests
for test in tests:
TypeError: TestCalculator.__init__() takes 1 positional argument but 2 were given
Upvotes: 0
Views: 88
Reputation: 7796
When inheriting from unittest.TestCase
, you do cannot overwrite the __init__
method. Instead, anything you want to do before each test runs can be put in the setUp
method, like so:
import unittest
import Calculator_steps as CalculatorClass
class TestCalculator(unittest.TestCase):
def setUp(self):
self.calculator = CalculatorClass.Calculator() # instantiate the CalculatorClass
def test_DecimalValues(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('3.55,8.76')
def test_NonNumeric(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('7,8,e')
def test_MoreThanThreeIntegers(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('5,7,2,8')
def test_MoreThan100(self):
with self.assertRaises(Exception):
self.calculator.addNumbers('109,76,90')
To expand, the unitttest
module is designed in such a way that special things happen in the __init__
method of unittest.TestCase
, and the module does not support overwriting the __init__
method. In theory, you could overwrite the __init__
method if you call super().__init__
at some point during the overwritten method, but why would you want to? The unittest module is designed so that you should never need to overwrite the __init__
method. You can use the setUp
and setUpClass
methods to do any instantiating needed before running your tests
Upvotes: 2