Unit Testing Private Methods Issues

In MSTest, the attribute [Shadowing]

helps unit test a private method from another assembly. Here is the link: What is the Shadowing attribute that VS uses when creating unit tests?

My questions:

  • Should private methods be tested separately separately?
  • Is it good practice (?) To change the private application's accessor method internal

    to make it available for unit testing in some other test project / assembly? (using InternalsVisibleTo

    )
  • If private methods are tested indirectly through the public method that calls them, can this be called "unit" testing?
+3


source to share


4 answers


  • No, private methods should not be validated. Your application will only interact with the public API. Therefore, you should check the expected behavior of your class for this interaction. Private methods are part of the internal logic implementation. Users of your class shouldn't care how this is implemented.
  • No, this is not good. See above. You should only check the public API.
  • You should only check public methods. You don't care if the public method calls the private method or not until the test passes. If the test fails, fix it. But don't test personal methods.


UPDATE (how to determine what to test): Ideally (in test-first approach) test is the first user of your class. When you write a test, you are trying to imagine how users will use your class. Users won't interact with private methods (Reflection is cheating). So, your test, as the first user of your class, shouldn't interact with private methods.

+4


source


To answer your questions briefly:

  • In general, they shouldn't. Most of the time your private bits will be checked when testing the class / public API contract. For a time this is not possible, testing a private method is a unit test like everything else.

  • This is quite common. While changing the visibility may be considered a bad idea, it is not so bad when it only changes internally. However, in approaches like TDD, the need for testing usually stimulates your behavior in such a way that hacks like this are not needed. But, as I said, it's pretty common - you shouldn't worry too much about it, unless it gets to ridiculous levels (say, all private parts of classes).

  • It is a unit test if it tests a separate unit (or one logical concept ) of your class. Private methods are most often created as a result of refactoring in the public parts, which in most cases will focus on unit testing. If you think your private method is no longer a unit, it might be a refactoring call.



Also, I suggest looking here , here and.

+2


source


I would rather use all private methods through public calls. There must be a path to execute each private line from the open call, and if not, you can remove that code.

Using internal rather than private can end up in a big mess, I won't use this approach.

+1


source


As a Java developer, I do this. I also don't change access levels. I am using reflection to access private methods. I don't need to provide it to my users, and I don't need to show them my unit tests.

This is Java's dirty secret: you can always get around reflection access restrictions. I don't know if this also applies to C # and .NET, but you can browse it to see.

0


source







All Articles