Skip to main content

A simple micro BDD framework for Python

Project description

GivenPy

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

Example

import unittest
from typing import Dict

from hamcrest import *
from starlette.testclient import TestClient

from tests.givenpy import given, when, then
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')))

or simpler

import unittest
from typing import Dict

from hamcrest import *
from starlette.testclient import TestClient

from tests.givenpy import given, when, then
from tests.steps import prepare_api_server, create_test_client, prepare_injector


def test_health_works():
    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

Documentation

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:

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)))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

givenpy-1.0.0.tar.gz (5.8 kB view hashes)

Uploaded Source

Built Distribution

givenpy-1.0.0-py3-none-any.whl (6.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page