Skip to main content

test2doc

Generate documentation from tests.

To install: pip install test2doc

Motivation

If you're like me, you think that having tests and documentation is good, but writing them is soul-killing. It's repetitive and so easy to deviate from a consistent look, make mistakes, and all that jazz.

Fortunately, there are tools out there to help out. Tools that will transform your docs into tests, so you not only can make sure that the examples in your docs actually work, but also get some test coverage from your docs.

That's doc2test stuff, and it's nice.

But what about test2doc?

What if you have a nicely commented test function like the following one in your tests/ folder:

def test_func(func, wrap):
    from inspect import signature

    # Just wrapping the func gives you a sort of copy of the func.
    wrapped_func = wrap(func)  # no transformations
    # The behavior remains the same:
    assert wrapped_func(2, "co") == "coco" == func(2, "co")
    # ... and the signature as well:
    assert str(signature(wrapped_func)) == "(a, b: str, c='hi')"

Who's going to read that?

The nerds, okay, but not the normal people, the many users you'd have if you had this as a nicely formatted documentation.

Something that would look like

About test_func(func, wrap)

from inspect import signature

Just wrapping the func gives you a sort of copy of the func.

wrapped_func = wrap(func)  # no transformations

The behavior remains the same:

assert wrapped_func(2, "co") == "coco" == func(2, "co")

... and the signature as well:

assert str(signature(wrapped_func)) == "(a, b: str, c='hi')"

You know what I'm getting at...

Do it like this:

from test2doc import code_to_docs

docs = code_to_docs(test_func)

Metadata

Release files for test2doc 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for test2doc 0.0.4
File Size Uploaded
test2doc-0.0.4.tar.gz 15.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for test2doc 0.0.4
File Interpreter ABI Platform
test2doc-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 28.8 kB

Release files / test2doc-0.0.4.tar.gz

Download URL test2doc-0.0.4.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
942bd95fc40b9d3b2fb59474c20bbb5944653f2d8d8dd2dd54fcec503e491b26
BLAKE2b-256 checksum
How to use checksums
38c6c7389c46eb973c570ccdcebc3fc8efdb12cbf67adde3fbdd348c046ec913
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / test2doc-0.0.4-py3-none-any.whl

Download URL test2doc-0.0.4-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7fa1e5471731c29a1c0ddf3ea9bf6bab81f343a3b2ab6309b9ceb488058e5e89
BLAKE2b-256 checksum
How to use checksums
85766d8787347483866ef76e2dc7930609d96c1ab3a82131d17de640995d74d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page