-2

I can't seem to figure out why my unittest is not running. enter image description here

I am trying to test the methods in calculator class:

class Calculator():

    def __init__(self):
        pass

    def is_peak(self, start_time: str):
        time = start_time.split(':') 
        hour = time[0]
        int_hour = int(hour)
        if 6 <= int_hour < 18:
            return True
        else:
            return False

This is the testcase for unittest:

class TestCalculator(unittest.TestCase):

    def test_time(self):
        self.assertEqual(True, Calculator.is_time('9:40'))

2 Answers2

1

The method is_peak() should be a static method since it is called without initializing the class.

Since no object is initialized, the self parameter in is_peak() becomes just like any other. This is why you're getting the positional argument error.

Try this in your class instead:

class Calculator():
    def __init__(self):
        pass

    @staticmethod
    def is_peak(start_time: str):
        time = start_time.split(':')  # Extracts the hour character from the string
        hour = time[0]
        int_hour = int(hour)
        if 6 <= int_hour < 18:
            return True
        else:
            return False

Related question: Static methods in Python?

Scene
  • 489
  • 4
  • 16
0

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__']

Sabil
  • 3,750
  • 1
  • 5
  • 16