18.1. Introduction: Test Cases

A test case expresses requirements for a program, in a way that can be checked automatically. Specifically, a test asserts something about the state of the program at a particular point in its execution.

We have previously suggested that it’s a good idea to first write down comments about what your code is supposed to do, before actually writing the code. It is an even better idea to write down some test cases before writing a program.

There are several reasons why it’s a good habit to write test cases.

Now it’s time to learn how to write code for test cases.

Python provides a statement called assert.

Why would you ever want to write a line of code that can never compute anything useful for you, but sometimes causes a runtime error? For all the reasons we described above about the value of automated tests. You want a test that will alert that you that some condition you assumed was true is not in fact true. It’s much better to be alerted to that fact right away than to have some unexpected result much later in your program execution, which you will have trouble tracing to the place where you had an error in your code.

Why doesn’t assert print out something saying that the test passed? The reason is that you don’t want to clutter up your output window with the results of automated tests that pass. You just want to know when one of your tests fails. In larger projects, other testing harnesses are used instead of assert, such as the python unittest module. Those provide some output summarizing tests that have passed as well as those that failed. In this textbook, we will just use simple assert statements for automated tests.

To write a test, we must know what we expect some value to be at a particular point in the program’s execution. In the rest of the chapter, we’ll see some examples of assert statements and ideas for what kinds of assertions one might want to add in one’s programs.

Note

A note to instructors: this chapter is deliberately structured so that you can introduce testing early in the course if you want to. You will need to cover chapter 8, on Conditionals, before starting this chapter, because that chapter covers Booleans. The subchapters on testing types and testing conditionals can be covered right after that. The subchapter on testing functions can be delayed until after you have covered function definition.

Check your understanding

    When assert x==y is executed and x and y have different values, what will happen?

  • A runtime error will occur
  • The expression ``x==y`` evaluates to ``False``, so a runtime error will occur
  • A message is printed out saying that the test failed.
  • If the assertion fails, a runtime error will occur
  • x will get the value that y currently has
  • ``x==y`` is a Boolean expression, not an assignment statement
  • Nothing will happen
  • The expression ``x==y`` evaluates to ``False``
  • A message is printed out saying that the test passed.
  • When an assertion test passes, no message is printed. In this case, the assertion test fails.

    When assert x==y is executed and x and y have the same values, what will happen?

  • A runtime error will occur
  • The expression ``x==y`` evaluates to ``True``
  • A message is printed out saying that the test failed.
  • The expression ``x==y`` evaluates to ``True``
  • x will get the value that y currently has
  • ``x==y`` is a Boolean expression, not an assignment statement
  • Nothing will happen
  • The expression ``x==y`` evaluates to ``True``
  • A message is printed out saying that the test passed.
  • When an assertion test passes, no message is printed.

    Test cases are a waste of time, because the python interpreter will give an error message when the program runs incorrectly, and that’s all you need for debugging.

  • True
  • You might not notice the error, if the code just produces a wrong output rather generating an error. And it may be difficult to figure out the original cause of an error when you do get one.
  • False
  • Test cases let you test some pieces of code as you write them, rather than waiting for problems to show themselves later.
You have attempted of activities on this page
Next Section - 18.2. Checking Assumptions About Data Types