I would like to make unit tests a first class part of a language rather than bolting them on with annotations or encoding their function in class names. I don't have a strong argument against the minimalist language folks who like to keep the core language small, other than I have feeling it would make for a more powerful and useful language in the long run, even if I can't think of compelling reasons why at the moment :-)
One feature I would like to add is the ability for one class to say it is the unit test for another class. This would allow:
* The unit test class to have internal access to the class under test.
* An explicit test to make sure the often volumous test code is excluded from deployment images.
* Tools to automatically run tests and report results.
* A unit test class to be in a different package than the class under test.
So why did this burning issue come up?
Because when programmers unit test their C++ code they always wonder how they should do it. Should they make classes friends so they can peek at their privates? Should they make everything public? Should the they be able to call private methods? Should they access member attributes? Should they make two builds, one with #ifdef UNIT_TEST type code and one without out? How do you deal with static methods? There are really an endless number of questions because every situation you encounter in your program needs a way to make it cleanly and easily unit testable.
There are answers to all these questions, but the key points for me are:
1. Maintain data hiding.
2. A test must be able to do anything and everything necessary to verify correctness.
3. Code under test should have no idea it is being tested.
The engine that pulls the train here is the desire for data hiding: only expose the methods and attributes needed to fulfill the public contract of the class and the public contract should be as small and single purpose as possible.

Recent comments
23 weeks 1 day ago