jeudi 25 février 2021

Testing Python objects with complicated initialization

I find that I often want to have Python objects with complicated initialization:

class Foo(object):
    def __init__(self, thing):
        self.complicated_processing_A(thing)
        self.complicated_processing_B(thing)

In my code, I then create the object and all the complicated processing is done:

f = Foo(thing)

This is great except that testing the Foo class is really annoying because you need to do a lot of mocking.

Another option is to change __init__ so that it doesn't do much but then when I use the class, I need to carry out the extra steps:

f = Foo(thing)
f.complicated_processing_A(thing)
f.complicated_processing_B(thing)

Is there a good way to hide the details of a complicated class but also have clean testing code?

Aucun commentaire:

Enregistrer un commentaire