small medium large xlarge

Modern C++ Programming with Test-Driven Development: Code Better, Sleep Better


Cover image for Modern C++ Programming with Test-Driven Development

Modern C++ Programming with Test-Driven Development

Code Better, Sleep Better


If you program in C++ you’ve been neglected. Test-driven development (TDD) is a modern software development practice that can dramatically reduce the number of defects in systems, produce more maintainable code, and give you the confidence to change your software to meet changing needs. But C++ programmers have been ignored by those promoting TDD—until now. In this book, Jeff Langr gives you hands-on lessons in the challenges and rewards of doing TDD in C++.

  • Examples presented in the book were built under C++11 gcc 4.7.2.
  • Google Mock 1.6 (Most examples use Google Mock, but it isn’t difficult to translate them to your tool of choice)
  • CMakeLists.txt files are provided for each project. Examples provided were built using cmake version 2.8.9.
  • Various freely-available third-party libraries are used as the basis for examples in the book. These include:
    – cURL (one example)
    – JsonCpp (one example)
    – Boost (filesystem, date_time/gregorian, algorithm, assign) (several examples)

Customer Reviews

Jeff Langr has written another excellent book. This time he brings Test-Driven
Development to the world of C++. Jeff’s starting example brings us face to face
with the stark simplicity of good TDD. He explains why we work that way and
then provides key practical details, all the way through test doubles, working with
legacy code, threading, and more. This one is a keeper for anyone working with

- Ron Jeffries

Co-creator of the Extreme Programming methodology

Jeff Langr has written the best C++ book in years. Modern C++ Programming with
Test-Driven Development
is the perfect mixture of theory and practice. The abstractions
are explained with clarity and gusto, but the details are right there when
you need them. It’s sure to be an instant classic, in C++ and in TDD both.

- Michael D. Hill

XP coach and writer

Jeff is an expert software craftsman, and in this book he shares his considerable
wisdom about crafting great software. This book is not about testing, though you
will learn valuable testing techniques. It is about improving your skills, code,
products, and life through test-driving. Whether you’re a novice, expert, or in between,
Jeff will show you the how and the why of an expert who test-drives better
products with C++.

- James W. Grenning

Author of "Test-Driven Development for Embedded C"

See All Reviews

Choose Your Format(s)

  • $26.00 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

About this Title

Pages: 366
Published: 2013-10-30
Release: P1.0 (2013-10-30)
ISBN: 978-1-93778-548-2

Modern C++ Programming With Test-Driven Development, the only comprehensive treatment on TDD in C++ provides you with everything you need to know about TDD, and the challenges and benefits of implementing it in your C++ systems. Its many detailed code examples take you step-by-step from TDD basics to advanced concepts. As a veteran C++ programmer, you’re already writing high-quality code, and you work hard to maintain code quality. It doesn’t have to be that hard.

In this book, you’ll learn:

  • how to use TDD to improve legacy C++ systems
  • how to identify and deal with troublesome system dependencies
  • how to do dependency injection, which is particularly tricky in C++
  • how to use testing tools for C++ that aid TDD
  • C++11 features that facilitate TDD

As you grow in TDD mastery, you’ll discover how to keep a massive C++ system from becoming a design mess over time, as well as particular C++ trouble spots to avoid. You’ll find out how to prevent your tests from being a maintenance burden and how to think in TDD without giving up your hard-won C++ skills. Finally, you’ll see how to grow and sustain TDD in your team.

Whether you’re a complete unit-testing novice or an experienced tester, this book will lead you to mastery of test-driven development in C++.

Q&A with Jeff Langr, author of Modern C++ Programming with Test-Driven Development

Why did you write Modern C++ Programming with Test-Driven Development?

I’d been working with a number of C++ development teams recently that were very interested in using test-driven development (TDD) to get their source under control and whip it into a better shape. I thought, “If any language needs a way to safely accommodate continual, incremental change, it’s C++ .” There’s a dearth of material written on TDD in C++, and I figure I’d see what I could do to help.

Aren’t you being a bit bold with that book title?

Thanks for noticing. Sure, TDD isn’t the only way to do things, but I believe it’s the best technique we have in 2013 to help you succeed with programming, whether your language is C++ or something else. " Modern C++ " to me is about harnessing the great power of the language (including some nice, new C++11 features) with a mechanism that will allow you to do so safely and help you to craft a great design along the way.

If I already know TDD, what might I get out of this book?

I wanted Modern C++ Programming with Test-Driven Development to be the most up-to-date, comprehensive source of information on TDD. It covers just about everything you might expect, and adds a few up-to-date topics such as the Mikado Method and the Transformation Priority Premise (TPP). I incorporate my experiences throughout, so you’ll get a bunch of hard-won wisdom based on having practiced TDD for over a dozen years. You’ll also pick up some ideas for growing and sustaining TDD in your team.

Is the TDD material in the book book accessible to someone who isn’t familiar with C++?

While there’s plenty of code in the book, you should have no trouble following the examples if you’ve had any exposure to a C-based object-oriented language (such as Java, C#, or Objective-C). One of the goals in TDD is to structure your code to be expressive, and I strove to ensure the code in the book met that goal. I focus on TDD concepts and techniques that you can apply equally well to other languages.

What if I use a different unit testing tool?

The exercises use a mix of Google Test/Google Mock and CppUTest. However, once you’ve seen a couple unit testing tools, you’ve seen them all—it’s pretty easy to translate tests written in one tool into another (such as CppUnit, CxxTest, or Boost.Test). In the book, you’ll find an appendix that gives you some criteria to help you choose an appropriate unit testing tool for your team.

What You Need

  • A C++ compiler running under Windows or Linux, preferably one that supports C++11. Examples presented in the book were built under gcc 4.7.2.
  • Google Mock 1.6 (downloadable for free; it contains Google Test as well) or an alternate C++ unit testing tool. Most examples in the book are written for Google Mock, but it isn’t difficult to translate them to your tool of choice.
  • A good programmer’s editor or IDE.
  • cmake, preferably. Of course, you can use your own preferred make too. CMakeLists.txt files are provided for each project. Examples provided were built using cmake version 2.8.9.
  • Various freely-available third-party libraries are used as the basis for examples in the book. These include:
    – cURL
    – JsonCpp
    – Boost (filesystem, date_time/gregorian, algorithm, assign)
    Several examples use the boost headers/libraries. Only one example uses cURL and JsonCpp.

Contents & Extracts

  • Foreword
  • Introduction
    • But Can It Work for Me On My System?
    • Who This Book is For
    • What You’ll Need
    • How to Use this Book
    • About “Us”
    • About Me
    • About the C++ Style in this Book
  • Global Setup
  • Test-Driven Development: A First Example excerpt
    • Set Up
    • The Soundex Class
    • Getting Started
    • Fixing Unclean Code
    • Incrementalism
    • Fixtures and SetUp
    • Thinking and TDD
    • Limiting Length
    • Dropping Vowels
    • Doing What It Takes To Clarify Tests
    • Testing Outside the Box
    • Refactoring to Single Responsibility Functions
    • Finishing Up
    • What Tests Are We Missing?
    • Our Solution
    • The Soundex Class
    • Tear Down
  • Test-Driven Development Foundations
    • Set Up
    • Unit Test and TDD Fundamentals
    • The TDD Cycle: Red-Green-Refactor
    • The Three Rules of TDD
    • Getting Green on Red
    • Mindsets for Success
    • Mechanics for Success
    • Tear Down
  • Test Construction
    • Set Up
    • Organization
    • Fast Tests, Slow Tests, Filters, and Suites
    • Assertions
    • Inspecting Privates
    • Testing vs. Test-Driving: Parameterized Tests and Other Toys
    • Tear Down
  • Test Doubles excerpt
    • Set Up
    • Dependency Challenges
    • Test Doubles
    • A Hand-Crafted Test Double
    • Improving Test Abstraction When Using Test Doubles
    • Using Mock Tools
    • Alternate Dependency Injection Techniques
    • Design Will Change
    • Strategies for Using Test Doubles
    • Miscellaneous Test Double Topics
    • Tear Down
  • Incremental Design
    • Set Up
    • Simple Design
    • Where Is the Up-Front Design?
    • Refactoring Inhibitors
    • Tear Down
  • Quality Tests
    • Set Up
    • Tests Come FIRST
    • One Assert Per Test
    • Test Abstraction
    • Tear Down
  • Legacy Challenges
  • Additional TDD Concepts and Discussions
    • Alternate Approaches
    • Triangulation
    • The TPP
    • Performance
    • Integration Testing
    • Threading
  • Growing and Sustaining TDD
    • Set Up
    • Explaining TDD to Non-Techies
    • The Bad Test Death Spiral, aka The SCUMmy Cycle
    • Pair Programming
    • Katas and Dojos
    • Using the Code Coverage Metric Effectively
    • Continuous Integration
    • Deriving Team Standards for TDD
    • Keeping up With the Community
    • Tear Down
  • Appendix A. Other C++ Unit Testing Tools
  • Appendix B. Code Kata: Roman Numeral Converter
    • Set Up
    • Let’s Go!
    • Practice Makes Perfect
    • Tear Down
  • Bibliography


Jeff Langr wrote Agile in a Flash with Tim Ottinger. It’s a unique set of reference and recommendation cards that you can find tacked to many monitors and walls in agile shops. He has written other books and more than one hundred articles on software development, and trains and consults in software development, including test-driven development.

Upcoming Author Events

  • 2020-05-30: Jeff Langr
    From Mob to Symphony: A Mob Composing Workshop. Designed for non-technical folks who want to understand what mobbing feels like and why it can be great, this session has them collaborating to compose a song using software. (Mob Programming Conference 2020, Boston, MA, US)