Test-Driven Development with C++ - Abdul Wahid Tanner - E-Book

Test-Driven Development with C++ E-Book

Abdul Wahid Tanner

0,0
33,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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, you will use test-driven development (TDD) to gain practical skills 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 with unique features not found in other libraries.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 575

Veröffentlichungsjahr: 2022

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Test-Driven Development with C++

A simple guide to writing bug-free Agile code

Abdul Wahid Tanner

BIRMINGHAM—MUMBAI

Test-Driven Development with C++

Copyright © 2022 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Gebin George

Publishing Product Manager: Kunal Sawant

Senior Editor: Nithya Sadanandan

Technical Editor: Jubit Pincy

Copy Editor: Safis Editing

Project Coordinator: Deeksha Thakkar

Proofreader: Safis Editing

Indexer: Rekha Nair

Production Designer: Shyam Sundar Korumilli

Developer Relations Marketing Executive: Sonakshi Bubbar

First published: October 2022

Production reference: 1211022

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80324-200-2

www.packt.com

Focus on constant improvement

– Abdul Wahid Tanner

Contributors

About the author

I'm Abdul Wahid Tanner and have been programming C++ since 1992 and teaching others how to code for almost as long. I first became interested in TDD around 2005 and decided to implement a board game that I liked playing using strict TDD. At the time, I was using C# and a test framework that was able to find and run the tests. It wasn't until about 2014 that I decided to see whether I could use C++ and TDD together. There were many choices for tools that helped run C++ tests, but they were big and full of features. I wanted something small and simple, so I wrote a unit test library in about 650 lines of code. The code in this book is a complete and better rewrite of that early code. I hope you find the code in this book useful and decide to use it in your projects. However, even if you decide to use one of the commercial tools, you should still benefit from the understanding that this book provides about how the tools work and how to follow the TDD process.

About the reviewer

Serban Stoenescu is a software developer specializing in C++. He is currently working for Everseen on some projects that require OpenCV and some machine learning. In the past he has worked for SkAD Labs on a CAD project with OpenCASCADE, Atigeo on Big Data projects in Java, and Alcatel-Lucent (now part of Nokia). At Alcatel-Lucent, he was also a trainer, teaching Rational ROSE. He is the co-author of “Towards the Impact of Design Flaws on the Resources Used by an Application”, a paper at ARMS-CC 2014. Currently, he is also a Udemy instructor, his most popular course being “C++ Unit Testing: Google Test and Google Mock”. His other courses are “CMake from Zero to Hero” and “C++ Machine Learning Algorithms Inspired by Nature”.

Table of Contents

Preface

Part 1: Testing MVP

1

Desired Test Declaration

Technical requirements

What do we want tests to do for us?

What should a test look like?

What information does a test need?

How can we use C++ to write tests?

How will the first test be used?

Summary

2

Test Results

Technical requirements

Reporting a single test result

Enhancing the test declaration to support multiple tests

Summarizing the results

Redirecting the output results

Summary

3

The TDD Process

Technical requirements

Build failures come first

Do only what is needed to pass

Enhancing a test and getting another pass

Summary

4

Adding Tests to a Project

Technical requirements

How to detect whether a test passes or fails

Enhancing the testing library to support assertions

Should error cases be tested, too?

Summary

5

Adding More Confirm Types

Technical requirements

Fixing the bool confirms

Confirming equality

Decoupling test failures from line numbers

Adding more confirm types

Confirming string literals

Confirming floating point values

How to write confirms

Summary

6

Explore Improvements Early

Technical requirements

Getting line numbers without macros

Exploring lambdas for tests

Summary

7

Test Setup and Teardown

Technical requirements

Supporting test setup and teardown

Enhancing test setup and teardown for multiple tests

Handling errors in setup and teardown

Summary

8

What Makes a Good Test?

Technical requirements

Making tests easy to understand

Keeping tests focused on specific scenarios

Use random behavior only in this way

Only test your project

Test what should happen instead of how

Summary

Part 2: Using TDD to Create a Logging Library

9

Using Tests

Technical requirements

Why build a logging library?

How will TDD help build a logging library?

What would the ideal logging library look like?

Starting a project using TDD

Logging and confirming the first message

Adding timestamps

Constructing log messages with streams

Summary

10

The TDD Process in Depth

Technical requirements

Finding gaps in the testing

Adding log levels

Adding default tag values

Exploring filtering options

Adding new tag types

Refactoring the tag design with TDD

Designing tests to filter log messages

Controlling what gets logged

Enhancing filtering for relative matches

When is testing too much?

How intrusive should tests be?

Where do integration or system tests go in TDD?

What about other types of tests?

Summary

11

Managing Dependencies

Technical requirements

Designing with dependencies

Adding multiple logging outputs

Summary

Part 3: Extending the TDD Library to Support the Growing Needs of the Logging Library

12

Creating Better Test Confirmations

Technical requirements

The problem with the current confirmations

Simplifying string confirmations

Enhancing the test library to support Hamcrest matchers

Adding more Hamcrest types

Summary

13

How to Test Floating-Point and Custom Values

Technical requirements

More precise floating-point comparisons

Adding floating-point Hamcrest matchers

Writing custom Hamcrest matchers

Summary

14

How to Test Services

Technical requirements

Service testing challenges

What can be tested in a service?

Introducing the SimpleService project

Summary

15

How to Test With Multiple Threads

Technical requirements

Using multiple threads in tests

Making the logging library thread-safe

The need to justify multiple threads

Changing the service return type

Making multiple service calls

How to test multiple threads without sleep

Fixing one last problem detected with logging

Summary

Index

Other Books You May Enjoy

Part 1: Testing MVP

This book is divided into three parts. In this first part, you’ll learn why Test-Driven Development is important and how to use it to help you design and write software. We’ll be starting with an empty project and using Test-Driven Development practices to design and build a unit test library. You’ll learn everything you need by following along as we explore ideas and build a working project one step at a time.

The following chapter will be covered in this part:

Chapter 1, Desired Test DeclarationChapter 2, Test ResultsChapter 3, The TDD processChapter 4, Adding Tests to a ProjectChapter 5, Adding More Confirm TypesChapter 6, Explore Improvements EarlyChapter 7, Test Setup and TeardownChapter 8, What Makes a Good Test?