Skip to content

Releases: kaste/mockito-python

2.0.0

10 Mar 08:01

Choose a tag to compare

Highlights

First-class async/await and property/descriptor support. Chaining. InOrder.
Enhanced captor, new patch_attr and patch_dict.

  • Deprecate verifyNoMoreInteractions in favor of ensureNoUnverifiedInteractions.

  • Deprecate verifyNoUnwantedInteractions in favor of verifyExpectedInteractions.

  • Context managers now check usage and any expectations (set via expect) on exit. The usage
    check can be disabled with the environment variable MOCKITO_CONTEXT_MANAGERS_CHECK_USAGE="0".

  • The between matcher now supports open ranges, e.g. between=(0,) to assert that at least
    0 interactions occurred.

  • Calling thenAnswer() without arguments is now allowed and is treated like
    thenReturn() without arguments: the stubbed method will return None.

  • Allow ... in fixed argument positions as an ad-hoc any matcher.
    Trailing positional ... keeps its existing "rest" semantics.

  • Expanded mock({...}) constructor shorthands. Refer https://mockito-python.readthedocs.io/en/latest/mock-shorthands.html

  • Added a first-class InOrder API via mockito.InOrder. The legacy in-order mode only supported one mock at a time;
    the new API supports true cross-mock order verification.

    Migration (old limited style -> new style)::

    # Before (legacy, single-mock order only)
    from mockito import inorder
    inorder.verify(cat).meow()
    inorder.verify(cat).purr()
    
    # Now (preferred, explicit cross-mock order)
    from mockito import InOrder
    in_order = InOrder(cat, dog)
    in_order.verify(cat).meow()
    in_order.verify(dog).bark()
    
  • The legacy in-order verification mode (inorder.verify(...))
    is deprecated in favor of InOrder(...).

  • Added first-class async/await stubbing support: async callables now preserve
    awaitable behavior for thenReturn, thenRaise, and thenAnswer (including
    sync and async answer callables), with parity across when, when2,
    patch, and expect.
    Note that async introspection metadata (e.g. inspect.iscoroutinefunction)
    for stub wrappers is currently implemented only on Python 3.12+.

  • Added first-class property/descriptor stubbing support, including class-level property
    stubbing via when(F).p.thenReturn(...) and thenCallOriginalImplementation() support for
    property stubs (including chained answers like
    thenReturn(...).thenCallOriginalImplementation()). Stubbing instance properties now fails
    fast with clear guidance to use class-level stubbing (when(F).p...).

  • Added chained stubbing and expectations across call/property hops, e.g.
    when(cat).meow().purr().thenReturn(...), when(User).query.filter_by(...).first(), and
    expect(cat, times=1).meow().purr(), including cleanup that preserves sibling chain branches.

  • Extend captor to be used as a rest matcher

    E.g., support

    args = captor()
    when(freud).says(*args).thenReturn("Yes.")
    assert freud.says("Are", "you", "dreaming?") == "Yes."
    assert args.value == ("Are", "you", "dreaming?")
    # or
    kwargs = captor()
    when(freud).does(**kwargs).thenReturn(False)
    
  • Added call_captor

    call = call_captor()
    when(mock).do(call).thenReturn("ok")
    mock.do(1, 2, x=3)
    assert call.value == ((1, 2), {"x": 3})
    
  • Added patch_attr and patch_dict for non-callable monkeypatch-style use cases
    (e.g. sys.stdout, sys.argv, and environment/config dictionaries) with
    context-manager support and restoration through unstub.

    E.g.

    patch_attr("sys.argv", ["foo", "bar"])
    with patch_attr("sys.stdout", StringIO()) as stdout: ...
    with patch_dict(os.environ, {"user": "bob"}): ...
    
  • Added explicit partial-unstub targeting by host + attribute name.
    This complements method-reference partial unstub (e.g. unstub(cat.meow))
    and supports tuple form and shorthand form, including multiple attributes
    in one call.

    E.g.

    unstub(cat.meow)
    unstub((cat, "meow"))
    unstub(cat, "meow")
    unstub((cat, "meow"), (os.path, "exists"))
    
  • Also implemented unstub("os.path")

2.0.0a6

06 Mar 22:41
68ada9c

Choose a tag to compare

2.0.0a6 Pre-release
Pre-release
  • Extend captor to be used as a rest matcher

    E.g., support

    args = captor()
    when(freud).says(*args).thenReturn("Yes.")
    assert freud.says("Are", "you", "dreaming?") == "Yes."
    assert args.value == ("Are", "you", "dreaming?")
    # or
    kwargs = captor()
    when(freud).does(**kwargs).thenReturn(False)
    
  • Added call_captor

    call = call_captor()
    when(mock).do(call).thenReturn("ok")
    mock.do(1, 2, x=3)
    assert call.value == ((1, 2), {"x": 3})
    
  • Added patch_attr and patch_dict for non-callable monkeypatch-style use cases
    (e.g. sys.stdout, sys.argv, and environment/config dictionaries) with
    context-manager support and restoration through unstub.

    E.g.

    patch_attr("sys.argv", ["foo", "bar"])
    with patch_attr("sys.stdout", StringIO()) as stdout: ...
    with patch_dict(os.environ, {"user": "bob"}): ...
    
  • Added explicit partial-unstub targeting by host + attribute name.
    This complements method-reference partial unstub (e.g. unstub(cat.meow))
    and supports tuple form and shorthand form, including multiple attributes
    in one call.

    E.g.

    unstub(cat.meow)
    unstub((cat, "meow"))
    unstub(cat, "meow")
    unstub((cat, "meow"), (os.path, "exists"))
    
  • Also implemented unstub("os.path")

2.0.0a5

04 Mar 10:02
0ec047d

Choose a tag to compare

2.0.0a5 Pre-release
Pre-release
  • Bug fixes and iteration on the chain feature now supporting deep property chaining as well.

  • Rollback specificity ordering from a4. Turns out that this less predictable then the last one takes it all rule.

2.0.0a4

24 Feb 19:47
ca5889b

Choose a tag to compare

2.0.0a4 Pre-release
Pre-release
  • Allow ... in fixed argument positions as an ad-hoc any matcher.
    Trailing positional ... keeps its existing "rest" semantics.

  • Expanded mock({...}) constructor shorthands. Refer https://mockito-python.readthedocs.io/en/latest/mock-shorthands.html

  • Added chained stubbing and expectations across call/property hops, e.g.
    when(cat).meow().purr().thenReturn(...), when(User).query.filter_by(...).first(), and
    expect(cat, times=1).meow().purr(), including cleanup that preserves sibling chain branches.

  • BREAKING: Stubs for the same method are now sorted by specificity. #110 Rolled back in a5.

    Same-ish are for example

    when(os.path).exists(...).thenCallOriginalImplementation()
    when(os.path).exists('.flake8').thenReturn(False)
    

2.0.0a3

21 Feb 16:56

Choose a tag to compare

2.0.0a3 Pre-release
Pre-release
  • Added first-class async/await stubbing support: async callables now preserve
    awaitable behavior for thenReturn, thenRaise, and thenAnswer (including
    sync and async answer callables), with parity across when, when2,
    patch, and expect.
    Note that async introspection metadata (e.g. inspect.iscoroutinefunction)
    for stub wrappers is currently implemented only on Python 3.12+.

  • Added first-class property/descriptor stubbing support, including class-level property
    stubbing via when(F).p.thenReturn(...) and thenCallOriginalImplementation() support for
    property stubs (including chained answers like
    thenReturn(...).thenCallOriginalImplementation()). Stubbing instance properties now fails
    fast with clear guidance to use class-level stubbing (when(F).p...).

2.0.0a2

21 Feb 16:56
d381ac5

Choose a tag to compare

2.0.0a2 Pre-release
Pre-release
  • Added a first-class InOrder API via mockito.InOrder (also available as
    mockito.inorder.InOrder). The legacy in-order mode only supported one mock at a time;
    the new API supports true cross-mock order verification.

    Migration (old limited style -> new style)::

    # Before (legacy, single-mock order only)
    from mockito import inorder
    inorder.verify(cat).meow()
    inorder.verify(cat).purr()
    
    # Now (preferred, explicit cross-mock order)
    from mockito import InOrder
    in_order = InOrder(cat, dog)
    in_order.verify(cat).meow()
    in_order.verify(dog).bark()
    
  • The legacy in-order verification mode (inorder.verify(...))
    is deprecated in favor of InOrder(...).

2.0.0a1

21 Feb 16:55

Choose a tag to compare

2.0.0a1 Pre-release
Pre-release
  • Calling thenAnswer() without arguments is now allowed and is treated like
    thenReturn() without arguments: the stubbed method will return None.
  • Deprecate verifyNoMoreInteractions in favor of ensureNoUnverifiedInteractions.
  • Deprecate verifyNoUnwantedInteractions in favor of verifyExpectedInteractions.
  • Context managers now check usage and any expectations (set via expect) on exit. The usage
    check can be disabled with the environment variable MOCKITO_CONTEXT_MANAGERS_CHECK_USAGE="0".
  • The between matcher now supports open ranges, e.g. between=(0,) to assert that at least
    0 interactions occurred.

1.5.4

22 Jan 22:16

Choose a tag to compare

Surprisingly deepcopy(mock()) returned None. Fix that! For dumb mocks, let deepcopy(m) return an actual deep copy of m. For strict mocks, let it throw if it is an unexpected usage.

You can quickly define mock({"__deepcopy__": None}) to expect the usage and get the default deepcopy algorithm. Note that expectations and recorded usages are shared by default even for copied mocks. You can fix that DIY, e.g when(copy).deepcopy(m).thenReturn(n). (Or the other way around: when(m).__deepcopy__(...).thenReturn(n).