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
- Reproduce the problem with a small, clear input.
- Set a breakpoint before the suspected line.
- Start debugging and inspect variable values.
- Step over one statement at a time.
- Correct the cause, rebuild, and repeat the test.
Useful Visual Studio Tools
| Tool | Use |
|---|---|
| Breakpoint | Pauses execution at a selected line. |
| Step Over | Runs the current line without entering called functions. |
| Step Into | Moves into a called function. |
| Locals / Autos | Shows values currently in scope. |
| Watch | Tracks expressions or variables you select. |
| Call Stack | Shows 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.