Unit testing of object-oriented code requires two fundamental artifacts: replacing some of the original code with some kind of test doubles (aka dependency injection), and providing extra access to these private dependencies. In most cases we cannot apply link-time or preprocessor based dependency injection, therefore a typical testable unit uses additional interfaces or template type parameters for pure testing purposes. Thus a unit which we developed without testing in mind might have to be changed intrusively for testing. These changes can take us far away from the original natural design of the unit. As a result, in C++, test code is often badly interwoven with the unit we want to test. We've seen such interleaving in the software world before: exceptions and aspect oriented programming were both invented to try to eliminate this kind of interleaving of independent aspects. In this talk I'd like to demonstrate the above problems using code examples to show the decay of original intentions. I briefly discuss current possible directions to solve the dependency injection problem: e.g. using compile-time seams. Then, I will overview the current (partial) solutions for adding extra access for private data, like using friends, the Attorney-Client idiom, or exploiting privates using explicit template instantiations. Regarding to extra access of private data, I will present how a minimal language extension would help and make the testing code less intrusive. Open non-intrusive friend declarations could provide a good method to separate the test related code from the original intentions. We implemented a proof-of-concept solution, based on LLVM/Clang to show that such constructs can be established with a minimal syntactical and compilation overhead.
Gábor has been working with C++ since 2000. Currently, he is the member of Ericsson's CodeChecker program analysis team. He works on Cross Translation Unit (CTU) static analysis and the related ASTImporter of the LLVM/Clang compiler infrastructure.In 2019 he defended his PhD theses... Read More →
Dr. Zoltán Porkoláb is an Associate Professor of Faculty of informatics at Eötvös Loránd University (ELTE), Budapest, Hungary. At the same time, he holds Principal C++ Developer position at Ericsson Hungary Ltd.