Week 6 - Debugging and Testing
Learning Goals
- Construct a test table with expected and actual outputs.
- Identify and select relevant test data.
- Use breakpoints in Visual Studio to step through code.
- Use
Console.WriteLineas a debugging tool.
Key Concepts
Types of Test Data
Good testing covers three categories:
| Category | Description | Example for a quantity field |
|---|---|---|
| Normal | Typical valid input | 50 |
| Boundary | Values at the edge of the allowed range | 0, 1, 998, 999 |
| Invalid | Data that should be rejected | -1, 1000, "abc", "" |
Test Tables
A test table records what you expected to happen and what actually happened.
| Test # | Input | Expected Output | Actual Output | Pass/Fail |
|---|---|---|---|---|
| 1 | Name "Cola", Price 3.00, Qty 24 |
Product added successfully | Product added successfully | Pass |
| 2 | Name "" |
Error: Name cannot be blank | Error: Name cannot be blank | Pass |
| 3 | Price -5 |
Error: Price must be > 0 | Product added | Fail |
| 4 | Qty "abc" |
Error: Must be a whole number | Error: Must be a whole number | Pass |
Breakpoints in Visual Studio
A breakpoint pauses the program at a chosen line so you can inspect variables and follow the code step by step.
Basic process:
- Click in the margin beside a line number.
- Run the program with F5.
- Let the program pause at the breakpoint.
- Hover over variables to inspect their values.
- Use F10 to step through the next line.
Debugging Output Statements
You can also print temporary values while the program runs.
static double CalculateTotalValue(double[] prices, int[] quantities)
{
double total = 0;
for (int i = 0; i < prices.Length; i++)
{
double lineValue = prices[i] * quantities[i];
Console.WriteLine($"DEBUG: item {i} = ${lineValue}");
total += lineValue;
}
return total;
}
Your Task
Take your Week 5 solution and complete the following:
Part A - Build a Test Table
Design a test table with at least 8 tests covering:
- normal inputs that should succeed
- boundary values for price and quantity
- invalid inputs for all three fields
Write the test table as a comment block at the top of your program.
Part B - Run the Tests
Run your program and fill in the Actual column. Fix any failures you find.
Part C - Add a Breakpoint
Set a breakpoint inside one of your validation methods. Step through it with F10 while entering a boundary value. Write a short paragraph comment in your code describing what you observed.
Extension
- Deliberately introduce a bug into a calculation method.
- Use a breakpoint and step-through debugging to identify the exact line producing the wrong result.
- Fix the bug and note what the breakpoint revealed.
Common Mistakes to Avoid
- only testing inputs you expect to work
- skipping boundary values
- leaving debug
Console.WriteLinestatements in the finished program - writing the test table after testing instead of before