I am trying to design and test code similar to the following in a good object oriented way? (Or a pythonic way?)
Here is a factory class which decides whether a person's name is long or short:
class NameLengthEvaluator(object):
def __init__(self, cutoff=10)
self.cutoff = cutoff
def evaluate(self, name):
if len(self.name) > cutoff:
return 'long'
else:
return 'short'
Here is a person class with an opinion on the length of their own name:
class Person(object):
def __init__(self, name=None, long_name_opinion=8):
self.name = name
def name_length_opinion(self):
return 'My names is ' + \
NameLengthEvaluator(long_name_opinion).evaluate(self.name)
A couple questions:
- Does the
Person
methodname_length_opinion()
deserve a unit test, and if so what would it look like? - In general, is there a good way to test simple methods of classes with functionality that is entirely external?
It seems like any test for this method would just restate its implementation, and that the test would just exist to confirm that nobody was touching the code.
(disclaimer: code is untested and I am new to python)