Skip to main content

A simple wrapper around the unittest package with different styles for specs

Project description

unittest_specs

SimpleFlatSpec

Usage

The SimpleFlatSpec class provides an easy and readable way to write tests. Much like in unittest itself, each test case should be specified in a separate test function with a name outlining the expected behaviour. In the function body, any required setup can be performed, before calling self.expect() to provide the computed actual value. This declaration should be followed by an assertion, of which there is a range to choose from (see the following section for a complete list).

A very simple test case could look something like this:

from unittest_specs import SimpleFlatSpec


class MyTest(SimpleFlatSpec):
    def test_should_identify_even_number(self):
        is_even_number = 4 % 2 == 0
        
        self.expect(is_even_number).to_be_true()

Available Assertions

Assertion Function Description Equivalent unittest function
to_be() checks for equality between actual and expected value assertEqual()
to_not_be() checks for inequality between actual and expected value assertNotEqual()
to_be_of_type() checks whether the actual value is of a specified type assertIsInstance()
to_equal_list() checks an actual and expected list for equality assertListEqual()
to_contain() checks whether the actual collection contains an expected value assertTrue(expected_element in self._actual_value)
to_contain_all() checks whether the actual collection contains all expected values custom implementation
to_be_true() checks whether the actual value is of value True assertTrue()
to_be_false() checks whether the actual value is of value False assertFalse()
to_be_none() checks whether the actual value is None assertIsNone()
to_not_be_none() checks whether the actual value is not None assertIsNotNone()
to_be_a_list() checks whether the actual value is of type list assertIsInstance(actual_value, list)
to_be_a_dict() checks whether the actual value is of type dict assertIsInstance(actual_value, dict)
to_be_a_set() checks whether the actual value is of type set assertIsInstance(actual_value, set)
to_raise() intercepts an Exception expected to be raised in the test function assertRaises()
to_be_of_length() checks for an expected length of an object supporting len() assertEqual(len(actual_value), expected_value)

Parameterizing Tests

Instead of writing several test function covering the same functionality with different parameters, one can use parameterization to provide an arbitrarily long list of test data sets. This is implemented using the unittest.TestCase.subTest function, with the first element in each being used as the label.

A parameterized test might look like the following:

from unittest_specs import SimpleFlatSpec, parameterized


class MyTest(SimpleFlatSpec):
    
    @parameterized(params=[
        ('hello',  5),
        ('world!', 6),
    ])
    def test_should_check_string_length(self, given_string, expected_length):
        self.expect(given_string).to_be_of_length(expected_length)

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

unittest-specs-1.0.2.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unittest_specs-1.0.2-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file unittest-specs-1.0.2.tar.gz.

File metadata

  • Download URL: unittest-specs-1.0.2.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for unittest-specs-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c3789cb34222032bbf259a68effa8ef090245d299a2bb9c878105b6a23315649
MD5 3dbe3f600c387844381091bb8e5b9d89
BLAKE2b-256 5fe4778b7c847043c7a61d3ea5552441a0a8a1c586959d1caf7d25c8eebd8a47

See more details on using hashes here.

File details

Details for the file unittest_specs-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: unittest_specs-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for unittest_specs-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ca5f6fb0a8b1b035f65a1ad050f71fa3cd633baa56fc62c94c9f6dc3ecbb4785
MD5 8ead8d9026e325099f6c26550ec5b782
BLAKE2b-256 5dec6fd3cfc6fe37c967203ca8e7ccd08bbcf3cbe96094f5910b0414d39d9344

See more details on using hashes here.

Supported by

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