Let's start with a very simple Python function for us to test.
def square(x): return x * x
In order to verify the correctness of this code, we pass a value and we will test if the result of the function is what we expect. For example, we would give it an input of five and would expect the result to be 25.
To illustrate the concept, we can manually test this function in the command line using the assert
statement. The assert
statement in Python simply says that if the conditional statement after the assert
keyword returns False
, throw an exception as follows:
$ python >>> def square(x): ... return x * x >>> assert square(5) == 25 >>> assert square(7) == 49 >>> assert square(10) == 100 >>> assert square(10) == 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError
Using these assert
statements, we verified that the square function was working as intended.