Conditional Publication

Tags: .NET, C#

This is a nice trick I ran into when going over the sourcecode for the XML-RPC.NET library:

#if (DEBUG)
public
#endif
void Serialize(...)

This lets you run your unit tests when developing, but remember to set your methods to private when releasing.

3 Comments

  • Ori Folger said

    Besides ugly, it's also dangerous.
    Your units tests should test the same interface as the one exposed to client code. What's the point of testing methods not exposed to the client?
    If there's some capability that the client code can't test, what is it doing in this class anyway?

  • Avner Kashtan said

    I'm not going to go into the whole "should you unit-test your private methods" argument, which is a long and painful one.
    What I will say is that testing your private methods is a useful feature, even if you don't keep them around to your final tests. I certainly agree with the "you're not testing what you run" problem,

Comments have been disabled for this content.