@@ -3,6 +3,69 @@ Testing Your Code
33
44Testing your code is very important.
55
6+ Getting used to writting the testing code and the running code in parallel is
7+ now considered a good habit. Used wisely, this method helps you define more
8+ precisely your code's intent and have a more decoupled architecture.
9+
10+ Some general rules of testing:
11+
12+ - A testing unit should focus on one tiny bit of functionality and prove it
13+ correct.
14+
15+ - Each test unit must be fully independent. Each of them must be able to run
16+ alone, and also within the test suite, regardless of the order they are called.
17+ The implication of this rule is that each test must be loaded with a fresh
18+ dataset and may have to do some cleanup afterwards. This is usually
19+ handled by setUp() and tearDown() methods.
20+
21+ - Try hard to make tests that run fast. If one single test needs more than a
22+ few millisecond to run, development will be slowed down or the tests will not
23+ be run as often as desirable. I some cases, test can't be fast because they
24+ need a complex data structure to work on, and this data structure must be loaded
25+ every time the test runs. Keep this heavier tests in a separate test suite
26+ that is run by some scheduled task, and run all other tests as often as needed.
27+
28+ - Learn your tools and learn how to run a single test or a test case. Then,
29+ when developing a function inside a module, run this function's tests very
30+ often, ideally automatically when you save the code.
31+
32+ - Always run the full test suite before a coding session, and run it again
33+ after. This will give you more confidence that you did not break anything in
34+ the rest of the code.
35+
36+ - It is a good idea to implement a hook that runs all test before pushing code
37+ to a shared repository.
38+
39+ - If you are in the middle of a development and have to interrupt your work, it
40+ is a good idea to write a broken unit test about what you want to develop next.
41+ When comming back to work, you will have a pointer to where you were and get
42+ faster on tracks.
43+
44+ - The first step when you are debugging your code is to write a new test pinpointing
45+ the bug. While it is not always possible to do, those bug catching test are among
46+ the most valuable piece of code in your project.
47+
48+ - Use long and descriptive names for testing functions. The style guide here is
49+ slighlty different than that of running code, where short names are often
50+ preferred. The reason is testing functions are never called explicitely.
51+ ``square()`` or even ``sqr()`` is ok in running code, but in testing code you
52+ would has names such as ``test_square_of_number_2()``,
53+ ``test_square_negative_number()``. These function names are displayed when a
54+ test fail, and should be as descriptive as possible.
55+
56+ - When something goes wrong or has to be changed, and if your code has a good
57+ set of tests, you or other maintainers will rely largely on the testing suite
58+ to fix the problem or modify a given behavior. Therefore the testing code will
59+ be read as much as or even more than the running code. A unit test whose
60+ purpose is unclear is not very helpful is this case.
61+
62+ - Another use of the testing code is as an introduction to new developpers. When
63+ someone will have to work on the code base, runnning and reading the related
64+ testing code is often the best they can do. They will or should discover the
65+ hot spots, where most difficulties arise, and the corner cases. If they have to
66+ add some functionality, the first step should be to add a test and, by this mean,
67+ ensure the new functionality is not already a working path that has not been
68+ plugged in the interface.
669
770The Basics
871::::::::::
@@ -37,9 +100,36 @@ Doctest
37100-------
38101
39102The doctest module searches for pieces of text that look like interactive Python
40- sessions, and then executes those sessions to verify that they work exactly as
103+ sessions in docstrings , and then executes those sessions to verify that they work exactly as
41104shown.
42105
106+ Doctests have a different use case than proper unit tests: they are usually less
107+ detailed and don't catch special cases or obscure regression bugs. They are
108+ useful as an expressive documentation of the main use cases of a module and
109+ its components. However, doctests should run automatically each time
110+ the full test suite runs.
111+
112+ A simple doctest in a function:
113+
114+ ::
115+
116+ def square(x):
117+ """Squares x.
118+
119+ >>> square(2)
120+ 4
121+ >>> square(-2)
122+ 4
123+ """
124+
125+ return x * x
126+
127+ if __name__ == '__main__':
128+ import doctest
129+ doctest.testmod()
130+
131+ When running this module from the command line as in ``python module.py``, the doctests
132+ will run and complain if anything is not behaving as described in the docstrings.
43133
44134Tools
45135:::::
0 commit comments