Every topic starts at New. Sign in and your practice counts.
As lines of code increase, manual testing becomes complicated. Small test functions check specific parts of a program for chosen values.
This topic teaches how small test functions use assert to compare actual and expected results, using an add function with three cases and a multiply variant to trace a failing expectation.
For a written function that performs some operation, you write another function to check if it gives the correct result. This check compares the actual output with the expected output for chosen inputs.
Python's built-in unittest framework is a tool that helps you check if your code works in the desired way. Rather than manually testing your code by running it, you write small tests that check specific parts of your program. As the lines of code increase, the code may become complicated to test manually, so you write a set of tests to ensure the function is executing correctly for various inputs.
For a written function that performs some operation, you write another function, a test function, to check if the first function gives the correct result. The comparison uses assertions to compare the actual output with the expected output. If the output does not match the expected value, the assert raises an error with a custom message. If all assertions pass, it prints "All tests passed!"
def add(a, b): # Function to be tested
return a + b
def test_add(): # Test function
result = add(2, 3) # Test case 1: Check if 2 + 3 equals 5
assert result == 5, f"Test failed: Expected 5 but got {result}"
result = add(0, 0) # Test case 2: Check if 0 + 0 equals 0
assert result == 0, f"Test failed: Expected 0 but got {result}"
result = add(-1, 1) # Test case 3: Check if -1 + 1 equals 0
assert result == 0, f"Test failed: Expected 0 but got {result}"
print("All tests passed!")
test_add() # Run the test function
The example tests add(a, b) with three different pairs:
add(2, 3), expected to be 5.add(0, 0), expected to be 0.add(-1, 1), expected to be 0.Each case stores the result and checks if it matches the expected value, showing a custom failure message otherwise. When all three assertions hold, the test prints All tests passed!.
The expected result in the test can be written incorrectly, even when the function itself is correct. For example, overlooking a negative input can produce an expected result of 2 where the correct result is 0. In that case you get the Test failed: ... message. To investigate it you can debug the code by applying breakpoints in the code.
The same code is changed for multiplication and tests are adjusted accordingly. The question is whether all tests will pass and, if not, which one will fail and why.
def multiply(a, b):
return a * b
def test_multiply():
result = multiply(2, 3)
assert result == 6, f"Test failed: Expected 6 but got {result}"
result = multiply(4, 0)
assert result == 1, f"Test failed: Expected 1 but got {result}"
result = multiply(-1, -1)
assert result == 1, f"Test failed: Expected 1 but got {result}"
print("All tests passed!")
test_multiply()
The first check passes because multiply(2, 3) returns 6 as expected. The second check fails because multiply(4, 0) returns 0 while the test expects 1. The third check passes because multiply(-1, -1) returns 1 as expected. Try debugging the code.