API Reference

ClassTester

class fluenttest.ClassTester(cls)

Assert that a class matches certain conditions.

Parameters:cls (type) – the class under test

This class provides a readable and efficient method for asserting that a class implements a particular interface. Instead of specifying the interface using abstract base classes and the like, this test specifies the interface as a set of operations, parent classes, and class attributes.

>>> probe = ClassTester(dict)
>>> probe.implements_method('keys')
True
>>> probe.has_attribute('keys')
True
>>> probe.has_attribute('__class__')
True

Each assertion is implemented as a separate method that returns True or False. This is test runner agnostic so you can use the assertions with nose, unittest, or py.test.

has_attribute(name, typed=None, value=None)

Does the class have an attribute named name?

Parameters:
  • name (str) – the name of the attribute to probe
  • typed (type) – require the attribute to be an instance of this type
  • value – require the attribute to have this specific value

This method does not inspect the attributes of instances of the class. It only examines the named attributes of the class itself.

implements_method(name)

Does the class implement a method named name?

is_subclass_of(parent_class)

Is the class a subclass of parent_class?

Parameters:parent_class – a dotted class name or anything acceptable to lookup_class().

TestCase

class fluenttest.TestCase

Arrange, Act, Assert test case.

Sub-classes implement test cases by arranging the environment in the arrange() class method, perform the action in the act() class method, and implement assertions as test methods. The individual assertion methods have to be written in such a way that the test runner in use finds them.

allowed_exceptions

The exception or list of exceptions that the test case is interested in capturing. An exception raised from act() will be stored in exception.

exception

The exception that was thrown during the action or None.

classmethod act()

The action to test.

Subclasses are required to replace this method.

allowed_exceptions = ()

Catch this set of exception classes.

classmethod arrange()

Arrange the testing environment.

Concrete test classes will probably override this method and should invoke this implementation via super().

classmethod destroy()

Perform post-test cleanup.

Concrete tests classes may override this method if there are actions that need to be performed after act() is called. Subclasses should invoke this implementation via super().

This method is guaranteed to be called after the action under test is invoked and before teardown_class(). It will be called after any captured exception has been caught.

classmethod patch(target, **kwargs)

Patch a named class or method.

Parameters:target (str) – the dotted-name to patch
Returns:the result of starting the patch.

This method calls mock.patch() with target and **kwargs, saves the result, and returns the running patch.

classmethod patch_instance(target, **kwargs)

Patch a named class and return the created instance.

Parameters:target (str) – the dotted-name of the class to patch
Returns:tuple of (patched class, patched instance)

This method calls patch() with **kwargs to patch target and returns a tuple containing the patched class as well as the return_value attribute of the patched class. This is useful if you want to patch a class and manipulate the result of the code under test creating an instance of the class.

classmethod setup_class()

Arrange the environment and perform the action.

This method ensures that arrange() and act() are invoked exactly once before the assertions are fired. If you do find the need to extend this method, you should call this implementation as the last statement in your extension method as it will perform the action under test when it is called.

classmethod teardown_class()

Stop any patches that have been created.

Helpers Functions

the_class

fluenttest.the_class(cls, matcher_class=None)

Return a ClassTester instance for cls.

Parameters:
  • cls – the class instance to inspect.
  • matcher_class (type) – the type to instantiate, defaults to ClassTester

lookup_class

fluenttest.lookup_class(target)

Find a class named target.

Parameters:target (str) – the dot-separated name of the class to find, a class instance, or a type instance.
Returns:a class or type() instance

Table Of Contents

Previous topic

Unit Testing

Next topic

Change Log

This Page