Compare Output with Expected Value
In this step, you will learn how to compare the output of a function with an expected value. This is a crucial part of testing and ensuring that your functions are working correctly. By comparing the actual output with the expected output, you can identify and fix any errors in your code.
We'll continue using the calculate_sum.py
file from the previous steps. If you haven't completed the previous steps, please create the file with the following content:
def calculate_sum(x, y):
"""
This function calculates the sum of two numbers.
If the sum is greater than 10, it returns double the sum.
"""
sum_result = x + y
if sum_result > 10:
return sum_result * 2
else:
return sum_result
## Example usage:
num1 = 10
num2 = 5
result = calculate_sum(num1, num2)
print("The sum of", num1, "and", num2, "is", result)
## Capture the output and perform an additional operation
final_result = result + 20
print("The final result after adding 20 is:", final_result)
num3 = 2
num4 = 3
result2 = calculate_sum(num3, num4)
print("The sum of", num3, "and", num4, "is", result2)
## Capture the output and perform an additional operation
final_result2 = result2 * 2
print("The final result after multiplying by 2 is:", final_result2)
Now, let's add a function to compare the output with an expected value and print a message indicating whether the test passed or failed.
-
Open the calculate_sum.py
file in the VS Code editor.
-
Modify the script as follows:
def calculate_sum(x, y):
"""
This function calculates the sum of two numbers.
If the sum is greater than 10, it returns double the sum.
"""
sum_result = x + y
if sum_result > 10:
return sum_result * 2
else:
return sum_result
def compare_output(actual_output, expected_output):
"""
This function compares the actual output with the expected output.
"""
if actual_output == expected_output:
print("Test passed!")
else:
print("Test failed. Expected:", expected_output, "Actual:", actual_output)
## Example usage:
num1 = 10
num2 = 5
result = calculate_sum(num1, num2)
print("The sum of", num1, "and", num2, "is", result)
## Capture the output and perform an additional operation
final_result = result + 20
print("The final result after adding 20 is:", final_result)
## Compare the output with the expected value
expected_final_result = 50
compare_output(final_result, expected_final_result)
num3 = 2
num4 = 3
result2 = calculate_sum(num3, num4)
print("The sum of", num3, "and", num4, "is", result2)
## Capture the output and perform an additional operation
final_result2 = result2 * 2
print("The final result after multiplying by 2 is:", final_result2)
## Compare the output with the expected value
expected_final_result2 = 10
compare_output(final_result2, expected_final_result2)
In this modified script:
- We define a function called
compare_output
that takes two arguments: actual_output
and expected_output
.
- Inside the function, we compare the
actual_output
with the expected_output
.
- If they are equal, we print "Test passed!".
- If they are not equal, we print "Test failed." along with the expected and actual values.
- We then call the
compare_output
function with final_result
and expected_final_result
to compare the output of the first calculation with the expected value of 50.
- We also call the
compare_output
function with final_result2
and expected_final_result2
to compare the output of the second calculation with the expected value of 10.
-
Run the script using the following command:
python ~/project/calculate_sum.py
You should see the following output:
The sum of 10 and 5 is 30
The final result after adding 20 is: 50
Test passed!
The sum of 2 and 3 is 5
The final result after multiplying by 2 is: 10
Test passed!
This output demonstrates that you can successfully compare the output of a function with an expected value and determine whether the test passed or failed.