e43265fd1e07f0b0540de9c0c1a481574159a545
[metze/samba/wip.git] / lib / testtools / doc / overview.rst
1 ======================================
2 testtools: tasteful testing for Python
3 ======================================
4
5 testtools is a set of extensions to the Python standard library's unit testing
6 framework. These extensions have been derived from many years of experience
7 with unit testing in Python and come from many different sources. testtools
8 also ports recent unittest changes all the way back to Python 2.4.
9
10 What better way to start than with a contrived code snippet?::
11
12   from testtools import TestCase
13   from testtools.content import Content
14   from testtools.content_type import UTF8_TEXT
15   from testtools.matchers import Equals
16
17   from myproject import SillySquareServer
18
19   class TestSillySquareServer(TestCase):
20
21       def setUp(self):
22           super(TestSillySquare, self).setUp()
23           self.server = self.useFixture(SillySquareServer())
24           self.addCleanup(self.attach_log_file)
25
26       def attach_log_file(self):
27           self.addDetail(
28               'log-file',
29               Content(UTF8_TEXT
30                       lambda: open(self.server.logfile, 'r').readlines()))
31
32       def test_server_is_cool(self):
33           self.assertThat(self.server.temperature, Equals("cool"))
34
35       def test_square(self):
36           self.assertThat(self.server.silly_square_of(7), Equals(49))
37
38
39 Why use testtools?
40 ==================
41
42 Better assertion methods
43 ------------------------
44
45 The standard assertion methods that come with unittest aren't as helpful as
46 they could be, and there aren't quite enough of them.  testtools adds
47 ``assertIn``, ``assertIs``, ``assertIsInstance`` and their negatives.
48
49
50 Matchers: better than assertion methods
51 ---------------------------------------
52
53 Of course, in any serious project you want to be able to have assertions that
54 are specific to that project and the particular problem that it is addressing.
55 Rather than forcing you to define your own assertion methods and maintain your
56 own inheritance hierarchy of ``TestCase`` classes, testtools lets you write
57 your own "matchers", custom predicates that can be plugged into a unit test::
58
59   def test_response_has_bold(self):
60      # The response has bold text.
61      response = self.server.getResponse()
62      self.assertThat(response, HTMLContains(Tag('bold', 'b')))
63
64
65 More debugging info, when you need it
66 --------------------------------------
67
68 testtools makes it easy to add arbitrary data to your test result.  If you
69 want to know what's in a log file when a test fails, or what the load was on
70 the computer when a test started, or what files were open, you can add that
71 information with ``TestCase.addDetail``, and it will appear in the test
72 results if that test fails.
73
74
75 Extend unittest, but stay compatible and re-usable
76 --------------------------------------------------
77
78 testtools goes to great lengths to allow serious test authors and test
79 *framework* authors to do whatever they like with their tests and their
80 extensions while staying compatible with the standard library's unittest.
81
82 testtools has completely parametrized how exceptions raised in tests are
83 mapped to ``TestResult`` methods and how tests are actually executed (ever
84 wanted ``tearDown`` to be called regardless of whether ``setUp`` succeeds?)
85
86 It also provides many simple but handy utilities, like the ability to clone a
87 test, a ``MultiTestResult`` object that lets many result objects get the
88 results from one test suite, adapters to bring legacy ``TestResult`` objects
89 into our new golden age.
90
91
92 Cross-Python compatibility
93 --------------------------
94
95 testtools gives you the very latest in unit testing technology in a way that
96 will work with Python 2.4, 2.5, 2.6, 2.7 and 3.1.