Search code examples
pythonunit-testinginheritanceabstract-classinit

Clarity on Testing subclass init method


I am new to Object-oriented. I am trying to test my RightPyramid init method, not sure if there's a way I can do this as my RightPyramid init doesn't have any arguments.

from typing import List
from abc import ABCMeta, abstractmethod

class Triangle(metaclass=ABCMeta):
    def __init__(self, base: int, height: int) -> None:
        assert base >= 0, "base must be greater or equal to 0"
        assert height >= 0, "height must be greater or equal to 0"
        self.base = base
        self.height = height

    @abstractmethod
    def area(self) -> int:   
        pass


class RightPyramid(Triangle):
    def __init__(self):
        Triangle.__init__(self, 3, 2)

    def area(self):
        return 0.5 * self.base + self.height

Solution

  • If instead you want to test your Triangle base class behavior you should create new class :

    class TestInit(Triangle):
        def __init__(self):
            Triangle.__init__(self, -3, 2) # this should throw an assertion fail
    
    t = TestInit()