cavy – testing helper library

Version Python Source Coverage Docs CI

This is a kitchen sink library of utility classes that simplifies writing richer tests. I extracted the contents from various projects that I’ve written over the past few years – this is the result of not wanting to cut and paste or rewrite the same code everywhere.

Reference

class cavy.testing.AdditionalAssertionsMixin(methodName='runTest')

Useful assertions that aren’t in the Standard Library.

This mix-in includes some assertions that I’ve found myself wishing were part of the Standard Library from time to time.

assert_between(value, low, high, msg=None)

Assert that value is between low and high.

Parameters
  • value – value to compare

  • low – inclusive low range endpoint

  • high – inclusive high range endpoint

  • msg (str) – optional message to use on failure

assert_endswith(value, suffix, msg=None)

Assert that suffix is a suffix of value.

Parameters
  • value – value to check

  • suffix – sequence of values that value should end with

  • msg (str) – optional message to use on failure

Note that value and suffix ARE NOT required to be of the same type. They are required to be reversible sequences of the same underlying type.

assert_startswith(value, prefix, msg=None)

Assert that prefix is a prefix of value.

Parameters
  • value – value to check

  • prefix – sequence of values that value should start with.

  • msg (str) – optional message to use on failure

Note that value and prefix ARE NOT required to be of the same type. The only requirement is that they are ordered sequences of the same underlying type.

class cavy.testing.EnvironmentMixin(methodName='runTest')

Adds safe environment variable manipulation.

Use this instead of manipulating os.environ or calling os.setenv() directly. Failing to do so may cause strange test failures when environment variables leak between test cases.

reset_environment()

Undo changes to the environment.

setUp()

Hook method for setting up the test fixture before exercising it.

setenv(name, value)

Set an environment variable.

Parameters
  • name (str) – names the environment variable to set

  • value (str) – value to store in the environment

tearDown()

Automatically reset the environment during test clenaup.

unsetenv(name)

Clear an environment variable.

Parameters

name (str) – names the environment variable to clear

class cavy.testing.PEP8NamingMixin(methodName='runTest')

PEP8 compliant names for assertions.

Mix this class in over unittest.TestCase to provide PEP 8 compliant names for the assertions. This makes it possible to write tests that look like:

class MyTest(PEP8AssertionsMixin, unittest.TestCase):
    def test_something(self):
        self.assert_equal(1, 2)

instead of:

class MyTest(PEP8AssertionsMixin, unittest.TestCase):
    def test_something(self):
        self.assertEqual(1, 2)

It is a small thing but the non-PEP8 names in unittest have always bothered me.

assert_almost_equal(first, second, places=None, msg=None, delta=None)
assert_count_equal(first, second, msg=None)
assert_dict_equal(d1, d2, msg=None)
assert_equal(first, second, msg=None)
assert_false(expr, msg=None)
assert_greater(a, b, msg=None)
assert_greater_equal(a, b, msg=None)
assert_in(member, container, msg=None)
assert_is(expr1, expr2, msg=None)
assert_is_instance(obj, cls, msg=None)
assert_is_none(obj, msg=None)
assert_is_not(expr1, expr2, msg=None)
assert_is_not_none(obj, msg=None)
assert_less(a, b, msg=None)
assert_less_equal(a, b, msg=None)
assert_list_equal(list1, list2, msg=None)
assert_logs(logger=None, level=None)
assert_multi_line_equal(first, second, msg=None)
assert_not_almost_equal(first, second, places=None, msg=None, delta=None)
assert_not_equal(first, second, msg=None)
assert_not_in(member, container, msg=None)
assert_not_is_instance(obj, cls, msg=None)
assert_not_regex(text, unexpected_regex, msg=None)
assert_raises(expected_exception, *args, **kwargs)
assert_raises_regex(expected_exception, expected_regex, *args, **kwargs)
assert_regex(text, expected_regex, msg=None)
assert_sequence_equal(seq1, seq2, msg=None, seq_type=None)
assert_set_equal(set1, set2, msg=None)
assert_true(expr, msg=None)
assert_tuple_equal(tuple1, tuple2, msg=None)
assert_warns(expected_warning, *args, **kwargs)
assert_warns_regex(expected_warning, expected_regex, *args, **kwargs)

Release History

0.1.0 (6 Jul 2019)

0.0.0 (6 Jul 2019)

  • Initial name squatting release. Nothing to see here.