Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/practices/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ expected.

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

Note that ``pytest`` will recursively search subdirectories inside of ``./tests``
while searching for tests to run.

doctests
-------------------------------------------------------------------------------

In addition to the usual ways of writing unit tests with pytest, our template
supports tests embedded in documentation using pytest's
`doctest <https://doc.pytest.org/en/latest/how-to/doctest.html>`_ component.
Documentation comments in all source files, as well as ``.rst`` files in the ``./docs``
directory can contain doctests in the format outlined
`here <https://doc.pytest.org/en/latest/how-to/doctest.html>`_.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be some other link?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there's a better one. This change doesn't include anything .rst specific, so you just have to use the normal syntax in your .rst files.

AFAICT that one page is the only obviously authoritative place on the internet that explains the syntax. If we somehow do something special and more .rst friendly we could amend this.

Copy link
Contributor

@hombit hombit Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doctest is a part of PEP287, and it is support by the standard library's doctest module. So pytest docs is not the only authoritative place...

3 changes: 3 additions & 0 deletions python-project-template/pyproject.toml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ write_to = "src/{{package_name}}/_version.py"
[tool.pytest.ini_options]
testpaths = [
"tests",
"src",
"docs",
]
addopts = "--doctest-modules --doctest-glob=*.rst"

[tool.black]
line-length = 110
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
def greetings() -> str:
"""A friendly greeting for a future friend.

Example:
>>> greetings()
'Hello from LINCC-Frameworks!'

Note: this example becomes a unit test via doctests

Returns
-------
str
Expand All @@ -15,6 +21,12 @@ def greetings() -> str:
def meaning() -> int:
"""The meaning of life, the universe, and everything.

Example:
>>> meaning()
42

Note: this example becomes a unit test via doctests

Returns
-------
int
Expand Down