lundi 29 décembre 2014

Should I unittest private/protected method

This is actually language agnostic. But I'll give you context in python.


I have this parent class



class Mamal(object):
def __init__(self):
""" do some work """

def eat(self, food):
"""Eat the food"""
way_to_eat = self._eating_method()
self._consume(food)

def _eating_method(self):
"""Template method"""

def _consume(self, food):
"""Template method"""


Here eat is the only public method and _consume, _eating_method are actually protected method which will be implemented by child classes.


What will you test when you have written only the Mamal class?


Obviously all 4 methods.


Now lets introduce a child



class Whale(Mamal):
def _eating_method(self):
"""Template method"""

def _consume(self, food):
"""Template method"""


Look at this class. it has only 2 protected method.


Should I test all 4 methods of Whale (including 2 inherited) or just test the changes introduced (only overrided 2 methods)?


What is the ideal case?


Aucun commentaire:

Enregistrer un commentaire