Ignore the slide about exams.
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.
Automated testing in this case means writing code. Developers and testers write code and scripts that executes and tests some other code.
testing-lab
in your seng-201/
directory.sample.py
and put it in the testing-lab/
directory.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.
assert
statementNearly 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.
Let’s explore the assert
in Python.
test_sample.py
in the testing-lab/
directory. Edit the file in PyCharm.test_sample.py
assert True
assert False
print("Made it to the bottom.")
test_sample.py
. Notice the following.assert True
does not produce any output. The program simply continues.assert False
generates an exception. This is expected.print(...)
statement did not execute because the exception generated by assert False
crashed the program.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.
assert True
?assert False
?assert 16 == 2**4
?assert len('Bob') > 0 and 'Bob' == 'Alice'