Test-Driven Development with C++

Book description

Learn how to write a simple testing framework and extend it to drive the design of your logging library

Key Features

  • Learn how to solve various challenges when testing in C++ with the help of effective solutions
  • Develop a logging library with enhancements
  • Drive better code designs with effective tests

Book Description

Modern, standard C++ is all that is needed to create a small and practical testing framework that will improve the design of any project. This allows you to think about how the code will be used, which is the first step in designing intuitive interfaces. TDD is a modern balanced software development approach that helps to create maintainable applications, provide modularity in design, and write minimal code that drastically reduces defects. With the help of this book, you'll be able to continue adding value when designs need to change by ensuring that the changes don't break existing tests.

In this book, developers working with test-driven development (TDD) will be able to put their knowledge to work by writing a simple testing framework and then using it to drive the design of a logging library. The book will help you enhance your software development skills with test cases. You'll understand how to design and implement test cases. The chapters will also show you how to utilize the TDD approach to be more productive in software development than attempting to code in large unstructured steps.

By the end of this book, you'll have gained knowledge of TDD and testing and also built a working logging library.

What you will learn

  • Understand how to develop software using TDD
  • Keep the code for the system as error-free as possible
  • Refactor and redesign code confidently
  • Communicate the requirements and behaviors of the code with your team
  • Understand the differences between unit tests and integration tests
  • Use TDD to create a minimal viable testing framework

Who this book is for

This book is for C++ developers already familiar with and using C++ for daily tasks who want to improve their skillset. You don't need to be an expert but you should already have some knowledge of modern C++ and how to use templates to get the most out of this book.

Table of contents

  1. Test-Driven Development with C++
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1: Testing MVP
  7. Chapter 1: Desired Test Declaration
    1. Technical requirements
    2. What do we want tests to do for us?
    3. What should a test look like?
    4. What information does a test need?
    5. How can we use C++ to write tests?
    6. How will the first test be used?
    7. Summary
  8. Chapter 2: Test Results
    1. Technical requirements
    2. Reporting a single test result
    3. Enhancing the test declaration to support multiple tests
    4. Summarizing the results
    5. Redirecting the output results
    6. Summary
  9. Chapter 3: The TDD Process
    1. Technical requirements
    2. Build failures come first
    3. Do only what is needed to pass
    4. Enhancing a test and getting another pass
    5. Summary
  10. Chapter 4: Adding Tests to a Project
    1. Technical requirements
    2. How to detect whether a test passes or fails
    3. Enhancing the testing library to support assertions
    4. Should error cases be tested, too?
    5. Summary
  11. Chapter 5: Adding More Confirm Types
    1. Technical requirements
    2. Fixing the bool confirms
    3. Confirming equality
    4. Decoupling test failures from line numbers
    5. Adding more confirm types
    6. Confirming string literals
    7. Confirming floating point values
    8. How to write confirms
    9. Summary
  12. Chapter 6: Explore Improvements Early
    1. Technical requirements
    2. Getting line numbers without macros
    3. Exploring lambdas for tests
    4. Summary
  13. Chapter 7: Test Setup and Teardown
    1. Technical requirements
    2. Supporting test setup and teardown
    3. Enhancing test setup and teardown for multiple tests
    4. Handling errors in setup and teardown
    5. Summary
  14. Chapter 8: What Makes a Good Test?
    1. Technical requirements
    2. Making tests easy to understand
    3. Keeping tests focused on specific scenarios
    4. Use random behavior only in this way
    5. Only test your project
    6. Test what should happen instead of how
    7. Summary
  15. Part 2: Using TDD to Create a Logging Library
  16. Chapter 9: Using Tests
    1. Technical requirements
    2. Why build a logging library?
    3. How will TDD help build a logging library?
    4. What would the ideal logging library look like?
    5. Starting a project using TDD
    6. Logging and confirming the first message
    7. Adding timestamps
    8. Constructing log messages with streams
    9. Summary
  17. Chapter 10: The TDD Process in Depth
    1. Technical requirements
    2. Finding gaps in the testing
    3. Adding log levels
    4. Adding default tag values
    5. Exploring filtering options
    6. Adding new tag types
    7. Refactoring the tag design with TDD
    8. Designing tests to filter log messages
    9. Controlling what gets logged
    10. Enhancing filtering for relative matches
    11. When is testing too much?
    12. How intrusive should tests be?
    13. Where do integration or system tests go in TDD?
    14. What about other types of tests?
    15. Summary
  18. Chapter 11: Managing Dependencies
    1. Technical requirements
    2. Designing with dependencies
    3. Adding multiple logging outputs
    4. Summary
  19. Part 3: Extending the TDD Library to Support the Growing Needs of the Logging Library
  20. Chapter 12: Creating Better Test Confirmations
    1. Technical requirements
    2. The problem with the current confirmations
    3. Simplifying string confirmations
    4. Enhancing the test library to support Hamcrest matchers
    5. Adding more Hamcrest types
    6. Summary
  21. Chapter 13: How to Test Floating-Point and Custom Values
    1. Technical requirements
    2. More precise floating-point comparisons
    3. Adding floating-point Hamcrest matchers
    4. Writing custom Hamcrest matchers
    5. Summary
  22. Chapter 14: How to Test Services
    1. Technical requirements
    2. Service testing challenges
    3. What can be tested in a service?
    4. Introducing the SimpleService project
    5. Summary
  23. Chapter 15: How to Test With Multiple Threads
    1. Technical requirements
    2. Using multiple threads in tests
    3. Making the logging library thread-safe
    4. The need to justify multiple threads
    5. Changing the service return type
    6. Making multiple service calls
    7. How to test multiple threads without sleep
    8. Fixing one last problem detected with logging
    9. Summary
  24. Index
    1. Why subscribe?
  25. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Test-Driven Development with C++
  • Author(s): Abdul Wahid Tanner
  • Release date: November 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803242002