Test-Driven Developmentin C++ – How people code – Code in Science



Test-Driven Developmentin C++ – How people code – Code in Science

2 0


TDD_in_Cpp


On Github psteinb / TDD_in_Cpp

Test-Driven Developmentin C++

Dresden, Nov 13, 2014Peter SteinbachScionics Computer Innovation GmbH

Disclaimer

This work is licensed under aCreative Commons Attribution 4.0 International 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

Idea(s)

Second

Coffee & Code

Third

Launch To Mars

Validate

Unexpected Result!

Compile and run?

Build and Fix Software Model

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

Code in Science

Reproducibility?

Preclinical Cancer Research

from Nature 2012

Preclinical Cancer Research

from [doi:10.1038/483531a]

Scientific Computing?

Scipy 2014: Reproducible Research

Test-DrivenDevelopment

by Kent Beck (2002)

How?

by Nat Pryce

Live Demo

Writing a vector with a norm!

Classical Approach

Unit Testmethod to test smallest testable part of an application

Test Suitesequence of Unit Tests that validate the same entity

Test Fixturecalled 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)
0