Is unit testing private methods a good practice?

后端 未结 4 2039
青春惊慌失措
青春惊慌失措 2020-12-09 19:03

I am wondering if unit testing private methods is a good practice?

Normally only public interface should be tested.

However, I have found out that during com

4条回答
  •  温柔的废话
    2020-12-09 20:06

    It's not a good practice (yet that doesn't mean you should never do that), and if possible you want to avoid it. Testing private method usually means your design could be better. Let's take a quick look at your player example:

    • moveToFilePos: sounds more like a responsibility of something doing I\O operations, not a music player's
    • fillBuffers: more of a memory manager's job rather than music player
    • checkIfValidTimeRange: again, probably could be moved out of player's scope to some simple validation class (seems like this one might be useful in other places aswell)

    At the moment your music player does I/O, memory management and what not else. Is that all really in scope of its responsibilities?

提交回复
热议问题