How to populate a private container for unit test?

Posted by Sardathrion on Programmers See other posts from Programmers or by Sardathrion
Published on 2011-11-15T17:02:21Z Indexed on 2011/11/15 18:07 UTC
Read the original article Hit count: 177

Filed under:
|
|
|

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 date 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 run codes that need not be run during a unit test and in some case 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 excising the public method so I have data to test with?

Is there a way to do this with mock' patch.dict() that I missed?

© Programmers or respective owner

Related posts about python

Related posts about testing