This guide explains simple function tests without a testing framework through a focused Python example. You will learn the behavior to expect, why it matters in a real program, and how to recognize the common mistakes before applying it in your own project.
What You Will Learn
Even without unittest or pytest, a small function can be checked with a few carefully chosen assertions. Test a normal value, a boundary and a second representative input.
When and Why to Use It
The following example focuses on a practical use of Simple Function Tests Without a Testing Framework.
Step-by-Step Example
def is_even(number):
return number % 2 == 0
assert is_even(4) is True
assert is_even(0) is True
assert is_even(7) is False
print("All checks passed")
Understanding the Example
Each assert checks that the function returns the expected boolean. The final message appears only if no assertion fails in a regular interpreter run.
Expected Output
All checks passed
Important Behavior and Edge Cases
Assertions can be disabled with the -O option and are not a substitute for validating untrusted user input.
Try It Yourself
Test a temperature conversion function using zero, a positive value and a negative value.
Common Beginner Mistakes
- Assertions can be disabled with the -O option and are not a substitute for validating untrusted user input.
- Confusing a returned value with printed output, or failing to verify a function’s behavior with different input values.
- Skipping the practice step and assuming the example covers every input. Test at least one normal case and one edge case relevant to this topic.
Key Takeaways
- Even without unittest or pytest, a small function can be checked with a few carefully chosen assertions.
- Each assert checks that the function returns the expected boolean.
- Apply the technique deliberately, checking its documented input requirements and failure behavior before integrating it into a larger project.