The Test Framework

Throughout the class we’ll be using a test framework to check your code. Here’s what you need to know to make it all work.

If you haven’t done so already, create a directory to use for this class in your home directory or desktop (or wherever you want to save your work). We will refer to this folder as python_class. You can name it something else, just don’t forget where you put it as you will need it! Download the Exercise test files and put it in your python_class directory.

This file you downloaded (exercises.zip), when unzipped, should create a new folder called exercises. It contains the test framework we will be using, so you will be working from this folder. It also contains the files you will need to modify for most of the exercises in the class.

Confirm the Framework is Working

Note

You may need to write python3 or py instead of python to launch Python.

If that’s the case, replace python in all commands on this page with the correct command for your machine.

Note

If you use uv, you can write uv run python instead of python in all commands on this page (uv run python test.py for example). The exercises folder includes a pyproject.toml file, so uv will use Python 3.12 or newer.

If your laptop doesn’t allow running code from your home directory, unzip the exercises somewhere else (C:\programming for example).

Change directory to your exercises directory, and run the following:

$ python test.py

You should see output like this:

Welcome to the Test Framework! ✨

To show all testable exercises run:
python test.py --list

To test an exercise run:
python test.py <EXERCISE_NAME>

The message confirms the Test Framework is working! Yay! 🎉

How to Solve the Exercises

You will implement a specific function in an existing .py file in the exercises folder. These .py files already contain skeleton functions that you will implement when you get to the corresponding exercise. Sometimes the exercise will have you change a function you previously implemented.

It’s important that you write your code in the right place, or the tests won’t work!

How to Run the Tests

Each exercise will tell you the way to test it. You will always start by typing in the command window python test.py followed by the name given by the exercise. Don’t add arguments, only give the name of the test as shown in the exercise.

Always run the test program from your exercises folder.

As an example, we could run the tests for the longest_first exercise:

$ python test.py longest_first

Testing longest_first

Running LongestFirstTests test class in functions_test.py

test_empty_string (functions_test.LongestFirstTests.test_empty_string) ... ok
test_many_strings (functions_test.LongestFirstTests.test_many_strings) ... ok
test_single_string (functions_test.LongestFirstTests.test_single_string) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

The last line of output (OK) means the tests for this exercise are already passing.

Additional Testing Techniques

The test framework is meant to help you quickly test many different inputs against your code and quickly assert that your code performs the correct actions.

You may find using the test framework challenging to use at times. When that’s the case, you may want to try one of these approaches:

  1. Continue using the test framework, but add a breakpoint call to your code to drop into a Python debugger (PDB) where you can type debugging commands to inspect the state of your code. The 3 most useful PDB commands are:

    1. l to list the code line you’re currently on

    2. n to go to the next line in your code

    3. interact to enter an interactive Python prompt (a.k.a. the Python REPL)

    4. dir() (not a PDB command, but a built-in function) to list your current local variables

  2. Copy-paste your code from your text editor into a Python REPL. You’ll need to make sure you don’t have any blank lines in your blocks of code for this to work.

  3. Copy-paste this function into your Python REPL and then call paste_code to paste a block of code and immediately run it.

  4. Create your own file (called testme.py for example) which interacts with your function or class in specific ways and manually check the output.