Agile documentation in Python projects
| Author: | Tarek Ziadé <tarek@ziade.org> |
|---|---|
| Date: | $Date: 2007-02-21$ |
| License: | CC-By-SA 2 license |
The main goal is to understand how Agile documentation works in Python, through:
My secret goal is to come to RuPy to see how Ruby works...
What kind of documentation are we talking about ?
Technical documentation that helps to understand how a codebase works and how it can be used. This can be:
| [1] | (1, 2) Python folders and files |
What Agile means ?
A lightweight process to stay reactive to frequent changes
| [2] | The website looks a bit like some sect site but it's a good sect |
Agile principles can be applied to any repeating process:
Doing TDD in Python
This part is composed of:
Example:
>>> def division(a, b): ... return a / b
Let's test it !
>>> def test_division(): ... if division(4, 2) == 2: ... return 'OK' ... else: ... return 'SHUT DOWN THE COMPUTER NOW !' >>> test_division() 'OK'
Each use case tests one aspect of the code
Let's try this one:
>>> def test_division2(): ... if division(4, 0) == 0: ... return 'OK' ... else: ... return 'I WANT ZERO' >>> test_division2() Traceback (most recent call last): ... ZeroDivisionError: integer division or modulo by zero
The function fails, let's change it:
>>> def division(a, b): ... if b == 0: ... return 0 ... return a / b
and rerun the test:
>>> test_division2() 'OK'
Let's write the tests before the code:
>>> def test_average(): ... if average(1, 2, 3) == 2: ... return 'OK' ... else: ... return 'Houston we have a problem' >>> test_average() Traceback (most recent call last): ... NameError: global name 'average' is not defined
Now let's code average:
>>> def average(*args): ... return sum(args) / len(args)
And run the test again:
>>> test_average() 'OK'

TDD brings:
Jean-Charles says:
"Tests are a waste of time, just a toy for interpreted languages"
Bruce Eckel says:
"If it's not tested, it's broken"
Bruce's the one who's right:
Doing TDD in Python
Python is battery included. The standard library provides:
There are many other test framework projects out there
unittest provides helpers to write tests:
TestCase provides two things:
Writing a test case == deriving from TestCase:
>>> import unittest >>> class DivisionTestCase(unittest.TestCase): ... ... def test_one(self): ... self.assertEquals(division(4, 2) , 2) ... ... def test_two(self): ... self.assert_(division(4, 0) == 0) ...
Each test is a method with a test prefix.
The test can be:
Example of test module:
import unittest
from division import division
class DivisionTestCase(unittest.TestCase):
def test_one(self):
self.assertEquals(division(4, 2) , 2)
def test_two(self):
self.assert_(division(4, 0) == 0)
def test_suite():
suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(DivisionTestCase))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
Running the tests with the Python interpreter:
dabox:~ tarek$ python test_division.py .. ------------------------------------------------------------------ Ran 2 tests in 0.000s OK
Running the tests with the Python interpreter, with a failure:
dabox:~ tarek$ python test_division.py F. ================================================================== FAIL: test_one (__main__.DivisionTestCase) ------------------------------------------------------------------ Traceback (most recent call last): File "test_division.py", line 7, in test_one self.assertEquals(division(4, 2) , 3) AssertionError: 2 != 3 ------------------------------------------------------------------ Ran 2 tests in 0.000s FAILED (failures=1)
"Doing TDD with Python is so cool !" -- Magnum
Doing TDD in Python
By the way, how Python code is organized ?
Each package comes with

Following the TDD principles:
Our division package will look like this:
division
|
|-- division.py
|
|-- tests
| |
| |-- test_division.py
|
|-- doc
|
|-- division.txt <-- some cool doc
Doing TDD in Python
Mettre une photo de matrix
"I now TDD in Python now" -- Neo, The Matrix
Writing documents in reST
reSTructuredText (say reSt to be hype) is:
Example:
================== I am the reST file ================== I am the first section ====================== I am the content of the first section. I have things to say. I am the second section ======================= I am, what I am, what I aammmm...
Several remarks:
Writing documents in reST
XXX
Writing documents in reST
XXX
Writing documents in reST
reST can be:
Python developers
reST