Skip to main content

image image image Actions status


Documentation: https://maxcode123.github.io/unittest-extensions/
Source code: https://github.com/Maxcode123/unittest-extensions
PyPI: https://pypi.org/project/unittest-extensions/


unittest-extensions

Extension of Python's standard unittest library

Introduction

If testing is not easy, you will not do it.
If you do not test, bad things will happen.
Thus, if testing is not easy, bad things will happen.

This minimal library aims to simplify behavioural testing with Python's standard unittest library by separating object and data creation from behaviour assertion. Furthermore, it is intended to serve users that want to write really small test functions where what is being asserted is quickly comprehended and easily visible.

unittest-extensions does not have any dependencies, it is solely based on the Python standard library and mainly inspired by Ruby's RSpec framework.

Installation

pip install unittest-extensions

Usage

Suppose you have some code that looks like this:

from dataclasses import dataclass

@dataclass
class User:
    name: str
    surname: str

    def is_relative_to(self, user: "User") -> bool:
        return self.surname.casefold() == user.surname.casefold()

This is a dummy example, meaning that how exactly the User and their methods are implemented does not really matter; what we actually care about here is how to test this code given the above implementation.
Say we'd like to test the is_relative_to method with pairs of User names and surnames using the standard unittest library. So, here's an example of how we could do that as simply as possible:

std unittest

from unittest import main, TestCase


class TestIsRelativeToSameName(TestCase):
    def test_same_name(self):
        user1 = User("Niklas", "Strindberg")
        user2 = User("Niklas", "Ibsen")
        self.assertFalse(user1.is_relative_to(user2))

    def test_same_empty_name(self):
        user1 = User("", "Stringberg")
        user2 = User("", "Ibsen")
        self.assertFalse(user1.is_relative_to(user2))


class TestIsRelativeToSameSurname(TestCase):
    def test_same_surname(self):
        user1 = User("August", "Nietzsche")
        user2 = User("Henrik", "Nietzsche")
        self.assertTrue(user1.is_relative_to(user2))

    def test_same_empty_surname(self):
        user1 = User("August", "")
        user2 = User("Henrik", "")
        self.assertTrue(user1.is_relative_to(user2))

    def test_same_surname_case_sensitive(self):
        user1 = User("August", "NiEtZsChE")
        user2 = User("Henrik", "nietzsche")
        self.assertTrue(user1.is_relative_to(user2))

    def test_surname1_contains_surname2(self):
        user1 = User("August", "Solzenietzsche")
        user2 = User("Henrik", "Nietzsche")
        self.assertFalse(user1.is_relative_to(user2))


if __name__ == "__main__":
    main()

The cases we check here are rather limited but still there is some duplication in our code; we use many lines to create our User subjects. Of course we can avoid that by creating custom assertion methods that receive only the parameters that change between tests, but that's why a testing library is here for.
Here's how we could write the above code with unittest-extensions:

unittest-extensions

from unittest import main

from unittest_extensions import TestCase, args


class TestIsRelativeToSameName(TestCase):
    def subject(self, name1, name2):
        return User(name1, "Strindberg").is_relative_to(User(name2, "Ibsen"))

    @args("Niklas", "Niklas")
    def test_same_name(self):
        self.assertResultFalse()

    @args(name1="", name2="")
    def test_same_empty_name(self):
        self.assertResultFalse()


class TestIsRelativeToSameSurname(TestCase):
    def subject(self, surname1, surname2):
        return User("August", surname1).is_relative_to(User("Henrik", surname2))

    @args("Nietzsche", "Nietzsche")
    def test_same_surname(self):
        self.assertResultTrue()

    @args(surname1="", surname2="")
    def test_same_empty_surname(self):
        self.assertResultTrue()

    @args("NiEtZsChE", surname2="Nietzsche")
    def test_same_surname_case_sensitive(self):
        self.assertResultTrue()

    @args("Nietzsche", "Solszenietzsche")
    def test_surname2_contains_surname1(self):
        self.assertResultFalse()


if __name__ == "__main__":
    main()

The number of lines is still the same, but the testing code has become clearer:

  1. The subject of our assertions in each test case is documented in the subject method
  2. Each test method contains only information we care about, i.e. the input data (names/surnames) we test and the result of the assertion (true/false).

Release files for unittest-extensions 0.4.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 unittest-extensions 0.4.0
File Size Uploaded
unittest_extensions-0.4.0.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unittest-extensions 0.4.0
File Interpreter ABI Platform
unittest_extensions-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size:16.1 kB

Release files / unittest_extensions-0.4.0.tar.gz

Download URL unittest_extensions-0.4.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9cb166717bc39d9465b44a54b8f9c7c0a1027d169052b3b0c99abdb39fdd64f7
BLAKE2b-256 checksum
How to use checksums
09e8145f399a57d05a8c6af7f019e487a3d7fe0c99ef4a53a30470bd1f3cf55e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / unittest_extensions-0.4.0-py3-none-any.whl

Download URL unittest_extensions-0.4.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fa1479dde0441dc8057f4b989e8f2048a31ff81094b3460407bbfd311369ebbc
BLAKE2b-256 checksum
How to use checksums
0f7393fdbad6cb4afb2cff81abcd77536dfe99cd161678a41a366fc7e02e7d2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

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