Function Returns Expected Output
Validates that a function works as expected (correct output or correct exception).
Location of the snippet: python/basics/functions/function_returns_expected_output
This snippet is used to validate that a function works as expected (correct output or correct exception).
| Device Type |
|---|
| Jupyter |
Variables:
| Variable Name | Variable Description | Type | Required? | Default |
|---|---|---|---|---|
function_name | Name of the function to be tested. | str | Yes | |
test_cases | List of dictionaries with keys 'args', 'kwargs', 'return_value', 'exception', 'exception_message' for each test case. | list | Yes |
Examples:
1. Basic Function - Sum of Two Numbers
This example tests a simple function that adds two numbers, checking for a specific return value.
Scenario: Students are tasked with writing a function add_numbers that takes two arguments and returns their sum.
Task:
Define a Python function named add_numbers(a, b) that returns the sum of a and b.
Placeholder:
def add_numbers(a, b):
pass
Solution:
def add_numbers(a, b):
return a + b
Snippet for the assertion:
| Variable Name | Value |
|---|---|
function_name | add_numbers |
test_cases | [{"args": (1, 2), "return_value": 3}, {"args": (10, 20), "return_value": 30}] |
2. Function with Keyword Arguments and String Output
This example demonstrates testing a function that uses keyword arguments and returns a string.
Scenario: Students need to write a function greet_user that takes a name (positional) and an optional greeting (keyword) and returns a formatted greeting string.
Task:
Define a Python function named greet_user(name, greeting="Hello") that returns a string like "greeting, name!".
Placeholder:
def greet_user(name, greeting="Hello"):
pass
Solution:
def greet_user(name, greeting="Hello"):
return f"{greeting}, {name}!"
Snippet for the assertion:
| Variable Name | Value |
|---|---|
function_name | greet_user |
test_cases | [{"args": ("Alice", ), "return_value": "Hello, Alice!"}, {"args": ("Bob", ), "kwargs": {"greeting": "Hi"}, "return_value": "Hi, Bob!"}] |
3. Function Expected to Raise an Exception (Test only Exception, not Exception Message)
This example shows how to test if a function correctly raises a specific type of exception.
Scenario: Students are to write a function divide_numbers that performs division but raises a ZeroDivisionError if the divisor is zero.
Task:
Define a Python function divide_numbers(numerator, denominator) that returns numerator / denominator. If denominator is 0, it should raise a ZeroDivisionError.
Placeholder:
def divide_numbers(numerator, denominator):
pass
Solution:
def divide_numbers(numerator, denominator):
if denominator == 0:
raise ZeroDivisionError("Cannot divide by zero.")
return numerator / denominator
Snippet for the assertion:
| Variable Name | Value |
|---|---|
function_name | divide_numbers |
test_cases | [{"args": (10, 2), "return_value": 5.0}, {"args": (10, 0), "exception": ZeroDivisionError}] |
4. Function Expected to Raise an Exception with a Specific Message
This example shows how to test if a function raises a specific exception with a specific message.
Scenario: Students are to write a function greet_user that greets a user by name but raises a ValueError with a specific message if the name is empty.
Task:
Define a Python function greet_user(name) that returns "Hello, " + name. If name is an empty string, it should raise a ValueError with the message "Name cannot be empty.".
Placeholder:
def greet_user(name):
pass
Solution:
def greet_user(name):
if not name:
raise ValueError("Name cannot be empty.")
return "Hello, " + name
Snippet for the assertion:
| Variable Name | Value |
|---|---|
function_name | greet_user |
test_cases | [{"args": ("Alice",), "return_value": "Hello, Alice"}, {"args": ("",), "exception": ValueError, "exception_message": "Name cannot be empty."}] |