×

python · level 3 · unit 6 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Good tests check one behaviour, including the edge cases.

A good test checks one behaviour and has a name that says which. Test the normal case, then hunt for an or two: empty lists, zero, negative numbers, missing keys. Bugs love the edges.

runs before every test, so each one starts fresh. passes only if the code inside raises that error. checks many inputs in one test and reports each failure separately.

import unittest
def average(scores):
    if len(scores) == 0:
        raise ValueError("no scores")
    return sum(scores) / len(scores)

class AverageTests(unittest.TestCase):
    def test_typical_scores(self):
        self.assertEqual(average([2, 4, 6]), 4)
    def test_empty_list_is_an_error(self):
        with self.assertRaises(ValueError):
            average([])

unittest.main(argv=["x"], exit=False)

The time on the Ran 2 tests line changes from run to run, so never test your tests by it.