Skip to main content

Barkus Organization Python Cloud Functions Toolkit

Overview

This public Python package provides a set of utilities to streamline the testing of Cloud Functions within the Barkus Organization.

Installation

To use this package, you need to have Python installed. You can install the package using pip:

pipenv install barkus-func-test

API

1. BaseTestSuite

Base TestSuite class for class tests. Implement useful assertion methods and fixtures.

Example:

from barkus.functions.test import BaseTestSuite

class TestBaseTestSuite(BaseTestSuite):
	def before(self):
		self.values = ["batata"]

	def test_before_should_set_values_before_hand(self):
		self.assertEqual(self.values, ["batata"])

2. Patcher

Implements a wrapper around pytest.patcher API, adding better type-hint for modules.

Example: Assuming the python file we want to patch is located at src.foo.boo, and it has the following content:

def batata():
	return 2

def sut():
	return batata()
  • With function tests:
class Patcher(BasePatcher):
	__location__ = "src.foo.boo"
	def setup(self, ctx: PatcherCtx):
		super().setup(ctx) # If you're extending BasePatcher, this instruction is not necessary
		self.batata = self.patch("batata")

def test_should_properly_mock_module(mocker):
	mocks = Patcher(mocker)
	mocks.batata.return_value = 10

	result = sut()

	assert result == 10
  • With class tests: There's a helper class called UsePatcher that assists adding the patcher to the TestCase class
from barkus.functions.test import BaseTestSuite
from barkus.functions.test.patcher import BasePatcher, UsePatcher
from barkus.functions.test.patcher.context import PatcherCtx
from tests.patcher.foo import sut

class Patcher(BasePatcher):
	__location__ = "src.foo.boo"

	def setup(self, ctx: PatcherCtx):
		self.batata = self.patch("batata")

class TestPatcher(BaseTestSuite, UsePatcher[Patcher]):
	__patcher__ = Patcher

	def test_should_properly_mock_module(self):
		self.mocks.batata.return_value = 10

		result = sut()

		self.assertEqual(result, 10)

3. Matcher

Utilities functions to help perform assertions

3.1. Assert Reponse

Perform assertion on top of the function's response

Example:

from unittest.mock import Mock
from barkus.functions.test.patcher import BasePatcher
from barkus.functions.test.matchers import assert_response
from src.main import main

class Patcher(BasePatcher):
    __location__ = "src.main"

    def setup(self, ctx):
        self.perform: Mock = self.patch("perform")

def test_should_return_ok_on_success(mocker):
    Patcher(mocker)

    response = main(Mock())

    assert_response(response).toBe.ok()
    assert_response(response).notToBe.serverError()

Publishing New Version

To publish a new version you have to follow these steps:

1. You must make sure you have the dev-dependencies installed

pipenv install -d

2. You must configure ~/.pypirc file with credentials:

It should look something like this:

[distutils]
index-servers =
    pypi
    testpypi

[pypi]
username = __token__
password = pypi-*********

[testpypi]
username = __token__
password = pypi-*********

Note: the token is actually correct, it's not an example's placeholder

3. Update the package's version:

At setup.py, change the version to the desired value

4. Generate the build:

pipenv run build

5. Publish the package:

pipenv run publish

License

This toolkit is proprietary software developed by Barkus Organization. Unauthorized use, reproduction, or distribution is prohibited.

Metadata

Release files for barkus-func-test 0.0.1

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

Built distribution (wheel)

Table of built distributions (wheels) for barkus-func-test 0.0.1
File Interpreter ABI Platform
barkus_func_test-0.0.1-py3-none-any.whl Python 3 none any Details

Release files / barkus_func_test-0.0.1-py3-none-any.whl

Download URL barkus_func_test-0.0.1-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1adb313de9798ef1ade1ca34c33cb92136a6eaf79079c2926cf9536b11cab12d
BLAKE2b-256 checksum
How to use checksums
0e8409ecc8c267212aec7b327b1c909fba7b72350f092bad0c2c92ad3af9f5f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

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