Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Test-Driven Development with C++

You're reading from   Test-Driven Development with C++ A simple guide to writing bug-free Agile code

Arrow left icon
Product type Paperback
Published in Nov 2022
Publisher Packt
ISBN-13 9781803242002
Length 430 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Abdul Wahid Tanner Abdul Wahid Tanner
Author Profile Icon Abdul Wahid Tanner
Abdul Wahid Tanner
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1: Testing MVP
2. Chapter 1: Desired Test Declaration FREE CHAPTER 3. Chapter 2: Test Results 4. Chapter 3: The TDD Process 5. Chapter 4: Adding Tests to a Project 6. Chapter 5: Adding More Confirm Types 7. Chapter 6: Explore Improvements Early 8. Chapter 7: Test Setup and Teardown 9. Chapter 8: What Makes a Good Test? 10. Part 2: Using TDD to Create a Logging Library
11. Chapter 9: Using Tests 12. Chapter 10: The TDD Process in Depth 13. Chapter 11: Managing Dependencies 14. Part 3: Extending the TDD Library to Support the Growing Needs of the Logging Library
15. Chapter 12: Creating Better Test Confirmations 16. Chapter 13: How to Test Floating-Point and Custom Values 17. Chapter 14: How to Test Services 18. Chapter 15: How to Test With Multiple Threads 19. Index 20. Other Books You May Enjoy

What should a test look like?

It should be as simple to write a test as it is to declare and write a function, and we should be able to simplify things even further. A normal function can have whatever return type you want, a name, a set of parameters, and a body of code.

A function is also something that you write so that it can be called by other code. This code should know what the function does, what it returns, and what arguments need to be passed. We’ll keep things simple for our test functions and only worry about the name for now.

We want each test function to have its own name. Otherwise, how would we be able to keep track of all the various tests we’ll eventually be writing? As for the return type, we haven’t identified an actual need yet, so we’ll use void.

You’ll learn more about this process in Chapter 3, The TDD Process. When using TDD, don’t get ahead of yourself. Only do what you need to do at the time. As with the void return type, we’ll also not have any parameters.

It might seem too simple but this is a good start. So far, a test is nothing more than a function, which returns nothing and takes no parameters. It has a name to identify it and will include whatever code is needed to run the test.

Because we’re going to start using TDD to help design a simple testing library, our first test should ensure that we can create a test. This is a simple start, which defines a test function and calls it from main. All of this is in a single file called main.cpp:

#include <iostream>
void testCanBeCreated ()
{
    std::cout << "testCanBeCreated" << std::endl;
}
int main ()
{
    testCanBeCreated();
    return 0;
}

You might be thinking that this is not a test, it’s just a function that prints its own name, and you’d be right. We’re going to build it up from the very beginning in an agile manner using only what we have available. Right now, we don’t have a test library to use yet.

Still, this is starting to resemble what we eventually want. We want a test to be just like writing a function. If you build and run the project now, the output is as expected:

testCanBeCreated
Program ended with exit code: 0

This shows the output from running the program. It displays the name of the function. The text in the second line actually comes from my development tools and shows the program exit code. The exit code is the value returned from main.

This is a start but it can be improved. The next section will look at what information a test needs, such as its name.

You have been reading a chapter from
Test-Driven Development with C++
Published in: Nov 2022
Publisher: Packt
ISBN-13: 9781803242002
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime