This question has already been answered:

I have 2 projects in solution. In one class where there are private methods that need to be tested. Another project is unit tests. I know that I can use friendly builds to access the private methods of that class. Tell me how to do this? It is desirable, as detailed as possible.

Reported as a duplicate by the participants αλεχολυτ , vp_arth , ThisMan , Denis Bubnov , kmv 7 Mar '17 at 12:52 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

1 answer 1

Unit tests usually cover the publicly visible part. For tests and for a “user” (a programmer using your api) class is a black box — something is passed to the input, something is received at the output, and this is being tested.

If you have a need to test private methods, you may be violating the single responsibility principle. Try decomposition, select an additional class and access it yourself (and test it) on the black box principle. And private members are its internal state, which should not be interesting outside this class.

  • 2
    “According to the black box principle” if we are talking about unit tests, then this is still a white-box-test. If external dependence is highlighted (within the class, access to the database, network, file system, etc.) but this class is better to test at the level of integration / system / acceptance tests, rather than at the level of unit tests. - Artyom Okonechnikov
  • Yes, I agree, rather a "white box". - Aleksei