Assertions

The building block of testing.

You are getting the first edition of all these pages. Please let me know if you find an error!

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

  1. Create a directory named testing-lab in your seng-201/ directory.
  2. Download sample.py and put it in the testing-lab/ directory.
  3. Open it in Visual Studio Code, and run it.

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.

  1. Create a new file named test_sample.py in the testing-lab/ directory. Edit the file in Visual Studio Code.
  2. Add the following code:
    test_sample.py
    
    assert True
    assert False
    print("Made it to the bottom.")
    
  3. Run test_samply.py. Notice the following.
    • assert True does not produce any output. The program simply continues.
    • assert False generates an exception. This is expected.
    • The print(...) statement did not execute because the exception generated by assert False crashed the program.
  4. Comment out the assert False line and run it again. The print(...) 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'
Last modified September 9, 2024.