- You can use special classes that inherit classes with private (now protected) methods and methods that are called invisible from outside protected methods.
This is called a test specific subclass or test specific extension in the large test refactoring code book of http://xunitpatterns.com/
You can read more detailed information and ideas for testing personal methods here: http://xunitpatterns.com/Test-Specific%20Subclass.html
It works like
public TestClass : RealClass { public int CallHiddenCalculate() { return Calculate(); // Calculate is now protected method that we expose for test purposes in this class } }
You can put this class in a test assembly so that your real assembly does not contain test logic and classes, because its design is poor.
- You can also use conditional compilation for the visibility attribute, as shown below.
#if DEBUG
public
#else
private
#endif
In this case, you can call unit tests in Debug, but these methods will not be visible in the release. However, this approach is much worse than higher, and is more ugly.
Testing only the public interface may not be enough (and often it is not) to say that you got good coverage for testing, and your code is easy to maintain and refactor.
As regards marking private methods as internal and assembly tests, see internal methods, this is bad for many reasons.
- Your previous private methods are visible from your assembly because they are internal
- you will have a certain logic of the test (internal for these methods will be for testing purposes only) in the release version of your product, which is bad
and I think there are more of them, but they are most important
source share