Test-Driven Development
in C++


Dresden, Nov 13, 2014
Peter Steinbach
Scionics Computer Innovation GmbH

Disclaimer

This work is licensed under a
Creative Commons Attribution 4.0
International License
.

Creative Commons License


All material contained in the slides are linked to their source if not produced by the author. If you find intellectual property is not attributed to your satisfaction, feel free to contact me.

For feedback, forks, discussions and contributions, go to

github.com/psteinb/TDD_in_cpp.

How people code

First

http://commons.wikimedia.org/wiki/File:Pictofigo_-_Idea.png

Idea(s)

Second

www.jeffreydev.com/the-developer-title

Coffee & Code

Third

en.wikipedia.org/wiki/STS-115

Launch To Mars

Validate

http://en.wikipedia.org/wiki/Moon

Unexpected Result!

Compile and run?

Build and Fix Software Model

Build and Fix
from Software Process Models (Lecture by Colin Potts, Gerogia Tech, 1998)

Code in Science

blog.f1000research.com

Reproducibility?

Preclinical Cancer Research

Nature Comment
from Nature 2012

Preclinical Cancer Research

Nature Comment
from [doi:10.1038/483531a]

Scientific Computing?

SciPy2014
Scipy 2014: Reproducible Research

Test-Driven
Development

by Kent Beck (2002)

How?

by Nat Pryce

Live Demo


Writing a vector with a norm!

Classical Approach

Unit Test
method to test smallest testable part of an application

Test Suite
sequence of Unit Tests that validate the same entity

Test Fixture
called before/after execution of unit test to setup or tear down test environment

What to test?

Let's play!

Test the Contract!


  • pre-condition
    (type and content of input data)
  • service it provides
    (the responsibility it has)
  • post-condition
    (type and content of output data)


-- Thomas/Hunt, The Pragmatic Programmer, Addison-Wesley Professional, 1999

Exercise?

cyber-dojo.org

Summary


  • incremental design
  • developer confidence
  • less feature envy
  • reproducibility included
  • documentation included
  • continuous integration
  • ...



  • code is less bug free
  • human interaction?
  • dead code?
  • can be misleading!
  • hinders good design
  • ...


Ongoing Discussion!

Tools

from wikipedia

Standard Libraries

Standard xUnit Approach

from junit.sourceforge.net

State-of-the-Art Libraries

Meta-Programming


Code Inlined

//test inside the code in C++11 or with BOOST_STATIC_ASSERT in C++03
std::static_assert( sizeof(small_t) < sizeof(large_t) );
				      


Boost MPL Test Asserts

#include "boost/mpl/assert.hpp"

BOOST_MPL_ASSERT( sizeof(small_t) < sizeof(large_t) );

BOOST_MPL_ASSERT_MSG( sizeof(small_t) < sizeof(large_t) , 
                      MESSAGE_THAT_WILL_BE_PRINTED);

BOOST_MPL_ASSERT_NOT(( boost::is_same< small_t,large_t > ));

BOOST_MPL_ASSERT_RELATION( sizeof(small_t), <, sizeof(large_t) );
	    

Summary

  • real-world test-driven development has a learning curve
  • creates confidence and reproducibility
  • good software design step-by-step
  • a lot of tools available
  • no silver bullet!

Thank you for your attention!

from wikimedia.org

Literature

  • Robert C. Martin, Agile Software Development: Principles, Patterns and Practices. Pearson Education, 2002
  • Kent Beck, Test-Driven Development by Example. Addison-Wesley Longman, 2002
  • Overload Magazine, Issue 122
  • "Modern C++ Testing" by Phil Nash (author of catch)