📖 Scenario: You are part of a software development team. Your team wants to improve the quality of the code by testing it thoroughly. You will learn how to apply white-box testing techniques to check the internal workings of a simple function.
🎯 Goal: Build a step-by-step understanding of white-box testing techniques by creating a simple function, setting up test conditions, applying a white-box testing method, and completing the test coverage.
📋 What You'll Learn
Create a simple function with conditional logic
Define a variable to track test coverage
Apply a white-box testing technique (e.g., statement coverage)
Complete the test setup to ensure all paths are tested
💡 Why This Matters
🌍 Real World
White-box testing helps developers find bugs by checking the internal logic of their code. It ensures all parts of the code work as expected.
💼 Career
Software testers and developers use white-box testing techniques to improve software quality and reliability before release.
Progress0 / 4 steps
1
Create a simple function with conditional logic
Write a Python function called check_number that takes one parameter num. The function should return the string 'Positive' if num is greater than zero, 'Zero' if num is zero, and 'Negative' if num is less than zero.
Software Engineering
Hint
Use if, elif, and else statements to check the value of num.
2
Define a variable to track test coverage
Create a variable called coverage and set it to an empty list. This list will store the names of the branches tested in the function.
Software Engineering
Hint
Use an empty list [] to start tracking coverage.
3
Apply a white-box testing technique (statement coverage)
Write three lines of code that call check_number with the values 5, 0, and -3. After each call, append the string 'Positive', 'Zero', or 'Negative' respectively to the coverage list to track which branches have been tested.
Software Engineering
Hint
Call the function with each test value and append the result to coverage.
4
Complete the test setup to ensure all paths are tested
Add a line of code that creates a set called tested_branches from the coverage list to show unique branches tested. This helps confirm all branches in the function have been covered.
Software Engineering
Hint
Use the set() function to get unique tested branches.
Practice
(1/5)
1. Which of the following best describes white-box testing?
easy
A. Testing that examines the internal structure of the code
B. Testing that focuses only on user interface behavior
C. Testing done without knowledge of the code
D. Testing that only checks software performance
Solution
Step 1: Understand white-box testing definition
White-box testing involves looking inside the code to test its logic and structure.
Step 2: Compare options to definition
Testing that examines the internal structure of the code matches this definition exactly, while others focus on external or unrelated aspects.
Final Answer:
Testing that examines the internal structure of the code -> Option A
Quick Check:
White-box testing = internal code check [OK]
Hint: White-box tests look inside the code, not just outside [OK]
Common Mistakes:
Confusing white-box with black-box testing
Thinking white-box tests only check UI
Assuming white-box ignores code structure
2. Which syntax correctly represents a common white-box testing technique?
easy
A. User acceptance testing
B. Branch coverage
C. Load testing
D. Smoke testing
Solution
Step 1: Identify white-box testing techniques
Common white-box techniques include statement, branch, path, and condition coverage.
Step 2: Match options to white-box techniques
Only branch coverage is a white-box technique; others are types of testing but not white-box.
Final Answer:
Branch coverage -> Option B
Quick Check:
Branch coverage = white-box technique [OK]
Hint: Branch coverage is a classic white-box technique [OK]