Fluent interface facade for Michael Foord's mock.


License
Apache-2.0
Install
pip install fluentmock==0.3.3

Documentation

fluentmock

Build Status

Fluent interface facade for Michael Foord's Mock.

  • Easy and readable configuration of mock side effects.
  • Configuration and verification using matchers.

A example test using fluentmock and hamcrest:

from fluentmock import UnitTests, when, verify
from hamcrest import assert_that, equal_to


class SeveralAnswersTests(UnitTests):
  def test_should_return_configured_values_in_given_order(self):

    when(targetpackage).targetfunction(2).then_return(1).then_return(2).then_return(3)

    assert_that(targetpackage.targetfunction(2), equal_to(1))
    assert_that(targetpackage.targetfunction(2), equal_to(2))
    assert_that(targetpackage.targetfunction(2), equal_to(3))

    verify(targetpackage).targetfunction(2)

Matchers

fluentmock offers a lot of matchers. You can use then in mock configuration or in verification.

    def test_matching_any_integer_value(self):

        when(targetpackage).targetfunction(any_value_of_type(int)).then_return('argument was an integer')
        assert_that(targetpackage.targetfunction(13), equal_to('argument was an integer'))
        assert_that(targetpackage.targetfunction(42), equal_to('argument was an integer'))

    def test_matching_any_string_value(self):

        when(targetpackage).targetfunction(any_value_of_type(str)).then_return('argument was a string')
        assert_that(targetpackage.targetfunction('Hello'), equal_to('argument was a string'))
        assert_that(targetpackage.targetfunction('World'), equal_to('argument was a string'))

If you prefer to use constants instead of convenience functions fluentmock comes with these constants for matching:

ANY_BOOLEAN
ANY_DICTIONARY
ANY_FLOAT
ANY_INTEGER
ANY_LIST
ANY_SLICE
ANY_STRING
ANY_TUPLE
ANY_VALUE
ANY_VALUES

Requirements

If you are working on Python 2.6 please install Brett Cannon's importlib:

easy_install importlib

or

pip install importlib

Documentation