Assertions
Class video from Spring 25
Ignore the slide about exams.
Software testing
Software testing is both a manual and an automated effort.
Manual testing is when a tester (or user) enters values into the user interface and checks the behavior of the system.
Automated testing is where test code is used to check the results of the main product code. Automated testing is an essential part of program verification, which is an evaluation that software is behaving as specified and is free from errors.
Automated testing is a necessity in real systems with thousands of lines of code and many complex features. Manual testing is simply infeasible to do thoroughly.
Code that verifies code?
Automated testing in this case means writing code. Developers and testers write code and scripts that executes and tests some other code.
Exercise
- Create a directory named
testing-labin yourseng-201/directory. - Download
sample.pyand put it in thetesting-lab/directory. - Open the folder in PyCharm and run
sample.py.
The function calls in the __main__ section of code are a semi-automated test. The calls are automated, but the verification is still manual – you, the developer, have to verify that the output is indeed correct.
To have automated testing, we need a programmatic indicator of correctness. Enter the assert statement.
The assert statement
Nearly all programming languages have an assert keyword. An assertion checks if a value is True or False. If True, it does nothing. If False, the assert throws a special type of exception. Assertions are commonly used in languages like C and Ada to verify that something is True before continuing execution.
In most modern languages, including Python, the assert is the basis of automated testing.
Exercise
Let’s explore the assert in Python.
- Create a new file named
test_sample.pyin thetesting-lab/directory. Edit the file in PyCharm. - Add the following code:
test_sample.pyassert True assert False print("Made it to the bottom.") - Run
test_sample.py. Notice the following.assert Truedoes not produce any output. The program simply continues.assert Falsegenerates an exception. This is expected.- The
print(...)statement did not execute because the exception generated byassert Falsecrashed the program.
- Comment out the
assert Falseline and run it again. Theprint(...)statement will execute.
This demonstrates the behavior of assert. Let’s add some more interesting assertions. Add the following lines to the bottom of test_sample.py:
test_sample.py
x = 2**5
assert x == 32
assert type("Bob") == str
y = 16
assert x-y==16 and type("Bob") == str and int("25") == 25
print("Made it to the bottom.")
The right-hand side of the assert statements now use comparisons and boolean operators. This looks a bit more realistic. The assert can have any simple or complex Boolean expression so long as it evaluates to True or False.
Quick Exercise: Change the operators or values in the expressions so they evaluate to False. Notice how the last assert can fail if any of the comparisons are false.
We’ll put our assertions to work testing program code in the next lab.
Knowledge check
- Question: What two things are you trying to verify with program verification?
- Question: Why do we need automated testing?
- Question: What happens next if a Python program encounters the statement
assert True? - Question: What happens next if a Python program encounters the statement
assert False? - Question: What happens when the following executes:
assert 16 == 2**4? - Question: What happens when the following executes?
assert len('Bob') > 0 and 'Bob' == 'Alice'