Skip to main content

Semantic Unit Testing

What's semantic unit testing?

Semantic unit testing is a testing approach that evaluates whether a function's implementation aligns with its documented behavior. The code is analyzed using LLMs to assess whether the implementation matches the expected behavior described in the docstring.

Here's an example of how to use it

from suite import suite

tester = suite(model_name="openai/o3-mini")

def multiply(x: int, y: int):
    """Multiplies x by y

    Args:
        x (int): value
        y (int): value
    """
    return x + y

result = tester(multiply)
print(result)

# {'reasoning': "The function's docstring states that it should multiply x by y. 
#   However, the implementation returns x + y, which is addition instead of multiplication. 
#   Therefore, the implementation does not correctly fulfill what is described in the docstring.",
# 'passed': False}

In this example, the implementation of multiply contains an error (it uses addition instead of multiplication). When the tester is called with the multiply function, it evaluates the implementation against the docstring, providing feedback on any discrepancies. This process helps ensure that the function behaves as expected and adheres to its documentation.

Why?

  • Comprehensive Coverage: Traditional unit testing focuses on specific inputs and outputs, covering only a small surface of the code. suite, on the other hand, evaluates the semantic correctness of functions by analyzing their implementation against their documentation.
  • No need to write tests by hand: Writing tests by hand can be tiring and non-exhaustive. By using LLMs, we can avoid having to write specific examples one by one. This not only saves time but also ensures that a wider range of scenarios and edge cases are considered, leading to more robust testing outcomes.
  • Enhanced Reasoning with LLMs: By passing code and context to LLMs, Suite enables a deeper level of reasoning about the function's behavior. This capability allows for more nuanced evaluations.

How?

This library uses llm package by Simon Willison. When testing a method, its source code, docstring, and the dependencies information (any other method used by the code under testing) are retrieved and passed to an LLM for evaluation. Then, the LLM decides if the evaluation is correct or not.

Since we're using llm library we can use any supported model. From my experience, reasoning models that support structured outputs are the ones that work the best (eg: o3-mini).

Usage

To use the suite module, you can create an instance of the suite or async_suite class, depending on your needs. You will then pass the function you want to test, and suite will evaluate its implementation against its docstring, providing feedback on any discrepancies.

You have a couple of examples in the examples folder.

The intended usage of this package is for testing, so you could do something like

# tests/test_multiply.py

from package import multiply
from suite import suite

tester = suite(model="openai/o3-mini")

def test_multiply():
    assert tester(multiply)

Since suite also supports async operations you can use pytest-asyncio to speed up your tests (you don't need to run them sequentially since the bottlenck is not your laptop but the LLM provider).

Metadata

Release files for suite 1.0.1

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

Source distribution (sdist)

Source distribution for suite 1.0.1
File Size Uploaded Provenance Actions
suite-1.0.1.tar.gz 9.3 kB With provenance Details for suite-1.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 17.0 kB

Release files / suite-1.0.1.tar.gz

Download URL suite-1.0.1.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
44df40f94b4d0414b5a783e7f4147abd3a60997a3cc1e8a24af798cf6cea764e
BLAKE2b-256 checksum
How to use checksums
1252bc841706d3aafa99e936d793ee9605c07c2bad0c6324a5a3db8542ed70af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 10, 2025.

Transparency log

Release files / suite-1.0.1-py3-none-any.whl

Download URL suite-1.0.1-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e0121a7846bbd6a2123d7484e1fe102aedd3e0b44e25eed30cf2182c9ed037f6
BLAKE2b-256 checksum
How to use checksums
879e5979421674acee11d6443fc624f33ed3b0c16df999c66d50fb5d869eb2b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 10, 2025.

Transparency log

Security for suite 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for suite-1.0.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 10, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files Full provenance

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