Reputation: 26061
I'm trying to expose my private method to unit testing. I saw this example in UnitTesting Succintly:
public class DoesSomething
{
#if TEST
public
#else
private
#endif
void SomeComputation() {
}
}
Here is my attempted at it but the code still appears private when trying to access via test.
#if TEST
public
#else
private
#endif
Organization TransformToOrganization(OrgChartOrganizationUnitType org)
{
...
}
Upvotes: 2
Views: 6057
Reputation: 218798
I'm trying to expose my private method to unit testing.
You shouldn't. Private methods are just that, private. They are fully encapsulated within the class which contains them and not part of the external interface of that class. The unit tests should validate the external-interface (or externally visible functionality) of the class, not its private internal implementation.
To put it another way, writing tests for private methods tightly couples the tests to the specific implementations of those objects.
The private methods should be tested indirectly when they are used by the public-facing functionality of the objects. If they're not covered in those tests, you have two options:
Upvotes: 2
Reputation: 241583
You shouldn't need to expose private methods to your tests. The public methods of your class have a specification. Your unit tests should be testing that the public methods of your class adhere to that specification. How the public methods go about doing that is no one's business; you want to be free to change your private implementation however you like as long as you continue to adhere to the specification of your public methods.
If you write unit tests that depend on the private implementation, you end up with incredibly brittle unit tests that don't actually add any value because the only thing that you need to validate is that the publicly exposed members adhere to their specification. If there are little kittens running around hidden in the background making the whole thing work, it doesn't matter.
Upvotes: 15
Reputation: 8916
You have to change your TEST compile constant properly. That's not one that's built into to VS compile constants that get switched on/off for Debug/Release configurations.
Upvotes: 2