Skip to main content

Cloud Functions Testing Toolkit

Project description

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.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

barkus_func_test-0.0.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file barkus_func_test-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for barkus_func_test-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1adb313de9798ef1ade1ca34c33cb92136a6eaf79079c2926cf9536b11cab12d
MD5 4a27d0c6abcd759b91c89a7485e422f5
BLAKE2b-256 0e8409ecc8c267212aec7b327b1c909fba7b72350f092bad0c2c92ad3af9f5f9

See more details on using hashes here.

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