⚙️ C++

Pruebas de código en C++: guía detallada

What is it and why? (a real-life analogy)

Imagine you wrote a program — a calculator that should add, subtract, multiply, and divide numbers. You spent several hours, wrote the code, ran it... and it works! But how can you be sure it works correctly? Maybe when dividing by zero, the program gives a strange answer? Or when adding 0.1 + 0.2, the result is not 0.3 but 0.30000000000000004? (Spoiler: in C++ it will be like that if you don't know how to work correctly with floating-point numbers).

In real life, we constantly check things. When you buy a new phone, you turn it on, check the screen, camera, speakers. When a chef cooks soup, he tastes it with a spoon — is it oversalted? When a builder builds a bridge, he checks every beam, every bolt. This is testing.

In programming, testing is the process of verifying that the code does exactly what we expect it to do. And it's not just "run it and look." It's a whole science with rules, tools, and methodologies.

💡 Important note: Testing does not guarantee that there are no errors in the code. It only shows that the code works correctly in the tested situations. As the famous scientist Edsger Dijkstra said: "Testing can prove the presence of bugs, but not their absence."

Why is testing needed? Let's list the main reasons:

  • Saves time. Finding an error a month after writing the code is a nightmare. You've already forgotten how that piece of the program works, and you'll have to re-read everything from scratch. Tests help catch the error immediately.
  • Confidence in the code. When you change something in the program (add a new function or fix an old one), tests show whether anything else broke.
  • Documentation. Tests are living documentation. If someone else wants to understand your code, they can look at the tests and understand how each function should work.
  • Professionalism. In serious companies, code without tests is not accepted. It's like taking an exam without preparation — you might pass, but the chances of success are slim.

Theory: how it works from the inside

Now let's figure out what types of testing exist and how they work "under the hood."

Testing levels

Imagine you are building a house. First, you check each brick (unit testing), then you check how the bricks connect into walls (integration testing), then you check the entire finished room (system testing), and finally — you show the house to the customer (acceptance testing).

In programming, the levels are similar:

Level What we check Example
Unit Individual functions, classes, methods Check that the addition function correctly adds 2 and 3
Integration Interaction between modules Check that the salary calculation function correctly retrieves data from the database
System The entire program as a whole Run the game and check that all levels are passable
Acceptance Compliance with customer requirements Check that the program works on Windows 10 and 11

Types of tests by verification method

There are two main approaches: manual testing (a person runs the program and clicks buttons) and automated testing (code checks code). As programmers, we are interested in the latter.

Automated tests are divided into:

  • Positive — check that the program works under normal conditions. For example, a calculator should correctly add 2 + 2.
  • Negative — check that the program reacts correctly to errors. For example, a calculator should not crash when dividing by zero, but should display an error message.
  • Boundary — check at the boundaries of valid values. For example, if a function accepts numbers from 0 to 100, you need to check 0, 100, -1, 101.

How a test framework works

In C++, there are several libraries for testing. The most popular one is Google Test (or gtest). It works on the principle of "assertions." You write: "I assert that the function sum(2, 3) will return 5." If this is true — the test passes. If not — the test fails, and you see what went wrong.

Internally, the framework does approximately the following:

  1. Runs your function with the given parameters.
  2. Compares the result with the expected one.
  3. If the result matches — silently moves to the next test.
  4. If it doesn't match — records the error in the report and continues testing.

This is like an examiner checking your answers against a key. Only the examiner is a program, and it checks not students, but other programs.

Syntax in C++: all forms and variants

Let's look at how to write tests in C++ using Google Test. This is the most popular library, and it is installed almost everywhere. If you don't have it — no problem, we'll also look at a simple homemade option.

Installing Google Test

The easiest way is to use the vcpkg or Conan package manager. But for a start, you can simply download the source code from GitHub and add it to your project. We won't go deep into installation — that's a topic for a separate article.

Basic test structure

In Google Test, each test is a TEST() macro. It takes two parameters: the test suite name and the specific test name.

#include