virttest.unittest_utils package


virttest.unittest_utils.mock module

exception virttest.unittest_utils.mock.CheckPlaybackError

Bases: exceptions.Exception

Raised when mock playback does not match recorded calls.

class virttest.unittest_utils.mock.SaveDataAfterCloseStringIO(buf='')

Bases: StringIO.StringIO

Saves the contents in a final_data property when close() is called.

Useful as a mock output file object to test both that the file was closed and what was written.

final_data: Set to the StringIO’s getvalue() data when close() is
called. None if close() has not been called.
final_data = None
exception virttest.unittest_utils.mock.StubNotFoundError

Bases: exceptions.Exception

Raised when god is asked to unstub an attribute that was not stubbed

class virttest.unittest_utils.mock.anything_comparator

Bases: virttest.unittest_utils.mock.argument_comparator

class virttest.unittest_utils.mock.argument_comparator

Bases: object

class virttest.unittest_utils.mock.base_mapping(symbol, return_obj, *args, **dargs)

Bases: object

match(*args, **dargs)
class virttest.unittest_utils.mock.equality_comparator(value)

Bases: virttest.unittest_utils.mock.argument_comparator

class virttest.unittest_utils.mock.function_any_args_mapping(symbol, return_val, *args, **dargs)

Bases: virttest.unittest_utils.mock.function_mapping

A mock function mapping that doesn’t verify its arguments.

match(*args, **dargs)
class virttest.unittest_utils.mock.function_mapping(symbol, return_val, *args, **dargs)

Bases: virttest.unittest_utils.mock.base_mapping

class virttest.unittest_utils.mock.is_instance_comparator(cls)

Bases: virttest.unittest_utils.mock.argument_comparator

class virttest.unittest_utils.mock.is_string_comparator

Bases: virttest.unittest_utils.mock.argument_comparator

class virttest.unittest_utils.mock.mask_function(symbol, original_function, default_return_val=None, record=None, playback=None)

Bases: virttest.unittest_utils.mock.mock_function

run_original_function(*args, **dargs)
class virttest.unittest_utils.mock.mock_class(cls, name, default_ret_val=None, record=None, playback=None)

Bases: object

class virttest.unittest_utils.mock.mock_function(symbol, default_return_val=None, record=None, playback=None)

Bases: object


Like expect_call but don’t give a hoot what arguments are passed.

expect_call(*args, **dargs)
class virttest.unittest_utils.mock.mock_god(debug=False, fail_fast=True, ut=None)

Bases: object

With debug=True, all recorded method calls will be printed as they happen. With fail_fast=True, unexpected calls will immediately cause an exception to be raised. With False, they will be silently recorded and only reported when check_playback() is called.


Report any errors that were encounterd during calls to __method_playback().

create_mock_class(cls, name, default_ret_val=None)

Given something that defines a namespace cls (class, object, module), and a (hopefully unique) name, will create a mock_class object with that name and that possesses all the public attributes of cls. default_ret_val sets the default_ret_val on all methods of the cls mock.

create_mock_class_obj(cls, name, default_ret_val=None)
create_mock_function(symbol, default_return_val=None)

create a mock_function with name symbol and default return value of default_ret_val.


Mocks and saves the stdout & stderr output

mock_up(obj, name, default_ret_val=None)

Given an object (class instance or module) and a registration name, then replace all its methods with mock function objects (passing the original functions to the mock functions).

stub_class(namespace, symbol)
stub_class_method(cls, symbol)
stub_function(namespace, symbol)
stub_function_to_return(namespace, symbol, object_to_return)

Stub out a function with one that always returns a fixed value.

  • namespace – The namespace containing the function to stub out.
  • symbol – The attribute within the namespace to stub out.
  • object_to_return – The value that the stub should return whenever it is called.
stub_with(namespace, symbol, new_attribute)

Restores the stdout & stderr, and returns both output strings

unstub(namespace, symbol)
class virttest.unittest_utils.mock.regex_comparator(pattern, flags=0)

Bases: virttest.unittest_utils.mock.argument_comparator


Module contents