Testing Functions with Inputs
Students will learn to test functions with various inputs to ensure they produce expected outputs.
About This Topic
Testing is the practice of verifying that code behaves correctly across a range of inputs, including inputs that were not anticipated when the function was written. For 9th graders, this topic reframes debugging from reactive (fixing errors after they occur) to proactive (designing tests that reveal errors before they matter). CSTA 3A-AP-17 asks students to create and evaluate computational artifacts, which includes verifying that those artifacts work as specified.
A critical concept in function testing is the distinction between typical inputs, edge cases, and invalid inputs. A function that calculates letter grades might work correctly for scores of 75 and 90 but fail for 100, 0, or a negative number. Students who only test with values they expect to work often miss the conditions under which their code breaks. Learning to think critically about their own code is a mindset shift that testing activities can establish early.
Active learning through structured peer testing is especially effective here. When students exchange functions and write test cases for each other's code, they approach the function without prior knowledge of what it is supposed to do. This role as an outside tester surfaces both documentation gaps and actual bugs, giving both the tester and the author useful, specific feedback.
Key Questions
- Explain the importance of testing functions with different inputs.
- Design a set of test cases for a given function.
- Identify common errors that can be found by thoroughly testing function inputs and outputs.
Learning Objectives
- Design a comprehensive set of test cases for a given function, including typical, edge, and invalid inputs.
- Evaluate the effectiveness of a function's output against a defined set of expected results for various inputs.
- Identify common programming errors, such as off-by-one errors or incorrect handling of zero, by analyzing function test results.
- Explain the importance of proactive testing in preventing software defects and ensuring code reliability.
Before You Start
Why: Students must understand how to create and use functions before they can test them.
Why: Testing often involves checking different conditions, which relies on understanding how if/else statements work.
Why: Functions operate on data, so students need a foundational understanding of variables and common data types like integers and strings.
Key Vocabulary
| Test Case | A specific set of inputs and expected outputs designed to verify a particular aspect of a function's behavior. |
| Typical Input | A value that is commonly expected and within the normal operating range for a function. |
| Edge Case | An input value that lies at the boundary of valid inputs, often representing extreme or unusual conditions. |
| Invalid Input | A value that is outside the acceptable range or type for a function, which the function should ideally handle gracefully. |
| Assertion | A statement in code that checks if a condition is true; if false, it indicates a failure in the function's logic during testing. |
Watch Out for These Misconceptions
Common MisconceptionIf a function produces the right output for the input I tried, it is working correctly.
What to Teach Instead
A single passing test case confirms the function works for that specific input. It says nothing about edge cases, boundary values, or inputs the author did not think to test. Peer testing activities, where someone else designs the test cases, reliably surface this gap.
Common MisconceptionTesting is only necessary for large or complex programs.
What to Teach Instead
Any function with conditions, loops, or mathematical operations can produce unexpected results for certain inputs. Testing short functions is faster and teaches the discipline that scales to complex programs. Students who test small functions consistently develop the habits that prevent bugs in larger systems.
Common MisconceptionPassing all tests means the code is correct.
What to Teach Instead
Tests can only verify what they check. A function can pass every test case in a suite and still have bugs that no test case covers. This is why choosing good test cases, particularly edge cases, is as important a skill as writing the tests themselves.
Active Learning Ideas
See all activitiesTest Case Design Workshop
Each student writes a function and then designs a test suite with at least six cases: two typical inputs, two boundary values, and two invalid inputs. They trade functions with a partner, who runs the test suite and reports which cases passed and which failed, with notes on what the failure revealed.
Think-Pair-Share: What Could Go Wrong?
Present three function descriptions without code. Students individually list at least four inputs that could cause problems, then compare lists with a partner and categorize inputs by type (valid typical, valid edge, invalid). The class discusses which categories are most often overlooked.
Bug Hunt: Failing Tests
Provide pairs with a function and a set of test cases, some of which fail. They must identify the input pattern that causes failure and trace through the code to find the bug, without modifying the tests. They document their findings as a structured bug report.
Gallery Walk: Test Coverage Analysis
Post four functions with their test suites. Students annotate which inputs are missing from each test suite, writing specific test cases that should be added. The class votes on the most critical missing test for each function and discusses why that case matters.
Real-World Connections
- Software engineers at Google write thousands of automated tests for new features in Android apps. These tests ensure that the app functions correctly for millions of users, regardless of their device or network conditions.
- Video game developers rigorously test game mechanics and character abilities with a wide range of inputs. This prevents bugs like characters getting stuck in walls or abilities not working under specific circumstances, ensuring a smooth player experience.
- Financial institutions employ quality assurance testers to verify that their trading algorithms and banking software handle all possible transaction amounts, including zero and very large numbers, without errors.
Assessment Ideas
Students exchange functions they have written. Each student then writes 3-5 test cases for their partner's function, specifying the input and the expected output for each. Partners then run the tests and discuss any discrepancies.
Provide students with a simple function (e.g., a function to calculate the area of a rectangle) and a list of potential inputs. Ask students to identify which inputs are typical, which are edge cases, and which are invalid, and to explain their reasoning for each.
Give students a function that contains a subtle bug. Ask them to write one test case that will reveal the bug and briefly explain why that specific input causes the function to fail.
Frequently Asked Questions
What is an edge case?
What is the difference between a test case and a debugging session?
How does peer testing improve both the tester's and the author's understanding?
What is a unit test?
More in Programming with Purpose
Data Types and Variables
Students will learn to use different data types and variables to store and manipulate information in a program.
2 methodologies
Conditional Statements (If/Else)
Students will use conditional statements to control the execution flow of a program based on specific criteria.
2 methodologies
Looping Constructs (For/While)
Students will implement loops to repeat blocks of code, improving efficiency and reducing redundancy.
2 methodologies
Introduction to Functions
Students will design reusable code blocks to improve readability and maintainability.
2 methodologies
Function Design and Reusability
Students will focus on designing functions that are truly reusable across different projects.
2 methodologies
Documentation and Code Readability
Students will learn the importance of documentation in improving the usability of a code library.
2 methodologies