Debian Codegraph @

test/expensive_benchmarks/docutils_data/docs/dev/testing.txt

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