I am currently working on a small project in Python. I commonly structure my classes with some public methods that are documented but mainly deal with the high level concepts (what a user of the class should know and use), and a bunch of hidden (starting with underscore) methods which are in charge of the complex or low level processing.
I know that tests are essential to give confidence in the code and to ensure that any later modification has not broken the previous behaviour.
In order to build the higher level public methods on a trusted base, I generally test the private methods. I find it easier to find whether a code modification has introduced regressions and where. It means that those internal tests can breake on minor revisions and will need to be fixed/replaced
But I also know that unit testing private method is at least a disputed concept or more often considered as bad practice. The reason being: only public behaviour should be tested (ref.)
I do care about following best practices and would like to understand:
- why is using unit tests on private/hidden methods bad (what is the risk)?
- what are the best practices when the public methods can use low level and/or complex processing ?
- it is not a how to question. Python has not true concept of privacy and hidden methods are simply not listed but can be used when you know their name
- I have never been taught programming rules and patterns: my last classes are from the 80's... I have mainly learned languages by trial and failure and references on Internet (Stack Exchange being my favourite for years)