Skip to content
Computer Science · 9th Grade · Programming with Purpose · Weeks 19-27

Testing Functions with Inputs

Students will learn to test functions with various inputs to ensure they produce expected outputs.

Common Core State StandardsCSTA: 3A-AP-17

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

  1. Explain the importance of testing functions with different inputs.
  2. Design a set of test cases for a given function.
  3. 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

Defining and Calling Functions

Why: Students must understand how to create and use functions before they can test them.

Basic Conditional Statements (If/Else)

Why: Testing often involves checking different conditions, which relies on understanding how if/else statements work.

Variables and Data Types

Why: Functions operate on data, so students need a foundational understanding of variables and common data types like integers and strings.

Key Vocabulary

Test CaseA specific set of inputs and expected outputs designed to verify a particular aspect of a function's behavior.
Typical InputA value that is commonly expected and within the normal operating range for a function.
Edge CaseAn input value that lies at the boundary of valid inputs, often representing extreme or unusual conditions.
Invalid InputA value that is outside the acceptable range or type for a function, which the function should ideally handle gracefully.
AssertionA 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 activities

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

Peer Assessment

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.

Quick Check

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.

Exit Ticket

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?
An edge case is an input at the boundary of what a function is designed to handle. For a function that works with ages, zero and 150 are edge cases. For a function that processes a list, an empty list is an edge case. Edge cases are disproportionately likely to cause bugs because they are the inputs the author was least focused on when writing the code.
What is the difference between a test case and a debugging session?
A test case is a planned, reproducible check of whether a function produces the expected output for a specific input. Debugging is the process of finding and fixing a bug, which may have been discovered by a failing test case or by an unexpected crash. Testing is systematic and proactive; debugging is reactive and investigative.
How does peer testing improve both the tester's and the author's understanding?
The tester, approaching the function without prior knowledge, must infer what it should do and design inputs that probe its behavior. This builds the critical thinking that effective testing requires. The author sees which tests their function fails and gains a clearer picture of the gap between intended and actual behavior. Both roles strengthen understanding in different ways.
What is a unit test?
A unit test is an automated test that checks the behavior of a single function or small unit of code in isolation. Unit tests are run automatically, often before every code change, to catch regressions. The test cases students design manually in this topic are the conceptual foundation for understanding automated unit testing frameworks used in professional development.