Sometimes private functions of a module or class are simply yet-to-be-extracted internal units of functionality, which might deserve their own tests. So why not test them? We will write tests for them later on if/when they're extracted. So why not write the tests now, when they're still part of the same file?
To demonstrate:
First, I wrote module_a
. Now I want to write tests for it. I would like to test the 'private' function _private_func
. I don't understand why I wouldn't write a test for it, if later I might refactor it to its own internal module anyway, and then write tests for it.
Suppose I have a module with the following functions (it could be also be a class):
def public_func(a):
b = _do_stuff(a)
return _do_more_stuff(b)
_do_stuff
and _do_more_stuff
are 'private' functions of the module.
I understand the idea that we should only test the public interface, not the implementation details. However, here's the thing:
_do_stuff
and _do_more_stuff
contain the majority of the functionality of the module. Each one of them could be a public function of a different, 'internal' module. But they are not yet evolved and large enough to be extracted to separate files.
So testing these functions feels right because they are important units of functionality. If they were in different modules as public functions, we would have tested them. So why not test them when they're not yet (or ever) extracted to a different file?