Welcome to Micro-Quest PY-1.1-BP6-MQ10!
Our objective is to create a Python script named calculator.py
containing a function add(a, b)
that returns the sum of two numbers. This function will serve as a basic unit for understanding testing.
Welcome to Micro-Quest PY-1.1-BP6-MQ10!
Our objective is to create a Python script named calculator.py
containing a function add(a, b)
that returns the sum of two numbers. This function will serve as a basic unit for understanding testing.
Let's start by running the tests to see the current state and confirm the engineering specification.
Run pytest
in your terminal.
Test Results:
test_add_positive_integers
test_add_negative_integers
test_add_positive_and_negative_integers
test_add_with_zero
test_add_floating_point_numbers
As expected, the tests are failing because the add
function is not yet implemented.
Now, let's implement the add
function in calculator.py
by following the TODO
comments.
With the add
function implemented, let's run pytest
again to validate our work.
Run pytest
in your terminal.
Test Results:
test_add_positive_integers
test_add_negative_integers
test_add_positive_and_negative_integers
test_add_with_zero
test_add_floating_point_numbers
All tests passed!Great job! You've successfully implemented the add
function and confirmed it works correctly using tests.
This document provides a quick reference for defining Python functions and writing basic tests using pytest
.
A function is a block of code that performs a specific task. You define a function using the def
keyword.
def function_name(parameter1, parameter2):
"""
This is a docstring. It explains what the function does.
Args:
parameter1: Description of the first parameter.
parameter2: Description of the second parameter.
Returns:
Description of the value the function returns.
"""
# Function body starts here, indented
# Code to perform the task
result = parameter1 + parameter2
return result # Use return to send a value back
def
: Keyword to start a function definition.function_name
: Choose a descriptive name.(parameter1, parameter2)
: Input values the function accepts. Can be zero or more.:
: Marks the end of the function header."""..."""
): Explains the function's purpose, arguments (Args
), and return value (Returns
). Good practice for documentation.return
: Sends a value back from the function. If return
is not used, the function returns None
by default.Testing helps verify that your code works as expected. pytest
is a framework for writing and running tests in Python.
test_
or ending with _test.py
.test_
.A test function calls the code you want to test and uses assert
statements to check if the results are correct.
# Example test function structure
def test_something():
# Call the function you are testing
actual_result = your_function(input_value)
# Use assert to check if the actual result matches the expected result
assert actual_result == expected_result
def test_...
: Defines a test function. pytest
finds and runs functions named this way.assert condition
: Checks if condition
is True
. If it's False
, the test fails.You can include multiple assert
statements within a single test function to check different inputs or cases for the same function.
# Example test function with multiple assertions
def test_example_function():
# Test case 1: Positive input
assert example_function(5) == 25
# Test case 2: Negative input
assert example_function(-3) == 9
# Test case 3: Zero input
assert example_function(0) == 0
This approach helps ensure your function handles various situations correctly. If any assert
fails, the test function stops and is marked as failed.
Navigate to your project directory in the terminal and run the command:
pytest
pytest
will discover and run your test files and functions, reporting the results (PASSED, FAILED, etc.).