Skip to main content

PyPI Build

GivenPy

GivenPy is a super simple yet highly extensible micro testing library to enable easier behavior driven development in Python.

It doesn't require any fancy dependencies and can work with any test execution framework like unittest or pytest.

It is basically a syntax sugar that allows you to write tests in a more readable way. It also helps you to encourage the re-use of setup code (given steps).

Examples

A quick preview of what you can do with GivenPy:

import unittest
from typing import Dict

from givenpy import given, when, then
from hamcrest import *
from starlette.testclient import TestClient

from tests.steps import prepare_api_server, create_test_client, prepare_injector


class TestExample(unittest.TestCase):
    def test_health_works(self):
        with given([
            prepare_injector(),  # custom code to prepare the test environment
            prepare_api_server(),
            create_test_client(),
        ]) as context:
            client: TestClient = context.client

            with when():
                response: Dict = client.get('/api/v1/health').json()

            with then():
                assert_that(response, instance_of(dict))
                assert_that(response['status'], is_(equal_to('OK')))

Installation

pip install givenpy PyHamcrest

For AI coding agents

Install the GivenPy behavior tests skill in your own application's project to help your coding agent write meaningful tests using GivenPy and PyHamcrest. With Node.js/npm available, run:

npx skills add tadas-subonis/givenpy --skill givenpy-behavior-tests

Choose your coding agent when prompted. Add -g to install for your user across projects. For available agent options, see the Skills CLI documentation.

Then ask your agent: "Use the GivenPy behavior tests skill to test account withdrawal." Automatic skill selection depends on your agent. The skill follows your application's test runner and isolation rules, and checks whether your installed GivenPy supports async setup with agiven before using it.

The Python library and agent skill are separate installations: pip install givenpy does not install the skill, and installing the skill does not install Python dependencies.

Documentation

from givenpy import given, when, then
from hamcrest import *


def magic_function(x, external_number):
    return x + 1 + external_number


def there_is_external_number():
    context.external_number = 1


def test_magic_function():
    with given([
        there_is_external_number,
    ]) as context:
        with when("I call the magic function"):
            result = magic_function(1, context.external_number)

        with then():
            assert_that(result, is_(equal_to(3)))

But I recommend using more flexible higher functiosn that can become configurable:

from givenpy import given, when, then
from hamcrest import *


def magic_function(x, external_number):
    return x + 1 + external_number


def there_is_external_number(number):
    def step(context):
        context.external_number = number

    return step


def test_magic_function():
    with given([
        there_is_external_number(5),
    ]) as context:
        with when("I call the magic function"):
            result = magic_function(1, context.external_number)

        with then("it should return 7"):
            assert_that(result, is_(equal_to(7)))

You can also set up cleanup steps that will be executed after the test is finished:

from dataclasses import dataclass

from givenpy import given, when, then, lambda_with
from hamcrest import *


@dataclass
class Database:
    connected: bool = False

    def connect(self):
        self.connected = True

    def disconnect(self):
        self.connected = False


def database_is_ready():
    def step(context):
        def open():
            context.database = Database()
            context.database.connect()

        def close():
            context.database.disconnect()

        return lambda_with(open, close)

    return step


def test_database_should_be_closed_when_we_exit_context():
    with given([
        database_is_ready(),
    ]) as context:
        with then("it should connect to the database"):
            assert_that(context.database.connected, is_(True))

    with then("when context closes, it should disconnect from the database"):
        assert_that(context.database.connected, is_(False))

Async setup and cleanup

Use agiven when a setup step must await work or own an asynchronous resource. Steps may be ordinary or async functions and may return sync or async context managers. Resources close in reverse order, including when setup or the test fails. The existing given, when and then blocks also work inside an async test when setup is synchronous.

from givenpy import agiven, async_lambda_with, when, then
from hamcrest import assert_that, equal_to


def a_connected_service():
    async def step(context):
        async def connect():
            context.service = await make_test_service()

        async def disconnect():
            await context.service.close()

        return async_lambda_with(connect, disconnect)

    return step


async def test_service_returns_a_result():
    async with agiven([a_connected_service()]) as context:
        with when("a request is sent"):
            result = await context.service.request()

        with then("the response is ready"):
            assert_that(result.status, equal_to("ready"))

Run the async test with an async-capable test runner, such as pytest with pytest-asyncio, or call it with asyncio.run from a synchronous test.

Guidelines

  • Always be explicit with your environment set up by adding all the necessary steps to the given block.
    • Make the setup steps as simple as possible. You can create master steps that will call other steps.
  • Always prefer higher-order functions over simple functions for setup steps. This will allow you to create more flexible steps by allowing to parametrize them later.
  • The test should always have given, when and then blocks.
  • The test name should describe a functional behavior and should give an overview of what's happening and what are the expectations.
    • For example: test_user_should_be_able_to_login or test_user_should_not_be_able_to_login_with_invalid_credentials
    • And not: test_login or test_invalid_credentials
  • The test should be readable and should not require any additional comments to understand what's happening.
  • There should be only one when block per test. If you need multiple blocks, then you probably need multiple tests.
  • The when block should be as simple as possible. It should only contain the code that is being tested. Test the code from the end-user perspective.
  • Use PyHamcrest to write assertions. It's much more readable than the default unittest assertions.
    • Do not be afraid to introduce custom matchers if needed.
    • Extract complex & nested matchers to behavior-named functions that return the final matcher.

More examples

This example is from one of the bigger projects where we use givenpy to test our API endpoints.

import logging

import ulid
from hamcrest import *
from starlette.testclient import TestClient

from app.organizations.repository import TeamRepository
from app.organizations.team.core import Team
from givenpy import given, when, then
from tests.integration.organization.test_feedback_submission import person_is_present
from tests.integration.steps_auth import auth_is_ready
from tests.integration.steps_database import database_repo_is_ready, database_is_clean
from tests.integration.steps_issues import there_is_organization
from tests.steps import prepare_api_server, create_test_client, prepare_injector

logging.basicConfig(level=logging.DEBUG)


def test_team_creation_for_the_organization_should_work():
    with given([
        prepare_injector(),
        database_repo_is_ready(),
        database_is_clean(),
        prepare_api_server(),
        there_is_organization(),
        create_test_client(),
        auth_is_ready(),
    ]) as context:
        client: TestClient = context.client
        organization_id: ulid.ULID = context.organization_id

        with when():
            payload = {
                "command_name": "CreateTeamCommand",
                "entity_type": "team",
                "payload": {
                    "name": "Test Team",
                    "organization_id": str(organization_id),
                    "members": [
                    ]
                }
            }

            response = client.post(
                f'/api/v1/organizations/{organization_id}/teams',
                headers=context.add_token(),
                json=payload
            )

        with then():
            assert_that(response.status_code, equal_to(200))

            team_repo: TeamRepository = context.injector.get(TeamRepository)
            team = team_repo.find_one(response.json()['id'])

            assert_that(team.name, equal_to("Test Team"))
            assert_that(team.organization_id, equal_to(organization_id))


def there_is_team(name="Test Team"):
    def step(context):
        team_repo: TeamRepository = context.injector.get(TeamRepository)
        team = team_repo.save(
            Team(
                name=name,
                organization_id=context.organization_id,
                id=ulid.new(),
            )
        )
        context.team_id = team.id

    return step


def test_i_should_be_able_to_add_the_person_to_a_team():
    with given([
        prepare_injector(),
        database_repo_is_ready(),
        database_is_clean(),
        prepare_api_server(),
        there_is_organization(),
        create_test_client(),
        person_is_present(),
        there_is_team("Test Team 1"),
        auth_is_ready(),
    ]) as context:
        client: TestClient = context.client
        organization_id: ulid.ULID = context.organization_id
        team_repo: TeamRepository = context.injector.get(TeamRepository)

        with when():
            payload = {
                "command_name": "AddPersonToTeamCommand",
                "entity_type": "team",
                "payload": {
                    "team_id": str(context.team_id),
                    "person_id": str(context.person_id),
                }
            }

            response = client.post(
                f'/api/v1/organizations/{organization_id}/teams/{context.team_id}',
                headers=context.add_token(),
                json=payload
            )

        with then():
            team = team_repo.find_one(context.team_id)

            assert_that(response.status_code, equal_to(200))
            assert_that(team.people, has_item(context.person_id))


def test_a_list_of_teams_for_the_current_organization_should_be_retrievable():
    with given([
        prepare_injector(),
        database_repo_is_ready(),
        database_is_clean(),
        prepare_api_server(),
        there_is_organization(),
        create_test_client(),
        person_is_present(),
        there_is_team(name="Test Team 1"),
        there_is_team("Test Team 1"),
        auth_is_ready(),
    ]) as context:
        client: TestClient = context.client
        organization_id: ulid.ULID = context.organization_id

        with when():
            response = client.get(
                f'/api/v1/organizations/{organization_id}/teams',
                headers=context.add_token(),
            )

        with then():
            assert_that(response.json(), has_length(2))

Contributing

Just create a PR or something. I'll review it and merge it if it's good.

Releasing a new version

git tag -a 1.0.2 -m "Tag 1.0.2"
git push 
git push origin --tags

Metadata

Release files for givenpy 1.1.0

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

Source distribution (sdist)

Source distribution for givenpy 1.1.0
File Size Uploaded Provenance Actions
givenpy-1.1.0.tar.gz 20.3 kB Without provenance Details for givenpy-1.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 27.0 kB

Release files / givenpy-1.1.0.tar.gz

Download URL givenpy-1.1.0.tar.gz
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0c1a485ca709f02a535a5d066a422abd5d9cb6fad620df55b4069ef3ef45bbf9
BLAKE2b-256 checksum
How to use checksums
bd0caab04cb98f5ad578114b3eba3d6f66cc99533985d7ba88891a4e9439d67b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","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 / givenpy-1.1.0-py3-none-any.whl

Download URL givenpy-1.1.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a592a538cbcb10ecead02b19b0dce3ede7a5164b8f018bd1278423bec402ebb2
BLAKE2b-256 checksum
How to use checksums
08f0d6f3ed5649b6f4e43ea55dd8c23283b9f380fa273964d89f7056f3e3de89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","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}

Security for givenpy 1.1.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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