Skip to content

Commit cc3fcaf

Browse files
authored
Turn on basic doctests for modules and .rst docs (#511)
* Turn on basic doctests for modules and .rst docs * Short documentation of docstrings with link to syntax * Added docstring tests to example module
1 parent 193fa24 commit cc3fcaf

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

docs/practices/unit_testing.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@ expected.
2929

3030
It's also worth noting that if you want to write exploratory tests as you develop
3131
your code, but you *do not* want those tests to be included in automated test runs,
32-
feel free to place those tests in a directory outside of the ``./tests`` directory.
32+
feel free to place those tests in a directory outside of the ``./tests`` and
33+
``./src`` directories.
3334

3435
Note that ``pytest`` will recursively search subdirectories inside of ``./tests``
3536
while searching for tests to run.
37+
38+
doctests
39+
-------------------------------------------------------------------------------
40+
41+
In addition to the usual ways of writing unit tests with pytest, our template
42+
supports tests embedded in documentation using pytest's
43+
`doctest <https://doc.pytest.org/en/latest/how-to/doctest.html>`_ component.
44+
Documentation comments in all source files, as well as ``.rst`` files in the ``./docs``
45+
directory can contain doctests in the format outlined
46+
`here <https://doc.pytest.org/en/latest/how-to/doctest.html>`_.

python-project-template/pyproject.toml.jinja

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ write_to = "src/{{package_name}}/_version.py"
6363
[tool.pytest.ini_options]
6464
testpaths = [
6565
"tests",
66+
"src",
67+
"docs",
6668
]
69+
addopts = "--doctest-modules --doctest-glob=*.rst"
6770

6871
[tool.black]
6972
line-length = 110

python-project-template/src/{{package_name}}/{% if create_example_module %}example_module.py{% endif %}

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
def greetings() -> str:
55
"""A friendly greeting for a future friend.
66

7+
Example:
8+
>>> greetings()
9+
'Hello from LINCC-Frameworks!'
10+
11+
Note: this example becomes a unit test via doctests
12+
713
Returns
814
-------
915
str
@@ -15,6 +21,12 @@ def greetings() -> str:
1521
def meaning() -> int:
1622
"""The meaning of life, the universe, and everything.
1723

24+
Example:
25+
>>> meaning()
26+
42
27+
28+
Note: this example becomes a unit test via doctests
29+
1830
Returns
1931
-------
2032
int

0 commit comments

Comments
 (0)