Debian Codegraph @

docs/test_tools.md

raw
1 title: Test Tools
2
3 # Test Tools
4
5 Python-Markdown provides some testing tools which simplify testing actual
6 Markdown output against expected output. The tools are built on the Python
7 standard library [`unittest`][unittest]. Therefore, no additional libraries are
8 required. While Python-Markdown uses the tools for its own tests, they were
9 designed and built so that third party extensions could use them as well.
10 Therefore, the tools are importable from `markdown.test_tools`.
11
12 The test tools include two different `unittest.TestCase` subclasses:
13 `markdown.test_tools.TestCase` and `markdown.test_tools.LegacyTestCase`.
14
15 ## `markdown.test_tools.TestCase`
16
17 The `markdown.test_tools.TestCase` class is a `unittest.TestCase` subclass with
18 a few additional helpers to make testing Markdown output easier.
19
20 Properties
21 : `default_kwargs`: A `dict` of keywords to pass to Markdown for each
22 test. The defaults can be overridden on individual tests.
23
24 Methods
25 : `assertMarkdownRenders`: accepts the source text, the expected output, an optional
26 dictionary of `expected_attrs`, and any keywords to pass to Markdown. The
27 `default_kwargs` defined on the class are used except where overridden by
28 keyword arguments. The output and expected output are passed to
29 `TestCase.assertMultiLineEqual`. An `AssertionError` is raised with a diff
30 if the actual output does not equal the expected output. The optional
31 keyword `expected_attrs` accepts a dictionary of attribute names as keys with
32 expected values. Each value is checked against the attribute of that
33 name on the instance of the `Markdown` class using `TestCase.assertEqual`. An
34 `AssertionError` is raised if any value does not match the expected value.
35
36 : `dedent`: Dedent triple-quoted strings.
37
38 In all other respects, `markdown.test_tools.TestCase` behaves as
39 `unittest.TestCase`. In fact, `assertMarkdownRenders` tests could be mixed with
40 other `unittest` style tests within the same test class.
41
42 An example Markdown test might look like this:
43
44 ```python
45 from markdown.test_tools import TestCase
46
47 class TestHr(TestCase):
48 def test_hr_before_paragraph(self):
49 self.assertMarkdownRenders(
50 # The Markdown source text used as input
51 self.dedent(
52 """
53 ***
54 An HR followed by a paragraph with no blank line.
55 """
56 ),
57 # The expected HTML output
58 self.dedent(
59 """
60 <hr>
61 <p>An HR followed by a paragraph with no blank line.</p>
62 """
63 ),
64 # Other keyword arguments to pass to `markdown.markdown`
65 output_format='html'
66 )
67 ```
68
69 ## `markdown.test_tools.LegacyTestCase`
70
71 In the past Python-Markdown exclusively used file-based tests. Many of those
72 tests still exist in Python-Markdown's test suite, including the test files from
73 the [reference implementation][perl] (`markdown.pl`) and [PHP Markdown][PHP].
74 Each test consists of a matching pair of text and HTML files. The text file
75 contains a snippet of Markdown source text formatted for a specific syntax
76 feature and the HTML file contains the expected HTML output of that snippet.
77 When the test suite is run, each text file is run through Markdown and the
78 output is compared with the HTML file as a separate unit test. When a test
79 fails, the error report includes a diff of the expected output compared to the
80 actual output to easily identify any problems.
81
82 A separate `markdown.test_tools.LegacyTestCase` subclass must be created for
83 each directory of test files. Various properties can be defined within the
84 subclass to point to a directory of text-based test files and define various
85 behaviors/defaults for those tests. The following properties are supported:
86
87 * `location`: A path to the directory of test files. An absolute path is
88 preferred.
89 * `exclude`: A list of tests to skip. Each test name should comprise of a
90 file name without an extension.
91 * `normalize`: A boolean value indicating if the HTML should be normalized.
92 Default: `False`. Note: Normalization of HTML requires that [PyTidyLib] be
93 installed on the system. If PyTidyLib is not installed and `normalize` is set
94 to `True`, then the test will be skipped, regardless of any other settings.
95 * `input_ext`: A string containing the file extension of input files.
96 Default: `.txt`.
97 * `output_ext`: A string containing the file extension of expected output files.
98 Default: `html`.
99 * `default_kwargs`: A `markdown.test_tools.Kwargs` instance which stores the
100 default set of keyword arguments for all test files in the directory.
101
102 In addition, properties can be defined for each individual set of test files
103 within the directory. The property should be given the name of the file without
104 the file extension. Any spaces and dashes in the file name should be replaced
105 with underscores. The value of the property should be a
106 `markdown.test_tools.Kwargs` instance which contains the keyword arguments that
107 should be passed to `markdown.markdown` for that test file. The keyword
108 arguments will "update" the `default_kwargs`.
109
110 When the class instance is created during a test run, it will walk the given
111 directory and create a separate unit test for each set of test files using the
112 naming scheme: `test_filename`. One unit test will be run for each set of input
113 and output files.
114
115 The definition of an example set of tests might look like this:
116
117 ```python
118 from markdown.test_tools import LegacyTestCase, Kwargs
119 import os
120
121 # Get location of this file and use to find text file dirs.
122 parent_test_dir = os.path.abspath(os.path.dirname(__file__))
123
124
125 class TestFoo(LegacyTestCase):
126 # Define location of text file directory. In this case, the directory is
127 # named "foo" and is in the same parent directory as this file.
128 location = os.path.join(parent_test_dir, 'foo')
129 # Define default keyword arguments. In this case, unless specified
130 # differently, all tests should use the output format "html".
131 default_kwargs = Kwargs(output_format='html')
132
133 # The "xhtml" test should override the output format and use "xhtml".
134 xhtml = Kwargs(output_format='xhtml')
135
136 # The "toc" test should use the "toc" extension with a custom permalink
137 # setting.
138 toc = Kwargs(
139 extensions=['markdown.extensions.toc'],
140 extension_configs={'markdown.extensions.toc': {'permalink': "[link]"}}
141 )
142 ```
143
144 Note that in the above example, the text file directory may contain many more
145 text-based test files than `xhtml` (`xhtml.txt` and `xhtml.html`) and `toc`
146 (`toc.txt` and `toc.html`). As long as each set of files exists as a pair, a
147 test will be created and run for each of them. Only the `xhtml` and `toc` tests
148 needed to be specifically identified as they had specific, non-default settings
149 which needed to be defined.
150
151 ## Running Python-Markdown's Tests
152
153 As all of the tests for the `markdown` library are unit tests, standard
154 `unittest` methods of calling tests can be used. For example, to run all of
155 Python-Markdown's tests, from the root of the git repository, run the following
156 command:
157
158 ```sh
159 python -m unittest discover tests
160 ```
161
162 That simple command will search everything in the `tests` directory and it's
163 sub-directories and run all `unittest` tests that it finds, including
164 `unittest.TestCase`, `markdown.test_tools.TestCase`, and
165 `markdown.test_tools.LegacyTestCase` subclasses. Normal [unittest] discovery
166 rules apply.
167
168 !!! seealso "See Also"
169
170 See the [Contributing Guide] for instructions on setting up a
171 [development environment] for running the tests.
172
173 [unittest]: https://docs.python.org/3/library/unittest.html
174 [Perl]: https://daringfireball.net/projects/markdown/
175 [PHP]: http://michelf.com/projects/php-markdown/
176 [PyTidyLib]: http://countergram.github.io/pytidylib/
177 [Contributing Guide]: contributing.md
178 [development environment]: contributing.md#development-environment