You’ll probably notice a few things here:
Tests are organised into classes, which inherit from unittest.TestCase.
The main body of the test isin a method called test_can_start_a_list_and_re
trieve_it_later. Any method whose name starts with test_ is a test method,
and will be run by the test runner. You can have more than one test_ method
per class. Nice descriptive names for our test methods are a good idea too.
setUp and tearDown are special methods which get run before and after each
test. I’m using them to start and stop our browser—note that they’re a bit like a
try/except, in that tearDown will run even if there’s an error during the test
itself.1
No more Firefox windows left lying around!
We use self.assertIn instead of just assert to make our test assertions.
unittest provides lots of helper functions like this to make test assertions, like
assertEqual, assertTrue, assertFalse, and so on. You can find more in the
unittest documentation.
self.fail just fails no matter what, producing the error message given. I’m
using it as a reminder to finish the test.
Finally, we have the if __name__ == '__main__' clause (if you’ve not seen it
before, that’s how a Python script checks if it’s been executed from the command
line, rather than just imported by another script). We call unittest.main(),
which launches the unittest test runner, which will automatically find test
classes and methods in the file and run them.
warnings='ignore' suppresses a superfluous ResourceWarning which was
being emitted at the time of writing. It may have disappeared by the time you
read this; feel free to try removing it!