SlideShare une entreprise Scribd logo
1  sur  47
Télécharger pour lire hors ligne
Informatik / Computer Science


TDD and Mock Objects
with Eclipse, CUTE und Mockator




Prof. Peter Sommerlad
Director IFS Institute for Software
Agile PT, Porto June 23rd 2012
Contact Info: Prof. Peter Sommerlad
                                                                                                                          phone: +41 55 222 4984
                                                                                                                                 email: ifs@hsr.ch



                                                                                                     see more on http://linticator.com
Linticator
Linticator gives you immediate feedback on programming style and common                              Pricing for Linticator is CHF 500.- per user
                                                                                                     (non-floating license). A maintenance contract
programmer mistakes by integrating Gimpel Software’s popular PC-lint and
                                                                                                     that is required for updates costs 20% of
FlexeLint static analysis tools into Eclipse CDT.
                                                                                                     license fee per year. The compulsory first
PC-lint and FlexeLint are powerful tools, but they are not very well integrated into a
                                                                                                     maintenance fee includes 6 month of free
modern developer's workflow. Linticator brings the power of Lint to the Eclipse C/C++
                                                                                                     updates.
Development Tools by fully integrating them into the IDE. With Linticator, you get
                                                                                                     Orders, enquiries for multiple, corporate
continuous feedback on the code you are working on, allowing you to write better code.               or site licenses are welcome at ifs@hsr.ch.

• Automatic Lint Configuration                                                                        Linticator requires a corresponding PC-Lint
  Lint's configuration, like include paths and symbols, is automatically updated from your            (Windows) or FlexeLint license per user.
  Eclipse CDT settings, freeing you from keeping them in sync manually.
                                                                                                     IFS Institute for Software
• Suppress Messages Easily
                                                                                                     IFS is an institute of HSR Rapperswil,
  False positives or unwanted Lint messages can be suppressed directly from Eclipse,
                                                                                                     member of FHO University of Applied
  without having to learn Lint's inhibition syntax–either locally, per file or per symbol.
                                                                                                     Sciences Eastern Switzerland.
• Interactive “Linting” and Information Display
  Lint is run after each build or on demand, and its findings are integrated into the editor          In January 2007 IFS became an associate
  by annotating the source view with interactive markers, by populating Eclipse’s problem            member of the Eclipse Foundation.
  view with Lint’s issues and by linking these issues with our Lint Documentation View.
                                                                                                     The institute manages research and
• Quick-Fix Coding Problems
                                                                                                     technology transfer projects of four
  Linticator provides automatic fixes for a growing number of Lint messages, e.g, making
                                                                                                     professors and hosts about a dozen
  a reference-parameter const can be done with two keystrokes or a mouse-click.
                                                                                                     assistants and employees.
Register at http://linticator.com if you want to try it for 30 days or order via email. Linticator   http://ifs.hsr.ch/
is available for Eclipse CDT 3.4 (Ganymede), 3.5 (Helios) and later. It is compatible with
Freescale CodeWarrior.




IFS INSTITUTE FOR SOFTWARE!                  HSR Hochschule für Technik, Oberseestrasse 10, CH - 8640 Rapperswil!                    http://ifs.hsr.ch
Contact Info: Prof. Peter Sommerlad
                                                                                                                    phone: +41 55 222 4984
                                                                                                                              email: ifs@hsr.ch



Includator
#include Structure Analysis and Optimization for C++ for Eclipse CDT
The Includator plug-in analyzes the dependencies of C++ source file structures generated by #include
directives, suggests how the #include structure of a C++ project can be optimized and performs this
optimization on behalf of the developer. The aim of these optimizations is to improve code readability and
                                                                                                                         #
                                                                                                                    see more on

quality, reduce coupling and thus reduce duration of builds and development time of C++ software.                   http://includator.com
                                                                                                                    to be released 2012
Includator Features
• Find unused includes
  Scans a single source file or a whole project for superfluous #include directives and proposes them to
                                                                                                                    Individual licensing TBA.
  be removed. This also covers the removal of #include directives providing declarations that are
  (transitively) reachable through others.                                                                          Free 30-day trial licenses

• Directly include referenced files                                                                                  available by registering at
  Ignores transitively included declarations and proposes to #include used declarations directly, if they           http://includator.com/
  are not already included. This provides an “include-what-you-use” code structure.                                 account/register.
• Organize includes
                                                                                                                    Enquiries for corporate or
  Similar to Eclipse Java Development Tool's Organize imports feature for Java. Adds missing #include
  directives and removes superfluous ones.                                                                           site licenses are welcome

• Replace includes with forward declarations                                                                        at ifs@hsr.ch.

  Locates #include directives for definitions that can be omitted, when replacing them with
  corresponding forward declarations instead. This one is useful for minimizing #includes and reducing
  dependencies required in header files.
• Static code coverage
  Marks C++ source code as either used, implicitly used or unused by transitively following C++
  elements’ definitions and usages. This helps to trim declarations and definitions not used from your                 Get Swiss Quality
  source code. In contrast to dynamic code coverage, such as provided by our CUTE plug-in (http://
  cute-test.com) it allows to determine required and useless C++ declarations and definitions instead of
  executed oder not-executed statements.
• Find unused files
  Locates single or even entangled header files that are never included in the project’s source files.

User Feedback and Participation
Includator is in beta testing. Register at http://includator.com if you are interested in a 30-day trial license
or if you want to be notified about Includator’s release.




IFS INSTITUTE FOR SOFTWARE!                  HSR Hochschule für Technik, Oberseestrasse 10, CH - 8640 Rapperswil!                    http://ifs.hsr.ch
Contact Info: Prof. Peter Sommerlad
                                                                                                                        +41 55 222 4984
                                                                                                                         email: ifs@hsr.ch




                                                                       Get Swiss Quality

Green Bar for C++ with CUTE
Eclipse plug-in for C++ unit testing with CUTE
Automated unit testing improves program code quality, even under inevitable change and
refactoring. As a side effect, unit tested code has a better structure. Java developers are
used to this because of JUnit and its tight integration into IDEs like Eclipse. We provide the
modern C++ Unit Testing Framework CUTE (C++ Unit Testing Easier) and a
corresponding Eclipse plug-in. The free CUTE plug-in features:                                   IFS Institute for Software
• wizard creating test projects (including required framework sources)                           IFS is an Institute of HSR Rapperswil,
• test function generator with automatic registration                                            member of FHO University of Applied

• detection of unregistered tests with quick-fix for registration                                 Sciences Eastern Switzerland.

• test navigator with green/red bar (see screen shots to the right)                              In January 2007 IFS became an
                                                                                                 associate member of the Eclipse
• diff-viewer for failing tests (see screen shot down to the right)
                                                                                                 Foundation.
• code coverage view with gcov (see screen shot below)
                                                                                                 The institute manages research and
Support for TDD and Mock Objects (Mockator)                                                      technology transfer projects of four
The CUTE plug-in incorporates support for Test-Driven Development (TDD) in C++. It will          professors and hosts a dozen assistants
support code generation for test doubles and mock objects in spring 2012:                        and employees.
                                                                                                 http://ifs.hsr.ch/
• create unknown function, member function, variable, or type from its use in a test case
  as a quick-fix (see screen shots below)

• move function or type from test implementation to new header file. (More MOVE
  Refactorings are becoming available in spring 2012)                                            Eclipse update site for installing the

• toggle function definitions between header file and implementation file, for easier change        free CUTE plug-in:
  of function signature, including member functions (now part of CDT itself)                     http://cute-test.com/updatesite
• extract template parameter for dependency injection, aka instrumenting code under test
  with a test stub through a template argument (instead of Java-like super-class
  extraction)
• automatic generation of test stubs and mock classes from test cases (spring 2012)
• extract super-class for dependency injection (spring 2012)




IFS INSTITUTE FOR SOFTWARE HSR Hochschule für Technik, Oberseestrasse 10, CH - 8640 Rapperswil!                              http://ifs.hsr.ch
Contact Info: Prof. Peter Sommerlad
                                                                                                                       +41 55 222 4984
                                                                                                                         email: ifs@hsr.ch



                                                                                !
SConsolidator                                                                                More information:

                                                                                             http://SConsolidator.com/
Eclipse CDT plug-in for SCons
                                                                                             Install the free SConsolidator plug-in from
SCons (http://www.SCons.org/) is an open source software build tool which tries to fix the
                                                                                             the following Eclipse update site:
numerous weaknesses of make and its derivatives. For example, the missing automatic
dependency extraction, make’s complex syntax to describe build properties and cross-         http://SConsolidator.com/update

platform issues when using shell commands and scripts. SCons is a self-contained tool
which is independent of existing platform utilities. Because it is based on Python a SCons
user has the full power of a programming language to deal with all build related issues.     SConsolidator has been successfully used to
                                                                                             import the following SCons-based projects
However, maintaining a SCons-based C/C++ project with Eclipse CDT meant, that all the
                                                                                             into Eclipse CDT:
intelligence SCons puts into your project dependencies had to be re-entered into Eclipse
                                                                                             • MongoDB
CDT’s project settings, so that CDT’s indexer and parser would know your code’s compile      • Blender
settings and enable many of CDT’s features. In addition, SCons’ intelligence comes at the    • FreeNOS
price of relatively long build startup times, when SCons (re-) analyzes the project          • Doom 3
dependencies which can become annoying when you just fix a simple syntax error.               • COAST (http://coast-project.org)

SConsolidator addresses these issues and provides tool integration for SCons in Eclipse
for a convenient C/C++ development experience. The free plug-in features:
• conversion of existing CDT managed build projects to SCons projects
• import of existing SCons projects into Eclipse with wizard support
• SCons projects can be managed either through CDT or SCons
• interactive mode to quickly build single source files speeding up round trip times
• a special view for a convenient build target management of all workspace projects
• graph visualization of build dependencies with numerous layout algorithms and search
    and filter functionality that enables debugging SCons scripts.
• quick navigation from build errors to source code locations




                                                                                    !




!
IFS INSTITUTE FOR SOFTWARE HSR Hochschule für Technik, Oberseestrasse 10, CH - 8640 Rapperswil!                             http://ifs.hsr.ch
Peter Sommerlad
                                                         Credo:
peter.sommerlad@hsr.ch

 Work Areas                                              People create Software
   Refactoring Tools (C++, Scala, ...) for Eclipse        communication
   Decremental Development (make SW 10% its size!)        feedback
   Modern Agile Software Engineering
                                                           courage
   C++ standardization
 Pattern Books (co-author)                               Experience through Practice

  Pattern-oriented Software Architecture Vol. 1           programming is a trade
  Security Patterns                                       Patterns encapsulate practical
                                                            experience
 Background
                                                          Pragmatic Programming
  Diplom-Informatiker / MSc CS (Univ. Frankfurt/M)
                                                           test-driven development
  Siemens Corporate Research - Munich
                                                           automated development
  itopia corporate information technology, Zurich
                                                           Simplicity: fight complexity
  Professor for Software Engineering, HSR Rapperswil,
   Director IFS Institute for Software


                                                   2

                                                                                 © Peter Sommerlad
Software Quality
                                   3
                   © Peter Sommerlad
Software Quality
                                   4
                   © Peter Sommerlad
Software Quality
"An Elephant in the Room"                   5
                            © Peter Sommerlad
Software Quality
                                                                                          6
                                                                          © Peter Sommerlad



 classic approach:
  manual testing after creation

                                   Cape of Good

                                   Hope           or bury your head in the sand?
But:
Small Cute Things                   7
                    © Peter Sommerlad
Grow to become larger Problems!
                                                  8
                                  © Peter Sommerlad
Overview

                                                                                                     ScrumTable




                                                       Includator
     Eclipse CDT Compile Seam
          Metrics Unit Testing Preprocessor Seam
      Code
     Static

   Analysis




                                                                    Refactoring
                      Test- C++




                                                                            Object Seam
                                                                                      optimization
                                                                                      #include
                                     TDD
                         SConsolidator
                 Linticator


                      Driven
                      Development
Legacy Code                              C++ CUTE
           Link Seam                     Seam Introduction
                                         Mockator Mock Object
                                         Test Double

                                                                                                     Scala Refactoring



                                                                                                           © Peter Sommerlad
Part 1 - TDD
                                                           © Peter Sommerlad




  C++        Unit Testing with CUTE in Eclipse CDT

       Test-
 TDD



       Driven                    and         Refactoring
       Development

 CUTE http://cute-test.com - free!!!
 simple to use - test is a function
  understandable also for C programmers
 designed to be used with IDE support
  can be used without, but a slightly higher effort
 deliberate minimization of #define macro usage
  macros make life harder for C/C++ IDEs
TDD Cycle
                                                                         11
                                                          © Peter Sommerlad




                                                    This is the
                                RED                 part I'd like
                                                    to show
                              make a                automatic
                             failing test           support today




        Integrate                                Green
    make test and change                     make a change to
    permanent - check in                      pass the test




                             Refactor
                           make the design
                              simpler
Eclipse CDT TDD Support for CUTE and CUTE-plug-in features


 get the cute plug-in from http://cute-test.com/updatesite


 create CUTE test project
 create test function
 create function definition from call (in test case)
 create type definition from usage (in test case)
 create variable definition from usage (in test case)
 move type definition into new header file (from test case file)
 toggle function definition between header and implementation (part of CDT)




                                            12

                                                                         © Peter Sommerlad
TDD Demo
Simulation of a Switch                                                              13
                                                                     © Peter Sommerlad



 Simulation of a Switch (class)
  alternative possible, if this seems too simple and time permits




     The List for class Switch
     1. Create,GetState ! off                                Demo

     2. TurnOn,GetSTATE ! ON
     3. Turnoff,getSTATE ! off
Demo
                                                 14
                                  © Peter Sommerlad



 New C++ Project
   CUTE Test Project
 Run as CUTE Test
 Fix Failing Test
   write first "real" test
     rename test case function
     write test code
     make it compile through
      TDD quick-fixes (2x)
       create type
       create function

 Run Tests
   green bar
 iterate...
Overview

                                                                                                     ScrumTable




                                                       Includator
     Eclipse CDT Compile Seam
          Metrics Unit Testing Preprocessor Seam
      Code
     Static

   Analysis




                                                                    Refactoring
                      Test- C++




                                                                            Object Seam
                                                                                      optimization
                                                                                      #include
                                     TDD
                         SConsolidator
                 Linticator


                      Driven
                      Development
Legacy Code                              C++ CUTE
           Link Seam                     Seam Introduction
                                         Mockator Mock Object
                                         Test Double

                                                                                                     Scala Refactoring



                                                                                                           © Peter Sommerlad
Part 2 - Seams and Mocks


Mockator
 C++ Legacy Code Refactoring enabling Unit Testing
 Seam Introduction                  Test Double generation
   Object Seam
                                    Mock Object generation
   Compile Seam
   Link Seam
   Preprocessor Seam                Function Tracer generation



          Master Thesis by Michael Rüegg
          inspired and supervised by Prof. Peter Sommerlad
          soon to be released on http://mockator.com
          beta at http://sinv–56033.edu.hsr.ch/mockator/repo




                                                                 © Peter Sommerlad
Four Phase Test Case
Setup, Exercise, Verify, Teardown                                                   17
                                                                     © Peter Sommerlad


                                                How to decouple SUT from DOC?
 SUT - System under Test
 DOC - Depended on Component,




 source: xunitpatterns.com - Gerard Meszaros
Seams!
                                                                                              18
                                                                               © Peter Sommerlad
How to decouple SUT from DOC?
 Introduce a Seam:
  makes DOC exchangeable!
  C++ provides different mechanisms
 Object Seam (classic OO seam)
  Introduce Interface - Change SUT to use Interface instead of DOC directly
    introduces virtual function lookup overhead

  Pass DOC as a (constructor) Argument
  Pass Test Double as Argument for Tests
 Compile Seam (use template parameter)
  Make DOC a default template Argument
Introduce an Object Seam
                                                                                           19
                                                                            © Peter Sommerlad



 classic inheritance based mocking
  extract interface for DOC -> IDOC
  make SUT use IDOC
  create MOCK implementing IDOC and use it in UT



                                 SUT                           DOC

          UT                     SUT                           IDOC


                                                    MOCK              DOC
    in C++ this means overhead for DOC (virtual functions)!
Introduce an Object Seam
                                                                                                 20
                                                                                  © Peter Sommerlad



 classic inheritance based mocking
  extract interface for DOC -> IDOC
  make SUT use IDOC, edit constructor
  create MOCK implementing IDOC and use it in UT



                                 SUT                           DOC    Extract Interface
                                                                      Refactoring


          UT                     SUT                           IDOC


                                                    MOCK              DOC
    in C++ this means overhead for DOC (virtual functions)!
Introduce a Compile Seam
                                                                                         21
                                                                          © Peter Sommerlad



 C++ template parameter based mocking
  make DOC a default template argument of SUT


                          SUT                    DOC
                                             Extract template parameter Refactoring

                                         DOC

                                    SUT
                                         MOCK

                 UT                 SUT
   
Introduce a Compile Seam
                                                                                         22
                                                                          © Peter Sommerlad



 C++ template parameter based mocking
  make DOC a default template argument of SUT


                          SUT                    DOC
                                             Extract template parameter

                                         DOC

                                    SUT
                                         MOCK

                 UT                 SUT
   
C/C++ specifics: Link Seam, Preprocessor
                                                                                                                23
                                                                                                 © Peter Sommerlad



 absolutely no change on existing code of SUT needed!
 remove Dependency on system/library functions
  shadowing through providing an alternative implementation earlier in link sequence
    avoid dependency on system or non-deterministic functions, e.g. rand(), time(), or "slow" calls

  wrapping of functions with GNU linker --wrap option, allows calling original also
    good for tracing, additional checks (e.g., valgrind)

  wrapping functions within dynamic libraries with dlopen/dlsym&LD_PRELOAD
  problem: C++ name mangling if done by hand (solved by Mockator)
 replace calls through #define preprocessor macro
  as a means of last resort, many potential problems
Refactoring for Mocks in C++

Variations of Mock Objects classics




                      24
Principle of Mock objects
                                                                                      25
                                                                       © Peter Sommerlad



 A unit/system under test (SUT) depends on another component (DOC) that we want
  to separate out from our test.
 Reasons
  real DOC might not exist yet
  real DOC contains uncontrollable behavior
  want to test exceptional behavior by DOC that is hard to trigger
  using the real DOC is too expensive or takes to long
  need to locate problems within SUT not DOC
  want to test usage of DOC by SUT is correct
Why the need for Mock Objects?
                                                                      26
                                                       © Peter Sommerlad



 Simpler Tests and Design
  especially for external dependencies
  promote interface-oriented design
 Independent Testing of single Units
  isolation of unit under testing
  or for not-yet-existing units
 Speed of Tests
  no external communication (e.g., DB, network)
 Check usage of third component
  is complex API used correctly
 Test exceptional behaviour
  especially when such behaviour is hard to trigger
Types of Mock Objects
[Dave Astels]                                                                                        27
                                                                                      © Peter Sommerlad



 There exist different categories of Mock objects and different categorizers.


 Stubs
  substitutes for “expensive” or non-deterministic classes with fixed, hard-coded return
   values
 Fakes
  substitutes for not yet implemented classes
 Mocks
  substitutes with additional functionality to record function calls, and the potential to
   deliver different values for different calls
Interface-oriented Mock
                                                                                           28
                                                                            © Peter Sommerlad



 classic inheritance based mocking
  extract interface for DOC -> IDOC
  make SUT use IDOC
  create MOCK implementing IDOC and use it in UT



                                 SUT                           DOC

          UT                     SUT                           IDOC


                                                    MOCK              DOC
    in C++ this means overhead for DOC (virtual functions)!
Demo/Exercise
Code in need for Mocking                                                                   29
                                                                            © Peter Sommerlad



 A very simple game, roll dice, check if you’ve got 4 and you win, otherwise you
  loose.




                          Game                  Die
 We want to test class Die first:

 #include <cstdlib>

 struct Die
 {
 	 int roll() { return rand()%6 + 1; }
 };
How to test Game?
                                                    30
                                     © Peter Sommerlad




  #include "Die.h"
  class GameFourWins
  {
  	 Die die;
  public:
  	 GameFourWins();
  	 void play();
  };

  void GameFourWins::play(){
  	 if (die.roll() == 4) {
  	 	 cout << "You won!" << endl;
  	 } else {
  	 	 cout << "You lost!" << endl;
  	 }
  }
Refactoring
Introduce Parameter                                         31
                                             © Peter Sommerlad




#include "Die.h"
#include <iostream>

class GameFourWins
{
	 Die die;
public:
	 GameFourWins();
	 void play(std::ostream &os = std::cout);
};

void GameFourWins::play(std::ostream &os){
	 if (die.roll() == 4) {
	 	 os << "You won!" << endl;
	 } else {
	 	 os << "You lost!" << endl;
	 }
}
Test with a Mock ostream
                                                                                          32
                                                                           © Peter Sommerlad



 We now can use a ostrstream to collect the output of play() and check that against
  an expected value:
 void testGame() {
 	 GameFourWins game;
 	 std::ostringstream os;
 	 game.play(os);
 	 ASSERT_EQUAL("You lost!n",os.str());
 }

 What is still wrong with that test?
Simulation Mocks
Interface-oriented                                                                        33
                                                                           © Peter Sommerlad



 deliver predefined values
  we need that for our Die class
 Introduce an Interface
  struct DieInterface
  {
  	 virtual ~DieInterface(){}
  	 virtual int roll() =0;
  };

  struct Die: DieInterface
  {
  	 int roll() { return rand()%6+1; }
  };

 now we need to adjust Game as well to use DieInterface& instead of Die


 Mockator Pro plug-in will make those code conversions automatic (Summer 2012)
Simulation Mocks
preparing SUT                                                                             34
                                                                           © Peter Sommerlad



 Changing the interface, need to adapt call sites
 theDie must live longer than Game object


 class GameFourWins
 {
 	 DieInterface &die;
 public:
 	 GameFourWins(DieInterface &theDie):die(theDie){}
 	 void play(std::ostream &os = std::cout);
 };


 now we can write our test using an alternative implementation of DieInterface
 would using pointer instead of reference improve situation? what’s different?
Simulation Mock
Test it                                                                   35
                                                           © Peter Sommerlad



 This way we can also thoroughly test the winning case:


 struct MockWinningDice:DieInterface{
 	 int roll(){return 4;}
 };

 void testWinningGame() {
 	 MockWinningDice d;
 	 GameFourWins game(d);
 	 std::ostringstream os;
 	 game.play(os);
 	 ASSERT_EQUAL("You won!n",os.str());
 }
A C++ alternative using templates
                                                                                           36
                                                                            © Peter Sommerlad



 advantages: no virtual call overhead, no extra Interface extraction
  transformation provided by our "Introduce Typename Template Paramter" Refactoring
 drawback: inline/export problem potential
 template <typename Dice=Die>
 class GameFourWinsT
 {
 	 Dice die;
 public:
 	 void play(std::ostream &os = std::cout){
 	 	 if (die.roll() == 4) {
   	 os << "You won!" << std::endl;
 	 	 } else {
 	 	    os << "You lost!" << std::endl;
 	 	 }
 	 }
 };
 typedef GameFourWinsT<Die> GameFourWins;
Mock via template parameter
                                                              37
                                               © Peter Sommerlad



 The resulting test looks like this:
  struct MockWinningDice{
  	 int roll(){return 4;}
  };
  void testWinningGame() {
  	 GameFourWins<MockWinningDice> game;
  	 std::ostringstream os;
  	 game.play(os);
  	 ASSERT_EQUAL("You won!n",os.str());
  }

 should we also mock the ostream similarly?
Call Tracing Mocks
                                                                                          38
                                                                           © Peter Sommerlad



 We want also to count how often our dice are rolled. How to test this?

   struct MockWinningDice:DieInterface{
   	 int rollcounter;
   	 MockWinningDice():rollcounter(0){}
   	 int roll(){++rollcounter; return 4;}
   };
   void testWinningGame() {
   	 MockWinningDice d;
   	 GameFourWins game(d);
   	 std::ostringstream os;
   	 game.play(os);
   	 ASSERT_EQUAL("You won!n",os.str());
   	 ASSERT_EQUAL(1,d.rollcounter);
   	 game.play(os);
   	 ASSERT_EQUAL(2,d.rollcounter);
   }
Using C++ template Parameters for Mocking
                                                                                              39
                                                                               © Peter Sommerlad



 C++ template parameters can be used for mocking without virtual member function
  overhead and explicit interface extraction.
  no need to pass object in as additional parameter
  unfortunately no default template parameters for template functions (yet)
 You can mock
  Member Variable Types
  Function Parameter Types
 Mocking without template inline/export need is possible through explicit
  instantiations
Mockator Demo - Compile Seam using Templates
                                                              40
                                               © Peter Sommerlad



 Extract Template Parameter
  part of CUTE plug-in
 Use Template in Test
  introduce Mock object for DIE
    create test double class...
    add missing member function
    implement test double code

 Add Mock Object Support
  check for expected calls (C++11)
 additional features
  dependency injection through
    templates
    abstract interface classes
Mockator - Preprocessor Seam
                                                                                            41
                                                                             © Peter Sommerlad



 mockator_malloc.h
  #ifndef MOCKATOR_MALLOC_H_
  #define MOCKATOR_MALLOC_H_
  #include	 <cstdlib >
  int mockator_malloc(size_t size, const char *fileName, int lineNumber);
  #define malloc(size_t size) mockator_malloc((size),__FILE__,__LINE__)
  #endif

 mockator_malloc.c
  #include "mockator_malloc.h"
  #undef malloc
  int mockator_malloc(size_t size, const char * fileName, int lineNumber)	{
  //TODO your tracing code here
     return malloc(size);
  }



 Generating trace functions like this one is easy: Ctrl+Alt+T (on Linux)
  Mockator passes mockator_malloc.h to the GNU compiler by using its -include option
Mockator - Link Seam - Shadowing & Wrapping Library Functions
                                                                                                 42
                                                                                  © Peter Sommerlad



 Shadowing on Linux only using GCC Linker
 Allows wrapping of functions in shared libraries
  Both Linux and MacOS X supported
 Mockator does all the hard work like
  creating a shared library project,
  adding dependencies to the dl library,
  creating run-time configurations with the following env values, etc.,
    Linux: LD PRELOAD=libName.so MacOS X:
    DYLD FORCE FLAT NAMESPACE=1             int foo(int i) {
                                                static void *gptr = nullptr;
    DYLD INSERT LIBRARIES=libName.dylib        if(!gptr)	gptr	 =	 dlsym(RTLD_NEXT,"_Z3fooi");
                                                typedef int (*fptr)(int);
                                                fptr	my_fptr	=	 reinterpret_cast<fptr>(gptr);
                                                // TODO	put	 your	 code	 here
                                                return my_fptr(i);
                                             }
Questions ?




 http://cute-test.com (Mockator will be here 3.Q2012)
                                                         Have Fun with TDD
 http://linticator.com http://includator.com
 http://sconsolidator.com (M. Rüegg as well)
                                                          Mockator and
 peter.sommerlad@hsr.ch http://ifs.hsr.ch                 Refactoring!

                                                 43

                                                                     © Peter Sommerlad

Contenu connexe

Dernier

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 

Dernier (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

En vedette

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

En vedette (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

TDD and Mock Objects for C++ with Eclipse CDT, CUTE and Mockator

  • 1. Informatik / Computer Science TDD and Mock Objects with Eclipse, CUTE und Mockator Prof. Peter Sommerlad Director IFS Institute for Software Agile PT, Porto June 23rd 2012
  • 2. Contact Info: Prof. Peter Sommerlad phone: +41 55 222 4984 email: ifs@hsr.ch see more on http://linticator.com Linticator Linticator gives you immediate feedback on programming style and common Pricing for Linticator is CHF 500.- per user (non-floating license). A maintenance contract programmer mistakes by integrating Gimpel Software’s popular PC-lint and that is required for updates costs 20% of FlexeLint static analysis tools into Eclipse CDT. license fee per year. The compulsory first PC-lint and FlexeLint are powerful tools, but they are not very well integrated into a maintenance fee includes 6 month of free modern developer's workflow. Linticator brings the power of Lint to the Eclipse C/C++ updates. Development Tools by fully integrating them into the IDE. With Linticator, you get Orders, enquiries for multiple, corporate continuous feedback on the code you are working on, allowing you to write better code. or site licenses are welcome at ifs@hsr.ch. • Automatic Lint Configuration Linticator requires a corresponding PC-Lint Lint's configuration, like include paths and symbols, is automatically updated from your (Windows) or FlexeLint license per user. Eclipse CDT settings, freeing you from keeping them in sync manually. IFS Institute for Software • Suppress Messages Easily IFS is an institute of HSR Rapperswil, False positives or unwanted Lint messages can be suppressed directly from Eclipse, member of FHO University of Applied without having to learn Lint's inhibition syntax–either locally, per file or per symbol. Sciences Eastern Switzerland. • Interactive “Linting” and Information Display Lint is run after each build or on demand, and its findings are integrated into the editor In January 2007 IFS became an associate by annotating the source view with interactive markers, by populating Eclipse’s problem member of the Eclipse Foundation. view with Lint’s issues and by linking these issues with our Lint Documentation View. The institute manages research and • Quick-Fix Coding Problems technology transfer projects of four Linticator provides automatic fixes for a growing number of Lint messages, e.g, making professors and hosts about a dozen a reference-parameter const can be done with two keystrokes or a mouse-click. assistants and employees. Register at http://linticator.com if you want to try it for 30 days or order via email. Linticator http://ifs.hsr.ch/ is available for Eclipse CDT 3.4 (Ganymede), 3.5 (Helios) and later. It is compatible with Freescale CodeWarrior. IFS INSTITUTE FOR SOFTWARE! HSR Hochschule für Technik, Oberseestrasse 10, CH - 8640 Rapperswil! http://ifs.hsr.ch
  • 3. Contact Info: Prof. Peter Sommerlad phone: +41 55 222 4984 email: ifs@hsr.ch Includator #include Structure Analysis and Optimization for C++ for Eclipse CDT The Includator plug-in analyzes the dependencies of C++ source file structures generated by #include directives, suggests how the #include structure of a C++ project can be optimized and performs this optimization on behalf of the developer. The aim of these optimizations is to improve code readability and # see more on quality, reduce coupling and thus reduce duration of builds and development time of C++ software. http://includator.com to be released 2012 Includator Features • Find unused includes Scans a single source file or a whole project for superfluous #include directives and proposes them to Individual licensing TBA. be removed. This also covers the removal of #include directives providing declarations that are (transitively) reachable through others. Free 30-day trial licenses • Directly include referenced files available by registering at Ignores transitively included declarations and proposes to #include used declarations directly, if they http://includator.com/ are not already included. This provides an “include-what-you-use” code structure. account/register. • Organize includes Enquiries for corporate or Similar to Eclipse Java Development Tool's Organize imports feature for Java. Adds missing #include directives and removes superfluous ones. site licenses are welcome • Replace includes with forward declarations at ifs@hsr.ch. Locates #include directives for definitions that can be omitted, when replacing them with corresponding forward declarations instead. This one is useful for minimizing #includes and reducing dependencies required in header files. • Static code coverage Marks C++ source code as either used, implicitly used or unused by transitively following C++ elements’ definitions and usages. This helps to trim declarations and definitions not used from your Get Swiss Quality source code. In contrast to dynamic code coverage, such as provided by our CUTE plug-in (http:// cute-test.com) it allows to determine required and useless C++ declarations and definitions instead of executed oder not-executed statements. • Find unused files Locates single or even entangled header files that are never included in the project’s source files. User Feedback and Participation Includator is in beta testing. Register at http://includator.com if you are interested in a 30-day trial license or if you want to be notified about Includator’s release. IFS INSTITUTE FOR SOFTWARE! HSR Hochschule für Technik, Oberseestrasse 10, CH - 8640 Rapperswil! http://ifs.hsr.ch
  • 4. Contact Info: Prof. Peter Sommerlad +41 55 222 4984 email: ifs@hsr.ch Get Swiss Quality Green Bar for C++ with CUTE Eclipse plug-in for C++ unit testing with CUTE Automated unit testing improves program code quality, even under inevitable change and refactoring. As a side effect, unit tested code has a better structure. Java developers are used to this because of JUnit and its tight integration into IDEs like Eclipse. We provide the modern C++ Unit Testing Framework CUTE (C++ Unit Testing Easier) and a corresponding Eclipse plug-in. The free CUTE plug-in features: IFS Institute for Software • wizard creating test projects (including required framework sources) IFS is an Institute of HSR Rapperswil, • test function generator with automatic registration member of FHO University of Applied • detection of unregistered tests with quick-fix for registration Sciences Eastern Switzerland. • test navigator with green/red bar (see screen shots to the right) In January 2007 IFS became an associate member of the Eclipse • diff-viewer for failing tests (see screen shot down to the right) Foundation. • code coverage view with gcov (see screen shot below) The institute manages research and Support for TDD and Mock Objects (Mockator) technology transfer projects of four The CUTE plug-in incorporates support for Test-Driven Development (TDD) in C++. It will professors and hosts a dozen assistants support code generation for test doubles and mock objects in spring 2012: and employees. http://ifs.hsr.ch/ • create unknown function, member function, variable, or type from its use in a test case as a quick-fix (see screen shots below) • move function or type from test implementation to new header file. (More MOVE Refactorings are becoming available in spring 2012) Eclipse update site for installing the • toggle function definitions between header file and implementation file, for easier change free CUTE plug-in: of function signature, including member functions (now part of CDT itself) http://cute-test.com/updatesite • extract template parameter for dependency injection, aka instrumenting code under test with a test stub through a template argument (instead of Java-like super-class extraction) • automatic generation of test stubs and mock classes from test cases (spring 2012) • extract super-class for dependency injection (spring 2012) IFS INSTITUTE FOR SOFTWARE HSR Hochschule für Technik, Oberseestrasse 10, CH - 8640 Rapperswil! http://ifs.hsr.ch
  • 5. Contact Info: Prof. Peter Sommerlad +41 55 222 4984 email: ifs@hsr.ch ! SConsolidator More information: http://SConsolidator.com/ Eclipse CDT plug-in for SCons Install the free SConsolidator plug-in from SCons (http://www.SCons.org/) is an open source software build tool which tries to fix the the following Eclipse update site: numerous weaknesses of make and its derivatives. For example, the missing automatic dependency extraction, make’s complex syntax to describe build properties and cross- http://SConsolidator.com/update platform issues when using shell commands and scripts. SCons is a self-contained tool which is independent of existing platform utilities. Because it is based on Python a SCons user has the full power of a programming language to deal with all build related issues. SConsolidator has been successfully used to import the following SCons-based projects However, maintaining a SCons-based C/C++ project with Eclipse CDT meant, that all the into Eclipse CDT: intelligence SCons puts into your project dependencies had to be re-entered into Eclipse • MongoDB CDT’s project settings, so that CDT’s indexer and parser would know your code’s compile • Blender settings and enable many of CDT’s features. In addition, SCons’ intelligence comes at the • FreeNOS price of relatively long build startup times, when SCons (re-) analyzes the project • Doom 3 dependencies which can become annoying when you just fix a simple syntax error. • COAST (http://coast-project.org) SConsolidator addresses these issues and provides tool integration for SCons in Eclipse for a convenient C/C++ development experience. The free plug-in features: • conversion of existing CDT managed build projects to SCons projects • import of existing SCons projects into Eclipse with wizard support • SCons projects can be managed either through CDT or SCons • interactive mode to quickly build single source files speeding up round trip times • a special view for a convenient build target management of all workspace projects • graph visualization of build dependencies with numerous layout algorithms and search and filter functionality that enables debugging SCons scripts. • quick navigation from build errors to source code locations ! ! IFS INSTITUTE FOR SOFTWARE HSR Hochschule für Technik, Oberseestrasse 10, CH - 8640 Rapperswil! http://ifs.hsr.ch
  • 6. Peter Sommerlad Credo: peter.sommerlad@hsr.ch  Work Areas  People create Software  Refactoring Tools (C++, Scala, ...) for Eclipse  communication  Decremental Development (make SW 10% its size!)  feedback  Modern Agile Software Engineering  courage  C++ standardization  Pattern Books (co-author)  Experience through Practice  Pattern-oriented Software Architecture Vol. 1  programming is a trade  Security Patterns  Patterns encapsulate practical experience  Background  Pragmatic Programming  Diplom-Informatiker / MSc CS (Univ. Frankfurt/M)  test-driven development  Siemens Corporate Research - Munich  automated development  itopia corporate information technology, Zurich  Simplicity: fight complexity  Professor for Software Engineering, HSR Rapperswil, Director IFS Institute for Software 2 © Peter Sommerlad
  • 7. Software Quality 3 © Peter Sommerlad
  • 8. Software Quality 4 © Peter Sommerlad
  • 9. Software Quality "An Elephant in the Room" 5 © Peter Sommerlad
  • 10. Software Quality 6 © Peter Sommerlad  classic approach:  manual testing after creation Cape of Good Hope or bury your head in the sand?
  • 11. But: Small Cute Things 7 © Peter Sommerlad
  • 12. Grow to become larger Problems! 8 © Peter Sommerlad
  • 13. Overview ScrumTable Includator Eclipse CDT Compile Seam Metrics Unit Testing Preprocessor Seam Code Static Analysis Refactoring Test- C++ Object Seam optimization #include TDD SConsolidator Linticator Driven Development Legacy Code C++ CUTE Link Seam Seam Introduction Mockator Mock Object Test Double Scala Refactoring © Peter Sommerlad
  • 14. Part 1 - TDD © Peter Sommerlad C++ Unit Testing with CUTE in Eclipse CDT Test- TDD Driven and Refactoring Development  CUTE http://cute-test.com - free!!!  simple to use - test is a function  understandable also for C programmers  designed to be used with IDE support  can be used without, but a slightly higher effort  deliberate minimization of #define macro usage  macros make life harder for C/C++ IDEs
  • 15. TDD Cycle 11 © Peter Sommerlad This is the RED part I'd like to show make a automatic failing test support today Integrate Green make test and change make a change to permanent - check in pass the test Refactor make the design simpler
  • 16. Eclipse CDT TDD Support for CUTE and CUTE-plug-in features  get the cute plug-in from http://cute-test.com/updatesite  create CUTE test project  create test function  create function definition from call (in test case)  create type definition from usage (in test case)  create variable definition from usage (in test case)  move type definition into new header file (from test case file)  toggle function definition between header and implementation (part of CDT) 12 © Peter Sommerlad
  • 17. TDD Demo Simulation of a Switch 13 © Peter Sommerlad  Simulation of a Switch (class)  alternative possible, if this seems too simple and time permits The List for class Switch 1. Create,GetState ! off Demo 2. TurnOn,GetSTATE ! ON 3. Turnoff,getSTATE ! off
  • 18. Demo 14 © Peter Sommerlad  New C++ Project  CUTE Test Project  Run as CUTE Test  Fix Failing Test  write first "real" test  rename test case function  write test code  make it compile through TDD quick-fixes (2x)  create type  create function  Run Tests  green bar  iterate...
  • 19. Overview ScrumTable Includator Eclipse CDT Compile Seam Metrics Unit Testing Preprocessor Seam Code Static Analysis Refactoring Test- C++ Object Seam optimization #include TDD SConsolidator Linticator Driven Development Legacy Code C++ CUTE Link Seam Seam Introduction Mockator Mock Object Test Double Scala Refactoring © Peter Sommerlad
  • 20. Part 2 - Seams and Mocks Mockator C++ Legacy Code Refactoring enabling Unit Testing Seam Introduction Test Double generation Object Seam Mock Object generation Compile Seam Link Seam Preprocessor Seam Function Tracer generation Master Thesis by Michael Rüegg inspired and supervised by Prof. Peter Sommerlad soon to be released on http://mockator.com beta at http://sinv–56033.edu.hsr.ch/mockator/repo © Peter Sommerlad
  • 21. Four Phase Test Case Setup, Exercise, Verify, Teardown 17 © Peter Sommerlad How to decouple SUT from DOC?  SUT - System under Test  DOC - Depended on Component,  source: xunitpatterns.com - Gerard Meszaros
  • 22. Seams! 18 © Peter Sommerlad How to decouple SUT from DOC?  Introduce a Seam:  makes DOC exchangeable!  C++ provides different mechanisms  Object Seam (classic OO seam)  Introduce Interface - Change SUT to use Interface instead of DOC directly  introduces virtual function lookup overhead  Pass DOC as a (constructor) Argument  Pass Test Double as Argument for Tests  Compile Seam (use template parameter)  Make DOC a default template Argument
  • 23. Introduce an Object Seam 19 © Peter Sommerlad  classic inheritance based mocking  extract interface for DOC -> IDOC  make SUT use IDOC  create MOCK implementing IDOC and use it in UT SUT DOC UT SUT IDOC MOCK DOC  in C++ this means overhead for DOC (virtual functions)!
  • 24. Introduce an Object Seam 20 © Peter Sommerlad  classic inheritance based mocking  extract interface for DOC -> IDOC  make SUT use IDOC, edit constructor  create MOCK implementing IDOC and use it in UT SUT DOC Extract Interface Refactoring UT SUT IDOC MOCK DOC  in C++ this means overhead for DOC (virtual functions)!
  • 25. Introduce a Compile Seam 21 © Peter Sommerlad  C++ template parameter based mocking  make DOC a default template argument of SUT SUT DOC Extract template parameter Refactoring DOC SUT MOCK UT SUT 
  • 26. Introduce a Compile Seam 22 © Peter Sommerlad  C++ template parameter based mocking  make DOC a default template argument of SUT SUT DOC Extract template parameter DOC SUT MOCK UT SUT 
  • 27. C/C++ specifics: Link Seam, Preprocessor 23 © Peter Sommerlad  absolutely no change on existing code of SUT needed!  remove Dependency on system/library functions  shadowing through providing an alternative implementation earlier in link sequence  avoid dependency on system or non-deterministic functions, e.g. rand(), time(), or "slow" calls  wrapping of functions with GNU linker --wrap option, allows calling original also  good for tracing, additional checks (e.g., valgrind)  wrapping functions within dynamic libraries with dlopen/dlsym&LD_PRELOAD  problem: C++ name mangling if done by hand (solved by Mockator)  replace calls through #define preprocessor macro  as a means of last resort, many potential problems
  • 28. Refactoring for Mocks in C++ Variations of Mock Objects classics 24
  • 29. Principle of Mock objects 25 © Peter Sommerlad  A unit/system under test (SUT) depends on another component (DOC) that we want to separate out from our test.  Reasons  real DOC might not exist yet  real DOC contains uncontrollable behavior  want to test exceptional behavior by DOC that is hard to trigger  using the real DOC is too expensive or takes to long  need to locate problems within SUT not DOC  want to test usage of DOC by SUT is correct
  • 30. Why the need for Mock Objects? 26 © Peter Sommerlad  Simpler Tests and Design  especially for external dependencies  promote interface-oriented design  Independent Testing of single Units  isolation of unit under testing  or for not-yet-existing units  Speed of Tests  no external communication (e.g., DB, network)  Check usage of third component  is complex API used correctly  Test exceptional behaviour  especially when such behaviour is hard to trigger
  • 31. Types of Mock Objects [Dave Astels] 27 © Peter Sommerlad  There exist different categories of Mock objects and different categorizers.  Stubs  substitutes for “expensive” or non-deterministic classes with fixed, hard-coded return values  Fakes  substitutes for not yet implemented classes  Mocks  substitutes with additional functionality to record function calls, and the potential to deliver different values for different calls
  • 32. Interface-oriented Mock 28 © Peter Sommerlad  classic inheritance based mocking  extract interface for DOC -> IDOC  make SUT use IDOC  create MOCK implementing IDOC and use it in UT SUT DOC UT SUT IDOC MOCK DOC  in C++ this means overhead for DOC (virtual functions)!
  • 33. Demo/Exercise Code in need for Mocking 29 © Peter Sommerlad  A very simple game, roll dice, check if you’ve got 4 and you win, otherwise you loose. Game Die  We want to test class Die first: #include <cstdlib> struct Die { int roll() { return rand()%6 + 1; } };
  • 34. How to test Game? 30 © Peter Sommerlad #include "Die.h" class GameFourWins { Die die; public: GameFourWins(); void play(); }; void GameFourWins::play(){ if (die.roll() == 4) { cout << "You won!" << endl; } else { cout << "You lost!" << endl; } }
  • 35. Refactoring Introduce Parameter 31 © Peter Sommerlad #include "Die.h" #include <iostream> class GameFourWins { Die die; public: GameFourWins(); void play(std::ostream &os = std::cout); }; void GameFourWins::play(std::ostream &os){ if (die.roll() == 4) { os << "You won!" << endl; } else { os << "You lost!" << endl; } }
  • 36. Test with a Mock ostream 32 © Peter Sommerlad  We now can use a ostrstream to collect the output of play() and check that against an expected value: void testGame() { GameFourWins game; std::ostringstream os; game.play(os); ASSERT_EQUAL("You lost!n",os.str()); }  What is still wrong with that test?
  • 37. Simulation Mocks Interface-oriented 33 © Peter Sommerlad  deliver predefined values  we need that for our Die class  Introduce an Interface struct DieInterface { virtual ~DieInterface(){} virtual int roll() =0; }; struct Die: DieInterface { int roll() { return rand()%6+1; } };  now we need to adjust Game as well to use DieInterface& instead of Die  Mockator Pro plug-in will make those code conversions automatic (Summer 2012)
  • 38. Simulation Mocks preparing SUT 34 © Peter Sommerlad  Changing the interface, need to adapt call sites  theDie must live longer than Game object class GameFourWins { DieInterface &die; public: GameFourWins(DieInterface &theDie):die(theDie){} void play(std::ostream &os = std::cout); };  now we can write our test using an alternative implementation of DieInterface  would using pointer instead of reference improve situation? what’s different?
  • 39. Simulation Mock Test it 35 © Peter Sommerlad  This way we can also thoroughly test the winning case: struct MockWinningDice:DieInterface{ int roll(){return 4;} }; void testWinningGame() { MockWinningDice d; GameFourWins game(d); std::ostringstream os; game.play(os); ASSERT_EQUAL("You won!n",os.str()); }
  • 40. A C++ alternative using templates 36 © Peter Sommerlad  advantages: no virtual call overhead, no extra Interface extraction  transformation provided by our "Introduce Typename Template Paramter" Refactoring  drawback: inline/export problem potential template <typename Dice=Die> class GameFourWinsT { Dice die; public: void play(std::ostream &os = std::cout){ if (die.roll() == 4) { os << "You won!" << std::endl; } else { os << "You lost!" << std::endl; } } }; typedef GameFourWinsT<Die> GameFourWins;
  • 41. Mock via template parameter 37 © Peter Sommerlad  The resulting test looks like this: struct MockWinningDice{ int roll(){return 4;} }; void testWinningGame() { GameFourWins<MockWinningDice> game; std::ostringstream os; game.play(os); ASSERT_EQUAL("You won!n",os.str()); }  should we also mock the ostream similarly?
  • 42. Call Tracing Mocks 38 © Peter Sommerlad  We want also to count how often our dice are rolled. How to test this? struct MockWinningDice:DieInterface{ int rollcounter; MockWinningDice():rollcounter(0){} int roll(){++rollcounter; return 4;} }; void testWinningGame() { MockWinningDice d; GameFourWins game(d); std::ostringstream os; game.play(os); ASSERT_EQUAL("You won!n",os.str()); ASSERT_EQUAL(1,d.rollcounter); game.play(os); ASSERT_EQUAL(2,d.rollcounter); }
  • 43. Using C++ template Parameters for Mocking 39 © Peter Sommerlad  C++ template parameters can be used for mocking without virtual member function overhead and explicit interface extraction.  no need to pass object in as additional parameter  unfortunately no default template parameters for template functions (yet)  You can mock  Member Variable Types  Function Parameter Types  Mocking without template inline/export need is possible through explicit instantiations
  • 44. Mockator Demo - Compile Seam using Templates 40 © Peter Sommerlad  Extract Template Parameter  part of CUTE plug-in  Use Template in Test  introduce Mock object for DIE  create test double class...  add missing member function  implement test double code  Add Mock Object Support  check for expected calls (C++11)  additional features  dependency injection through  templates  abstract interface classes
  • 45. Mockator - Preprocessor Seam 41 © Peter Sommerlad  mockator_malloc.h #ifndef MOCKATOR_MALLOC_H_ #define MOCKATOR_MALLOC_H_ #include <cstdlib > int mockator_malloc(size_t size, const char *fileName, int lineNumber); #define malloc(size_t size) mockator_malloc((size),__FILE__,__LINE__) #endif  mockator_malloc.c #include "mockator_malloc.h" #undef malloc int mockator_malloc(size_t size, const char * fileName, int lineNumber) { //TODO your tracing code here return malloc(size); }  Generating trace functions like this one is easy: Ctrl+Alt+T (on Linux)  Mockator passes mockator_malloc.h to the GNU compiler by using its -include option
  • 46. Mockator - Link Seam - Shadowing & Wrapping Library Functions 42 © Peter Sommerlad  Shadowing on Linux only using GCC Linker  Allows wrapping of functions in shared libraries  Both Linux and MacOS X supported  Mockator does all the hard work like  creating a shared library project,  adding dependencies to the dl library,  creating run-time configurations with the following env values, etc.,  Linux: LD PRELOAD=libName.so MacOS X:  DYLD FORCE FLAT NAMESPACE=1 int foo(int i) { static void *gptr = nullptr;  DYLD INSERT LIBRARIES=libName.dylib if(!gptr) gptr = dlsym(RTLD_NEXT,"_Z3fooi"); typedef int (*fptr)(int); fptr my_fptr = reinterpret_cast<fptr>(gptr); // TODO put your code here return my_fptr(i); }
  • 47. Questions ?  http://cute-test.com (Mockator will be here 3.Q2012) Have Fun with TDD  http://linticator.com http://includator.com  http://sconsolidator.com (M. Rüegg as well) Mockator and  peter.sommerlad@hsr.ch http://ifs.hsr.ch Refactoring! 43 © Peter Sommerlad