Skip to content

2.0.0

Latest

Choose a tag to compare

@kaste kaste released this 10 Mar 08:01
· 27 commits to master since this release

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")