Lesson 19 of 20Practical Modern C++25 min

Debugging and Testing in Visual Studio

Debugging helps you understand why a program behaves differently from what you expected. Testing checks whether important inputs produce the correct results.

A Practical Debugging Workflow

Useful Visual Studio Tools

ToolUse
BreakpointPauses execution at a selected line.
Step OverRuns the current line without entering called functions.
Step IntoMoves into a called function.
Locals / AutosShows values currently in scope.
WatchTracks expressions or variables you select.
Call StackShows the chain of function calls.

Assertions

Simple assertion
#include <cassert>

int calculateDiscount(int percentage)
{
    assert(percentage >= 0 && percentage <= 100);
    return percentage;
}

Small Test Cases

Testing a function
int add(int a, int b)
{
    return a + b;
}

int main()
{
    assert(add(2, 3) == 5);
    assert(add(-1, 1) == 0);
    assert(add(0, 0) == 0);
}
Testing habit

Include normal, boundary, and invalid inputs. For a mark, useful boundaries include 0, 50, and 100.

Lesson Summary

Use breakpoints, stepping, inspection, assertions, and small test cases.