Day 3

Olin Monday

Friday is an “Olin Monday”, so we will have class. If you are cross registered and you can’t make it, let Ben know.

Course Communication

We will be using Canvas as a tool for discussion forums, announcements, and assignment feedback. You should have received an invitation to Canvas, let Ben know if that is not the case. Please continue to contact instructors directly for private matters.

Reading Journal Debrief

With two people sitting around you, go over your reading journal. Identify any questions / difficulties and try to work through them. We will do a quick report out for lingering confusions along with observations you’d like to share with the rest of the class.

Intro to Unit Testing

Testing our functions interactively in the interpreter as we write them is great for quick tests.

As we move to more advanced functions and programs, we’d like to add several more features. We might want to save the tests, to run them again as our implementation changes. We might even want to write the tests ahead of time, an approach known as test-driven design.

Today, we’ll write our first unit tests. These short tests let us verify each piece of a program independently, which makes it more likely the entire program will be correct once we put everything together. You will be writing more unit tests in mini-project 1, and in all the work you do for this class.

We will be using the doctest Python module, which makes code examples in Python docstrings into unit tests automatically.

Running doctests

Doctests can be run in normal mode, in which only failing tests will be reported, or in verbose mode, which reports results from all tests.

If you’ve written a program called my_prog.py, you can test it from the command line by running

$ python3 -m doctest [-v] my_prog.py

where the [-v] means you can either include the -v verbose flag or not.

You can also run doctests from inside your program, by including:

import doctest
doctest.testmod(verbose=True)

at the bottom.

Exercise: Add unit tests to the functions you wrote for the reading journal and verify their correctness. Before you start typing out your unit tests, take a minute to think through what would constitute a good set of unit tests for each function.

Unit Testing questions

You are beginning to explore unit testing in class activities and in the Gene Finder starter code. Now is a good time to ask questions about their role in how you will design software.

  1. In what ways, if any, have you found unit tests helpful so far?
  2. What role does unit testing play in determining whether or not your program is correct? (This may vary depending on the program.)
  3. Any aspects of unit testing that you find unwieldy?
  4. Do you have any best practices that you’d like to share with the class from using unit tests?

Open studio time

Please use instructors and NINJAs in the studio for questions and approaches as you work in MP1.