Debian Codegraph @

docs/configuration.rst

raw
1 Configuring nose2
2 =================
3
4 Configuration Files
5 -------------------
6
7 nose2 can be configured via standard, ini-style config files.
8 The default files are ``unittest.cfg`` and ``nose2.cfg`` in the start directory.
9
10 The ini format has sections marked off by brackets ("``[unittest]``") and
11 ``key = value`` pairs within those sections.
12 When the value is a list, put each value into its own line with proper
13 indentation ::
14
15 key_expecting_list = value1
16 value2
17
18 Two command line options, :option:`-c` and :option:`--no-user-config`
19 may be used to determine which config files are loaded.
20
21 .. cmdoption :: -c CONFIG, --config CONFIG
22
23 Config files to load. Default behavior is to look for
24 ``unittest.cfg`` and ``nose2.cfg`` in the start directory, as well
25 as any user config files (unless :option:`--no-user-config` is
26 selected).
27
28 .. cmdoption :: --no-user-config
29
30 Do not load user config files. If not specified, in addition to the
31 standard config files and any specified with :option:`-c`, nose2
32 will look for ``.unittest.cfg`` and ``.nose2.cfg`` in the user's
33 $HOME directory.
34
35
36 Configuring Test Discovery
37 ~~~~~~~~~~~~~~~~~~~~~~~~~~
38
39 The ``[unittest]`` section of nose2 config files is used to configure
40 nose2 itself. The following options are available to configure test
41 discovery:
42
43 .. rst:configvar :: start-dir
44
45 This option configures the default directory to start discovery.
46 The default value is ``"."`` (the current directory where nose2
47 is executed). This directory is where nose2 will start looking for
48 tests.
49
50 .. rst:configvar :: code-directories
51
52 This option configures nose2 to add the named directories to
53 sys.path and the discovery path. Use this if your project has
54 code in a location other than the top level of the project, or the
55 directories ``lib`` or ``src``. The value here may be a list: put each
56 directory on its own line in the config file.
57
58 .. rst:configvar :: test-file-pattern
59
60 This option configures how nose detects test modules. It is a file
61 glob.
62
63 .. rst:configvar :: test-method-prefix
64
65 This option configures how nose detects test functions and
66 methods. The prefix set here will be matched (via simple string
67 matching) against the start of the name of each method in test
68 cases and each function in test modules.
69
70 Examples:
71
72 .. code-block :: ini
73
74 [unittest]
75 start-dir = tests
76 code-directories = source
77 more_source
78 test-file-pattern = *_test.py
79 test-method-prefix = t
80
81
82 Specifying Plugins to Load
83 ~~~~~~~~~~~~~~~~~~~~~~~~~~
84
85 To avoid loading any plugins, use the :option:`--no-plugins`
86 option. Beware, though: nose2 does all test discovery and loading via
87 plugins, so unless you are patching in a custom test loader and
88 runner, when run with :option:`--no-plugins`, nose2 will do nothing.
89
90 .. cmdoption :: --no-plugins
91
92 Do not load any plugins. *This kills the nose2.*
93
94 To specify plugins to load beyond the builtin plugins automatically
95 loaded, add a :config:`plugins` entry under the ``[unittest]``
96 section in a config file.
97
98 .. rst:configvar :: plugins
99
100 List of plugins to load. Put one plugin module on each line.
101
102 To exclude some plugins that would otherwise be loaded, add an
103 :config:`exclude-plugins` entry under the ``[unittest]``
104 section in a config file.
105
106 .. rst:configvar :: exclude-plugins
107
108 List of plugins to exclude. Put one plugin module on each line.
109
110 .. note ::
111
112 It bears repeating that in both :config:`plugins` and
113 :config:`exclude-plugins` entries, you specify the plugin *module*,
114 not the plugin *class*.
115 The module is specified by the (dot-separated) *fully qualified* name.
116
117 Examples:
118
119 .. code-block :: ini
120
121 [unittest]
122 plugins = myproject.plugins.frobulate
123 otherproject.contrib.plugins.derper
124
125 exclude-plugins = nose2.plugins.loader.functions
126 nose2.plugins.outcomes
127
128
129 Configuring Plugins
130 -------------------
131
132 Most plugins specify a config file section that may be used to
133 configure the plugin. If nothing else, any plugin that specifies a
134 config file section can be set to automatically register by including
135 ``always-on = True`` in its config:
136
137 .. code-block :: ini
138
139 [my-plugin]
140 always-on = True
141
142 Plugins may accept any number of other config values, which may be
143 booleans, strings, integers or lists. A polite plugin will document
144 these options somewhere. Plugins that want to make use of nose2's
145 `Sphinx`_ extension as detailed in :doc:`dev/documenting_plugins`
146 *must* extract all of their config values in their ``__init__``
147 methods.
148
149 .. _Sphinx : http://sphinx.pocoo.org/
150
151
152 Test Runner Tips and Tweaks
153 ---------------------------
154
155 Running Tests in a Single Module
156 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157
158 You can use ``nose2.main`` in the same way that ``unittest.main`` (and
159 ``unittest2.main``) have historically worked: to run the tests in a
160 single module. Just put a block like the following at the end of the
161 module::
162
163 if __name__ == '__main__':
164 import nose2
165 nose2.main()
166
167 Then *run the module directly* -- In other words, do not run the
168 ``nose2`` script.
169
170 Rolling Your Own Runner
171 ~~~~~~~~~~~~~~~~~~~~~~~
172
173 You can take more control over the test runner by foregoing the
174 ``nose2`` script and rolling your own. To do that, you just need to
175 write a script that calls ``nose2.discover``, for instance::
176
177 if __name__ == '__main__':
178 import nose2
179 nose2.discover()
180
181 You can pass several keyword arguments to ``nose2.discover``, all of
182 which are detailed in the documentation for
183 :class:`nose2.main.PluggableTestProgram`.
184
185 Altering the Default Plugin Set
186 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
187
188 To add plugin *modules* to the list of those automatically loaded, you
189 can pass a list of module names to add (the ``plugins``) argument or
190 exclude (``excludedPlugins``). You can also subclass
191 :class:`nose2.main.PluggableTestProgram` and set the class-level
192 ``defaultPlugins`` and ``excludePlugins`` attributes to alter plugin
193 loading.
194
195 When Loading Plugins from Modules is not Enough
196 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
197
198 **None of which will help** if you need to register a plugin *instance*
199 that you've loaded yourself. For that, use the ``extraHooks`` keyword
200 argument to ``nose2.discover``. Here, you pass in a list of 2-tuples,
201 each of which contains a hook name and a plugin *instance* to register
202 for that hook. This allows you to register plugins that need runtime
203 configuration that is not easily passed in through normal channels --
204 and also to register *objects that are not nose2 plugins* as hook
205 targets. Here's a trivial example::
206
207 if __name__ == '__main__':
208 import nose2
209
210 class Hello(object):
211 def startTestRun(self, event):
212 print("hello!")
213
214 nose2.discover(extraHooks=[('startTestRun', Hello())])
215
216 This can come in handy when integrating with other systems that expect
217 you to provide a test runner that they execute, rather than executing
218 tests yourself (django, for instance).