How do you test a private method?
Quick Answer
You generally don't test private methods directly — you test them through the public method that calls them. If a private method is complex enough to want its own tests, that's a sign it should be its own class with a public interface. Testing privates via reflection couples tests to implementation.
I don't test private methods directly. They're implementation details, and I get to them through the public method that uses them — if I exercise all the public scenarios, the private logic is covered. If a private method is so involved that I really want dedicated tests for it, that's telling me it wants to be its own class with its own public interface, and pulling it out usually makes the design better too. Using reflection to poke at privates just welds the tests to the internal structure.
- Private methods are implementation details — a refactor that keeps public behaviour should be free.
- “I can't reach this branch through the public API” often means dead code or a missing public scenario.
- If extraction is genuine, the new class's dependencies become injectable and the whole thing gets cleaner.
internal+[InternalsVisibleTo("Tests")]is a middle ground for a helper you don't want fully public — but prefer proper extraction.