Practical Examples of Decision-Based Test Cases
- NxtGen QA

- Oct 7, 2024
- 2 min read
Updated: Oct 8, 2024
Decision-based testing involves writing test cases that verify the outcomes of logical decisions within the code. These decisions often control the flow of the application, making it crucial to test both the positive and negative paths.
1. Testing an If-Else Statement
Consider a simple if-else statement that checks whether a user is logged in:
To achieve decision coverage, you need to write two test cases:
Test Case 1 (True Condition): Simulate a scenario where the user is logged in. The expected result is that the dashboard is shown.
Test Case 2 (False Condition): Simulate a scenario where the user is not logged in. The expected result is that the login screen is shown.
2. Testing a Switch Statement
Consider a switch statement that processes different user roles:
To test all branches of this switch statement, you need the following test cases:
Test Case 1 (Admin Role): Simulate a user with the "admin" role and verify that admin access is granted.
Test Case 2 (Editor Role): Simulate a user with the "editor" role and verify that editor access is granted.
Test Case 3 (Viewer Role): Simulate a user with the "viewer" role and verify that viewer access is granted.
3. Testing Complex Logical Conditions
For more complex conditions involving multiple variables, such as:
To achieve decision coverage, you need to account for all combinations of the two conditions (age >= 18 and hasValidID):
Test Case 1 (Both True): Test when the user is over 18 and has a valid ID, ensuring that entry is allowed.
Test Case 2 (Age True, ID False): Test when the user is over 18 but does not have a valid ID, ensuring that entry is denied.
Test Case 3 (Age False, ID True): Test when the user is under 18 but has a valid ID, ensuring that entry is denied.
Test Case 4 (Both False): Test when the user is under 18 and does not have a valid ID, ensuring that entry is denied.
4. Using Decision Tables for Complex Scenarios
In scenarios where multiple conditions affect the outcome, decision tables can help organize the test cases. A decision table lists all possible combinations of conditions and the corresponding actions that should be taken.
Conclusion
Decision-based test cases are essential for ensuring that all logical branches in the code are exercised. By writing test cases for both true and false outcomes of each decision, teams can catch bugs that might only appear under specific conditions.







Comments