You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -81,7 +81,7 @@ the output. Thus the example source needs to be valid python code still.
81
81
## Install and test
82
82
83
83
```
84
-
$ pip install -e .
84
+
$ pip install scipy-doctest
85
85
$ pytest --pyargs scipy_doctest
86
86
```
87
87
@@ -93,9 +93,56 @@ or nearly so.
93
93
94
94
The other layer is the `pytest` plugin.
95
95
96
+
### Run doctests via pytest
97
+
98
+
To run doctests on your package or project, follow these steps:
99
+
100
+
1.**Install the plugin**
101
+
102
+
```bash
103
+
pip install scipy-doctest
104
+
```
105
+
106
+
2.**Register or load the plugin**
107
+
108
+
Next, you need to register or load the pytest plugin within your test module or `conftest.py` file.
109
+
110
+
To do this, add the following line of code:
111
+
112
+
```python
113
+
# In your conftest.py file or test module
114
+
115
+
pytest_plugins ="scipy_doctest"
116
+
```
117
+
118
+
Check out the [pytest documentation](https://docs.pytest.org/en/stable/how-to/writing_plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file) for more information on requiring/loading plugins in a test module or `conftest.py` file.
119
+
120
+
3.**Run doctests**
121
+
122
+
Once the plugin is registered, run the doctests by executing the following command:
See [More fine-grained control](https://github.com/scipy/scipy_doctest#More-fine-grained-control) section
140
+
for details on how to customize the behavior.
141
+
96
142
97
143
### Basic usage
98
144
145
+
The use of `pytest` is optional, and you can use the `doctest` layer API.
99
146
For example,
100
147
101
148
```
@@ -112,7 +159,8 @@ For more details, see the `testmod` docstring. Other useful functions are
112
159
`find_doctests`, `run_docstring_examples` and `testfile` (the latter two mimic
113
160
the behavior of the eponymous functions of the `doctest` module).
114
161
115
-
#### Command-line interface
162
+
163
+
### Command-line interface
116
164
117
165
There is a basic CLI, which also mimics that of the `doctest` module:
118
166
```
@@ -127,8 +175,10 @@ Text files can also be CLI-checked:
127
175
$ python -m scipy_doctest bar.rst
128
176
```
129
177
178
+
Notice that the command-line usage only uses the default `DTConfig` settings.
179
+
130
180
131
-
####More fine-grained control
181
+
## More fine-grained control
132
182
133
183
More fine-grained control of the functionality is available via the following
134
184
classes
@@ -147,134 +197,104 @@ configuration is simply creating an instance, overriding an attribute and
147
197
passing the instance to `testmod` or constructors of `DT*` objects. Defaults
148
198
are provided, based on a long-term usage in SciPy.
149
199
200
+
See the [DTConfig docstring](https://github.com/scipy/scipy_doctest/blob/main/scipy_doctest/impl.py#L24)
201
+
for the full set of attributes that allow you to fine-tune your doctesting experience.
150
202
151
-
### The SciPy Doctest Pytest Plugin
203
+
To set any of these attributes, create an instance of `DTConfig` and assign the attributes
204
+
in a usual way.
152
205
153
-
The pytest plugin enables the use of `scipy_doctest` tools to perform doctests.
206
+
If using the pytest plugin, it is convenient to use the default instance, which
207
+
is predefined in `scipy_doctest/conftest.py`. This instance will be automatically
208
+
passed around via an
209
+
[attribute of pytest's `Config` object](https://github.com/scipy/scipy_doctest/blob/58ff06a837b7bff1dbac6560013fc6fd07952ae2/scipy_doctest/plugin.py#L39).
154
210
155
-
Follow the given instructions to utilize the pytest plugin for doctesting.
211
+
### Examples
156
212
157
-
### Running Doctests on SciPy
213
+
```
214
+
dt_config = DTConfig()
215
+
```
158
216
159
-
1.**Install plugin**
217
+
or, if using pytest,
160
218
161
-
```bash
162
-
pip install scipy-doctest
219
+
```python
220
+
from scipy_doctest.conftest import dt_config # a DTConfig instance with default settings
163
221
```
164
222
165
-
2.**Configure Your Doctesting Experience**
223
+
and then
166
224
167
-
To tailor your doctesting experience, you can utilize an instance of `DTConfig`.
168
-
An in-depth explanation is given in the [tailoring your doctesting experience](https://github.com/scipy/scipy_doctest#tailoring-your-doctesting-experience) section.
To run doctests on specific SciPy modules, e.g: `cluster`, use the following command:
242
+
If you don't set these attributes, the [default settings](https://github.com/scipy/scipy_doctest/blob/58ff06a837b7bff1dbac6560013fc6fd07952ae2/scipy_doctest/impl.py#L94) of the attributes are used.
180
243
181
-
```bash
182
-
python dev.py smoke-docs -s cluster
183
-
```
184
244
185
-
###Running Doctests on Other Packages/Projects
245
+
#### Alternative Checkers
186
246
187
-
If you want to run doctests on packages or projects other than SciPy, follow these steps:
247
+
By default, we use the floating-point aware `DTChecker`. If you want to use an
248
+
alternative checker, all you need to do is to define the corresponding class,
249
+
and add an attribute to the `DTConfig` instance. For example,
188
250
189
-
1.**Install the plugin**
190
251
191
-
```bash
192
-
pip install scipy-doctest
252
+
```
253
+
class VanillaOutputChecker(doctest.OutputChecker):
254
+
"""doctest.OutputChecker to drop in for DTChecker.
255
+
256
+
LSP break: OutputChecker does not have __init__,
257
+
here we add it to agree with DTChecker.
258
+
"""
259
+
def __init__(self, config):
260
+
pass
193
261
```
194
262
195
-
2.**Register or Load the Plugin**
263
+
and
196
264
197
-
Next, you need to register or load the pytest plugin within your test module or `conftest.py` file.
265
+
```
266
+
dt_config = DTConfig()
267
+
dt_config.CheckerKlass = VanillaOutputChecker
268
+
```
198
269
199
-
To do this, add the following line of code:
270
+
See [a pytest example](https://github.com/scipy/scipy_doctest/blob/main/scipy_doctest/tests/test_pytest_configuration.py#L63)
271
+
and [a doctest example](https://github.com/scipy/scipy_doctest/blob/main/scipy_doctest/tests/test_runner.py#L94)
272
+
for more details.
200
273
201
-
```python
202
-
# In your conftest.py file or test module
203
274
204
-
pytest_plugins ="scipy_doctest"
205
-
```
275
+
### The SciPy Doctest Pytest Plugin
206
276
207
-
Check out the [pytest documentation](https://docs.pytest.org/en/stable/how-to/writing_plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file) for more information on requiring/loading plugins in a test module or `conftest.py` file.
277
+
The pytest plugin enables the use of `scipy_doctest` tools to perform doctests.
208
278
209
-
3.**Configure your doctesting experience**
279
+
Follow the given instructions to utilize the pytest plugin for doctesting.
210
280
211
-
An in-depth explanation is given in the [tailoring your doctesting experience](https://github.com/scipy/scipy_doctest#tailoring-your-doctesting-experience) section.
281
+
### NumPy and SciPy wrappers
212
282
213
-
4.**Run doctests**
214
283
215
-
Once the plugin is registered, you can run your doctests by executing the following command:
284
+
NumPy wraps `scipy-doctest` with the `spin` command
[DTConfig](https://github.com/scipy/scipy_doctest/blob/main/scipy_doctest/impl.py#L23) offers a variety of attributes that allow you to fine-tune your doctesting experience.
235
-
236
-
These attributes include:
237
-
1.**default_namespace (dict):** Defines the namespace in which examples are executed.
238
-
2.**check_namespace (dict):** Specifies the namespace for conducting checks.
239
-
3.**rndm_markers (set):** Provides additional directives which act like `# doctest: + SKIP`.
240
-
4.**atol (float) and rtol (float):** Sets absolute and relative tolerances for validating doctest examples.
241
-
Specifically, it governs the check using `np.allclose(want, got, atol=atol, rtol=rtol)`.
242
-
5.**optionflags (int):** These are doctest option flags.
243
-
The default setting includes `NORMALIZE_WHITESPACE` | `ELLIPSIS` | `IGNORE_EXCEPTION_DETAIL`.
244
-
6.**stopwords (set):** If an example contains any of these stopwords, the output is not checked (though the source's validity is still assessed).
245
-
7.**pseudocode (list):** Lists strings that, when found in an example, result in no doctesting. This resembles the `# doctest +SKIP` directive and is useful for pseudocode blocks or similar cases.
246
-
8.**skiplist (set):** A list of names of objects with docstrings known to fail doctesting and are intentionally excluded from testing.
247
-
9.**user_context_mgr:** A context manager for running tests.
248
-
Typically, it is entered for each DocTest (especially in API documentation), ensuring proper testing isolation.
249
-
10.**local_resources (dict):** Specifies local files needed for specific tests. The format is `{test.name: list-of-files}`. File paths are relative to the path of `test.filename`.
250
-
11.**parse_namedtuples (bool):** Determines whether to perform a literal comparison (e.g., `TTestResult(pvalue=0.9, statistic=42)`) or extract and compare the tuple values (e.g., `(0.9, 42)`). The default is `True`.
251
-
12.**nameerror_after_exception (bool):** Controls whether subsequent examples in the same test, after one has failed, may raise spurious NameErrors. Set to `True` if you want to observe these errors or if your test is expected to raise NameErrors. The default is `False`.
252
-
253
-
To set any of these attributes, create an instance of `DTConfig` called `dt_config`.
254
-
This instance is already set as an [attribute of pytest's `Config` object](https://github.com/scipy/scipy_doctest/blob/58ff06a837b7bff1dbac6560013fc6fd07952ae2/scipy_doctest/plugin.py#L39).
255
-
256
-
**Example:**
257
-
258
-
```python
259
-
from scipy_doctest import dt_config # a DTCongig instance with default settings
$ python dev.py smoke-tutorials # ReST user guide tutorials
271
295
```
272
296
273
-
If you don't set these attributes, the [default settings](https://github.com/scipy/scipy_doctest/blob/58ff06a837b7bff1dbac6560013fc6fd07952ae2/scipy_doctest/impl.py#L94) of the attributes are used.
274
-
275
-
By following these steps, you will be able to effectively use the SciPy Doctest pytest plugin for doctests in your Python projects.
276
297
277
-
Happy testing!
278
298
279
299
## Rough edges and sharp bits
280
300
@@ -308,7 +328,7 @@ being optional. So you either guard the imports in doctests (yikes!), or
308
328
the collections fails if dependencies are not available.
309
329
310
330
The solution is to explicitly `--ignore` the paths to modules with optionals.
311
-
(or use `DTConfig.pytest_extra_ignore` list):
331
+
(or, equivalently, use `DTConfig.pytest_extra_ignore` list):
0 commit comments