I have a class that defines a private (well, __container
to be exact since it is python) container. I am using the information within said container as part of the logic of what the class does and have the ability to add/delete the elements of said container.
For unit tests, I need to populate this container with some data. That data depends on the test done and thus putting it all in setUp() would be impractical and bloated -- plus it could add unwanted side effects.
Since the data is private, I can only add things via the public interface of the object. This runs code that need not be run during a unit test and in some cases is just a copy and paste from another test.
Currently, I am mocking the whole container but somehow it does not feel that elegant a solution. Due to Python mocking frame work (mock), this requires the container to be public -- so I can use patch.dict()
. I would rather keep that data private.
What pattern can one use to still populate the containers without excercising the public method so I have data to test with?
Is there a way to do this with mock's patch.dict()
that I missed?
_private
members and__name_mangeled
ones as well (the latter requires knowledge of the name mangling algorithm though, and is even more fragile). That doesn't mean you should though.patch.object(sut._ClassName__container, {...})
and that would do it. That would be a solution. If you wanted to write it up, I'd upvote you...