Skip to content

Commit 9e0eb0b

Browse files
committed
Fleshed out the tests documentation
The doctest bit still needs some work, I've never really used it myself so not sure what the benefits are :)
1 parent ba66c1e commit 9e0eb0b

File tree

1 file changed

+103
-10
lines changed

1 file changed

+103
-10
lines changed

docs/writing/tests.rst

Lines changed: 103 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,148 @@ The Basics
1111
Unittest
1212
--------
1313

14+
Unittest is the batteries-included test module in the Python standard library.
15+
Its API will be familiar to anyone who has used any of the JUnit/nUnit/CppUnit
16+
series of tools.
1417

18+
Creating testcases is accomplished by subclassing a TestCase base class
1519

16-
Doc Strings
17-
-----------
20+
::
1821

22+
import unittest
1923

24+
def fun(x):
25+
return x + 1
2026

27+
class MyTest(unittest.TestCase):
28+
def test(self):
29+
self.assertEqual(fun(3), 4)
2130

22-
Tools
23-
:::::
31+
As of Python 2.7 unittest also includes its own test discovery mechanisms.
32+
33+
`unittest in the standard library documentation <http://docs.python.org/library/unittest.html>`_
2434

2535

2636
Doctest
2737
-------
2838

39+
The 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
41+
shown.
42+
43+
44+
Tools
45+
:::::
46+
2947

3048
py.test
3149
-------
3250

51+
py.test is a no-boilerplate alternative to Python's standard unittest module.
52+
3353
::
3454

3555
$ pip install pytest
3656

57+
Despite being a fully-featured and extensible test tool it boasts a simple
58+
syntax. Creating a test suite is as easy as writing a module with a couple of
59+
functions
60+
61+
::
62+
63+
# content of test_sample.py
64+
def func(x):
65+
return x + 1
66+
67+
def test_answer():
68+
assert func(3) == 5
69+
70+
and then running the `py.test` command
71+
72+
::
73+
74+
$ py.test
75+
=========================== test session starts ============================
76+
platform darwin -- Python 2.7.1 -- pytest-2.2.1
77+
collecting ... collected 1 items
78+
79+
test_sample.py F
80+
81+
================================= FAILURES =================================
82+
_______________________________ test_answer ________________________________
83+
84+
def test_answer():
85+
> assert func(3) == 5
86+
E assert 4 == 5
87+
E + where 4 = func(3)
88+
89+
test_sample.py:5: AssertionError
90+
========================= 1 failed in 0.02 seconds =========================
91+
92+
far less work than would be required for the equivalent functionality with the
93+
unittest module!
94+
95+
`py.test <http://pytest.org/latest/>`_
96+
3797

3898
Nose
3999
----
40100

101+
nose extends unittest to make testing easier.
102+
103+
104+
::
105+
106+
$ pip install nose
107+
108+
nose provides automatic test discovery to save you the hassle of manually
109+
creating test suites. It also provides numerous plugins for features such as
110+
xUnit-compatible test output, coverage reporting, and test selection.
111+
112+
`nose <http://readthedocs.org/docs/nose/en/latest/>`_
113+
114+
115+
tox
116+
---
117+
118+
tox is a tool for automating test environment management and testing against multiple
119+
interpreter configurations
41120

42121
::
43122

44123
$ pip install tox
45124

125+
tox allows you to configure complicatated multi-parameter test matrices via a
126+
simple ini-style configuration file.
46127

128+
`tox <http://tox.testrun.org/latest/>`_
47129

48130
Unittest2
49131
---------
50132

51-
A backport of Python 2.7's
133+
unittest2 is a a backport of Python 2.7's unittest module which has an improved
134+
API and better assertions over the one available in previous versions of Python.
52135

136+
If you're using Python 2.6 or below, you can install it with pip
53137

54138
::
55139

56-
$ pip install tox
140+
$ pip install unittest2
57141

142+
You may want to import the module under the name unittest to make porting code
143+
to newer versions of the module easier in the future
58144

59-
tox
60-
---
145+
::
61146

147+
import unittest2 as unittest
148+
149+
class MyTest(unittest.TestCase):
150+
...
151+
152+
This way if you ever switch to a newer python version and no longer need the
153+
unittest2 module, you can simply change the import in your test module without
154+
the need to change any other code.
155+
156+
`unittest2 <http://pypi.python.org/pypi/unittest2>`_
62157

63-
::
64158

65-
$ pip install tox

0 commit comments

Comments
 (0)