Course 3.2Lesson 2 of 5
The unittest module is Python's standard tool for tests. A test class inherits from unittest.TestCase, each method whose name starts with test is a test, and the assert methods such as assertEqual, assertAlmostEqual, assertTrue and assertRaises check the results and give a clear report when they fail. setUp runs before each test, so that every test starts from a fresh state. unittest.main runs all the tests and prints a dot for a pass, an F for a failure and an E for an error, then a summary. A test should check one thing and have a name that says what it checks. Use assertAlmostEqual for decimal numbers, and assertRaises with a with block for an expected error.
Try the code and the simulations beside the lesson.
After this lesson you can
unittest.TestCase,assertEqual, assertAlmostEqual, assertTrue and assertRaises,setUp to give every test a fresh start.A table of tests is simple and good. For bigger programs, programmers use a test framework: a tool that finds the tests, runs them, and reports in a standard way. Python has one in its standard library: unittest.
Predict first. There are seven tests. One of them is wrong on purpose. Which one fails, and how does the report show it? Then run it.
The output looks like this:
.......F (one letter for each test)
======================================================================
FAIL: test_a_wrong_expectation (__main__.TestAverage...)
----------------------------------------------------------------------
AssertionError: 2.0 != 3
Ran 8 tests in 0.001s
FAILED (failures=1)F is a failure: the test ran and the answer was wrong. E is an error: something went wrong in the test itself, for example a crash.2.0 != 3. That is the answer of the function, and the expected answer.OK, or FAILED with the numbers.In our program, average([1, 2, 3]) is 2.0, and the test wrongly expected 3. This is a failure of the test, not of the function: a reminder that the expected answers must be right, too.
Use assertAlmostEqual for every answer that comes from decimal arithmetic. The default, 7 places, suits most programs.
test_edges fails, you know what to look at. A test with twenty asserts does not say which one matters.test_empty_list_raises says everything.setUp is for.assertRaises for each way that the function should refuse.| Table of tests | unittest | |
|---|---|---|
| Short to write | yes | a little longer |
| One report with all failures | with your loop | built in |
| Shows both values | with your print | built in |
| Checks for errors | with try | assertRaises |
| Runs only some tests, or in a big project | by hand | built in |
For a small script, a table is fine. For a project that grows, use unittest (or pytest, a popular tool that you may meet later).
test. It is never run.assertEqual. Use assertAlmostEqual.self. before assertEqual.with in assertRaises.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
BankAccount of the course on classes.unittest.TestCase. Methods that start with test are the tests.assertEqual, assertAlmostEqual, assertTrue and assertRaises compare and report both values.setUp runs before each test.F is a failure, and E is an error.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
F mean in the report?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.