profile picture

How to test private methods?

Testing private methods. When and how?

October 20, 2023 - 168 words - 1 min Found a typo? Edit me
testing software design


This is a question that I have encountered with some frequency for a long time. So I thought I would put together my thoughts on the subject here.

Short answer


Long answer

Never ever.

What if…?

If you really want to test a private method, consider extracting that private method logic into a separate class, and write a unit test for that class’ behavior.

For this one, I was inspired by Fran Iglesias’ original post.