plainbox.testing_utils.testcases – additional TestCase classes

Implementation of additional TestCase classes that aid in testing.

class plainbox.testing_utils.testcases.TestCaseParameters(names, values)[source]

Class for holding test case parameters

Instances of this class are provided as the .parameters attribute to instances of TestCaseWithParameters during run().

The class supports attribute lookup so tests can be written in convenient form. Having a test case with parameters ‘foo’ and ‘bar’ they can be accessed as self.parameters.foo and self.parameters.bar respectively.

class plainbox.testing_utils.testcases.TestCaseWithParameters(methodName='runTest', parameters=None)[source]

TestCase parametrized by any number of named parameters.

This class can save a lot of typing or creating dummy base classes and other implementation tricks that aim to reduce duplication in code that tests multiple values of a parameter.

For all intents and purposes this is just a standard TestClass instance, what makes it different are the few tricks employed to make it appear as a collection of test cases instead.

Each TestCase created from the test_** methods will be invoked for all the parameter values. Any number of parameters that can be provided. A tuple of values must be provided for each parameter.

In simple cases the developer only needs to provide two class attributes, parameter_names and parameter_values. In more complex cases behavior can be customized by defining two class methods, get_parameter_names() and get_parameter_values(). Note that you _must_ define a non-empty list of parameter values, otherwise this test will behave as if it never existed (analogous how multiplication by zero works).

Note

Technical note for tinkerers and subclass authors. Python unittest framework is pretty annoying to work with or extend. In practice you should always keep the source code (of a particular python version) open to reason about it.

Parametrization is implemented by creating additional instances of TestCaseWithParameters with immutable, bound, parameters. That logic is implemented in run(). The multiplication of TestCase instances happens in _parametrize(). If your sub-class needs to do something special there you might need to override it.

Ideally the unittest framework could allow customizing discovery in a standard way. If that were true then we could instantiate parametrized copies early and then let the normal run() mechanics work. Sadly this is not the case.

Most special python methods also had to be overridden to take account of the new _parameters instance attribute. This includes __str__(), __repr__(), __eq__() and __hash__().

Additional methods unique to unittest framework were implemented to convey the value of the parameter back to the user / developer. Those include id() and countTestCases()

countTestCases()[source]

Overridden method of unittest.TestCase()

Behaves different depending on whether it is called on the original instance or the parametrized instance. The original instance behaves like a TestSuite, returning the number of parameter values.

Each parametrized instance (created with _parametrize()) returns 1

classmethod get_parameter_names()[source]

Return a tuple of parameters that affect this test case.

classmethod get_parameter_values()[source]

Return a tuple of tuple of values that should be mapped to subsequent attributes named, as returned by get_parameter_names()

id()[source]

Overridden version of TestCase.id()

This is an internal implementation detail of TestCase, it is used in certain places instead of __str__(). It behaves very similar to what __str__() does, except that it displays the class name differently

parameter_names = ()
parameter_values = ()
parameters[source]

Return the value of parameters that specialize this test case

Normal instances always return None here, the special, parametrized, instance that is constructed once for each parameter value during run() (where testing actually happens), returns the real value.

run(result=None)[source]

Overridden version of TestCase.run()

Creates additional instances of the class being tested, one for each value returned by get_parameter_values() by calling _parametrize().

Each of the parametrized instances is tested normally.

plainbox.testing_utils.testcases.load_tests(loader, suite, pattern)[source]

Previous topic

plainbox.testing_utils.resource

Next topic

plainbox.vendor – vendorized packages

This Page