Debian Codegraph @

docs/dev/testing.rst

raw
1 .. include:: ../header.rst
2
3 ===================
4 Docutils_ Testing
5 ===================
6
7 :Authors: Lea Wiemann <[email protected]>;
8 David Goodger <[email protected]>;
9 Docutils developers <[email protected]>
10 :Revision: $Revision: 9906 $
11 :Date: $Date: 2024-08-15 10:43:38 +0200 (Do, 15. Aug 2024) $
12 :Copyright: This document has been placed in the public domain.
13
14 :Abstract: This document describes how to run the Docutils test suite,
15 how the tests are organized and how to add new tests or modify
16 existing tests.
17
18 .. _Docutils: https://docutils.sourceforge.io/
19
20 .. contents::
21
22 When adding new functionality (or fixing bugs), be sure to add test
23 cases to the test suite. Practise test-first programming; it's fun,
24 it's addictive, and it works!
25
26
27 Running the Test Suite
28 ======================
29
30 Before checking in any changes, run the entire Docutils test suite to
31 be sure that you haven't broken anything. From a shell do::
32
33 cd docutils/test
34 python -u alltests.py
35
36 Before `checking in`__ changes to the Docutils core, run the tests on
37 all `supported Python versions`_ (see below for details).
38 In a pinch, the edge cases should cover most of it.
39
40 .. note::
41 The ``alltests.py`` test runner is based on the standard library's unittest_
42 framework.
43 Since Docutils 0.19, running ``python -m unittest`` and the pytest_
44 framework no longer result in spurious failures (cf. `bug #270`_).
45 However, there are differences in the reported number of tests
46 (``alltests.py`` also counts sub-tests).
47 In future, running the test suite may require pytest_.
48
49 __ policies.html#check-ins
50 .. _unittest: https://docs.python.org/3/library/unittest.html
51 .. _pytest: https://pypi.org/project/pytest
52 .. _`bug #270`: https://sourceforge.net/p/docutils/bugs/270/
53
54
55 .. _Python versions:
56
57 Testing across multiple Python versions
58 ---------------------------------------
59
60 A Docutils release has a commitment to support a minimum Python version
61 and beyond (see dependencies__ in README.rst). Before a release is cut,
62 tests must pass in all `supported versions`_. [#]_
63
64 __ ../../README.html#dependencies
65
66 You can use `tox`_ to test with all supported versions in one go.
67 From the shell::
68
69 cd docutils
70 tox
71
72 To test a specific version, use the ``pyNNN`` environment. For example::
73
74 tox -e py312
75
76 `pyenv`_ can be installed and configured (see `installing pyenv`_) to
77 get multiple Python versions::
78
79 # assuming your system runs Python 3.12
80 pyenv install 3.10
81 pyenv install 3.11
82 pyenv global system 3.10 3.11 3.12
83
84 # reset your shims
85 rm -rf ~/.pyenv/shims && pyenv rehash
86
87 This will give you ``python3.10`` through ``python3.12``.
88 Then run::
89
90 python3.10 -u alltests.py
91 [...]
92 python3.12 -u alltests.py
93
94 .. note::
95 When using the `Python launcher for Windows`__, specify the Python version
96 as option, e.g., ``py -3.12 -u alltests.py`` for Python 3.12.
97
98 .. cf. https://sourceforge.net/p/docutils/bugs/434/
99 __ https://docs.python.org/3/using/windows.html#python-launcher-for-windows
100
101
102 .. [#] Good resources covering the differences between Python versions
103 are the `What's New` documents (`What's New in Python 3.11`__ and
104 similar).
105
106 __ https://docs.python.org/3/whatsnew/3.11.html
107
108
109 .. _supported versions:
110 .. _supported Python versions: ../../README.html#requirements
111 .. _pyenv: https://github.com/yyuu/pyenv
112 .. _installing pyenv: https://github.com/yyuu/pyenv#installation
113 .. _tox: https://pypi.org/project/tox/
114
115
116 Unit Tests
117 ==========
118
119 Unit tests test single functions or modules (i.e. whitebox testing).
120
121 If you are implementing a new feature, be sure to write a test case
122 covering its functionality. It happens very frequently that your
123 implementation (or even only a part of it) doesn't work with an older
124 (or even newer) Python version, and the only reliable way to detect
125 those cases is using tests.
126
127 Often, it's easier to write the test first and then implement the
128 functionality required to make the test pass.
129
130
131 Writing New Tests
132 -----------------
133
134 When writing new tests, it very often helps to see how a similar test
135 is implemented. For example, the files in the
136 ``test_parsers/test_rst/`` directory all look very similar. So when
137 adding a test, you don't have to reinvent the wheel.
138
139 If there is no similar test, you can write a new test from scratch
140 using Python's ``unittest`` module. For an example, please have a
141 look at the following imaginary ``test_square.py``::
142
143 #! /usr/bin/env python3
144
145 # $Id: testing.rst 9906 2024-08-15 08:43:38Z grubert $
146 # Author: Your Name <[email protected]>
147 # Copyright: This module has been placed in the public domain.
148
149 """
150 Test module for docutils.square.
151 """
152
153 import unittest
154 if __name__ == '__main__':
155 # prepend the "docutils root" to the Python library path
156 # so we import the local `docutils` package.
157 sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
158
159 import docutils.square
160
161
162 class SquareTest(unittest.TestCase):
163
164 def test_square(self):
165 self.assertEqual(docutils.square.square(0), 0)
166 self.assertEqual(docutils.square.square(5), 25)
167 self.assertEqual(docutils.square.square(7), 49)
168
169 def test_square_root(self):
170 self.assertEqual(docutils.square.sqrt(49), 7)
171 self.assertEqual(docutils.square.sqrt(0), 0)
172 self.assertRaises(docutils.square.SquareRootError,
173 docutils.square.sqrt, 20)
174
175
176 if __name__ == '__main__':
177 unittest.main()
178
179 For more details on how to write tests, please refer to the
180 documentation of the ``unittest`` module.
181
182 .. Note::
183
184 Unit tests and functional test should generally disable configuration
185 files, setting the `_disable_config`__ setting to True ::
186
187 settings_overrides['_disable_config'] = True
188
189 in order to be independent on the users local configuration.
190
191 __ ../user/config.html#disable-config
192
193 .. _functional:
194
195 Functional Tests
196 ================
197
198 The directory ``test/functional/`` contains data for functional tests.
199
200 Performing functional testing means testing the Docutils system as a
201 whole (i.e. blackbox testing).
202
203
204 Directory Structure
205 -------------------
206
207 + ``functional/`` The main data directory.
208
209 + ``input/`` The input files.
210
211 - ``some_test.rst``, for example.
212
213 + ``output/`` The actual output.
214
215 - ``some_test.html``, for example.
216
217 + ``expected/`` The expected output.
218
219 - ``some_test.html``, for example.
220
221 + ``tests/`` The config files for processing the input files.
222
223 - ``some_test.py``, for example.
224
225
226 The Testing Process
227 -------------------
228
229 When running ``test_functional.py``, all config files in
230 ``functional/tests/`` are processed. (Config files whose names begin
231 with an underscore are ignored.) The current working directory is
232 always Docutils' main test directory (``test/``).
233
234 For example, ``functional/tests/some_test.py`` could read like this::
235
236 # Source and destination file names.
237 test_source = "some_test.rst"
238 test_destination = "some_test.html"
239
240 # Keyword parameters passed to publish_file.
241 reader_name = "standalone"
242 parser_name = "rst"
243 writer_name = "html"
244 settings_overrides['output-encoding'] = 'utf-8'
245 # Relative to main ``test/`` directory.
246 settings_overrides['stylesheet_path'] = '../docutils/writers/html4css1/html4css1.css'
247
248 The two variables ``test_source`` and ``test_destination`` contain the
249 input file name (relative to ``functional/input/``) and the output
250 file name (relative to ``functional/output/`` and
251 ``functional/expected/``). Note that the file names can be chosen
252 arbitrarily. However, the file names in ``functional/output/`` *must*
253 match the file names in ``functional/expected/``.
254
255 ``test_source`` and ``test_destination`` are removed from the
256 namespace, as are all variables whose names begin with an underscore
257 ("_"). The remaining names are passed as keyword arguments to
258 ``docutils.core.publish_file``, so you can set reader, parser, writer
259 and anything else you want to configure. Note that
260 ``settings_overrides`` is already initialized as a dictionary *before*
261 the execution of the config file.
262
263
264 Creating New Tests
265 ------------------
266
267 In order to create a new test, put the input test file into
268 ``functional/input/``. Then create a config file in
269 ``functional/tests/`` which sets at least input and output file names,
270 reader, parser and writer.
271
272 Now run ``test_functional.py``. The test will fail, of course,
273 because you do not have an expected output yet. However, an output
274 file will have been generated in ``functional/output/``. Check this
275 output file for validity [#]_ and correctness. Then copy the file to
276 ``functional/expected/``.
277
278 If you rerun ``test_functional.py`` now, it should pass.
279
280 If you run ``test_functional.py`` later and the actual output doesn't
281 match the expected output anymore, the test will fail.
282
283 If this is the case and you made an intentional change, check the
284 actual output for validity and correctness, copy it to
285 ``functional/expected/`` (overwriting the old expected output), and
286 commit the change.
287
288 .. [#] `Docutils XML` can be validated with
289 ``xmllint --dtdvalid docs/ref/docutils.dtd --noout *.xml``.