The issue is with the function call.
self.assertEqual(True, Calculator.is_peak('9:40'))
This should be like this:
self.assertEqual(True, Calculator().is_peak('9:40'))
Your unittest code should be like this:
class TestCalculator(unittest.TestCase):
def test_peak(self):
# testCase 1 (User enters a valid time in the peak hour)
self.assertEqual(True, Calculator().is_peak('9:40'))
# # testCase 2 (User enters a valid time in the off-peak hour)
self.assertEqual(True, Calculator().is_peak('20:30'))
Explanation:
Why should we use Calculator()
instead of Calculator
?
If we want access full features of a class we have to create a object of a class by using Calculator()
.
When make function call like this Calculator.is_peak()
it consider self
as a parameter but when we call like this Calculator().is_peak()
it consider self
as a class default parameter.
We can see the different behavior of class from below mentions snippets.
print(dir(Calculator.is_peak))
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
print(dir(Calculator().is_peak))
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']