Operator matchers

Comparisons

These matchers use Python’s relational operators: <, >=, etc.

class argmatch.operators.Less(*args, **kwargs)[source]

Matches values that are smaller (as per < operator) than given object.

Accepts a single argument: the reference object to compare against.

It can be passed either as a single positional parameter, or as a single keyword argument – preferably with a readable name, for example:

some_mock.assert_called_with(Number() & LessOrEqual(to=42))
argmatch.operators.LessThan

alias of Less

argmatch.operators.Lt

alias of Less

class argmatch.operators.LessOrEqual(*args, **kwargs)[source]

Matches values that are smaller than, or equal to (as per <= operator), given object.

Accepts a single argument: the reference object to compare against.

It can be passed either as a single positional parameter, or as a single keyword argument – preferably with a readable name, for example:

some_mock.assert_called_with(Number() & LessOrEqual(to=42))
argmatch.operators.LessOrEqualTo

alias of LessOrEqual

argmatch.operators.Le

alias of LessOrEqual

class argmatch.operators.Greater(*args, **kwargs)[source]

Matches values that are greater (as per > operator) than given object.

Accepts a single argument: the reference object to compare against.

It can be passed either as a single positional parameter, or as a single keyword argument – preferably with a readable name, for example:

some_mock.assert_called_with(Number() & LessOrEqual(to=42))
argmatch.operators.GreaterThan

alias of Greater

argmatch.operators.Gt

alias of Greater

class argmatch.operators.GreaterOrEqual(*args, **kwargs)[source]

Matches values that are greater than, or equal to (as per >= operator), given object.

Accepts a single argument: the reference object to compare against.

It can be passed either as a single positional parameter, or as a single keyword argument – preferably with a readable name, for example:

some_mock.assert_called_with(Number() & LessOrEqual(to=42))
argmatch.operators.GreaterOrEqualTo

alias of GreaterOrEqual

argmatch.operators.Ge

alias of GreaterOrEqual

By length

In addition to simple comparison matchers described, argmatch offers a set of dedicated matchers for asserting on object’s length. You can use them in conjunction with any Python Sequence: a string, list, collections.deque, and so on.

class argmatch.operators.Shorter(*args, **kwargs)[source]

Matches values that are shorter (as per < comparison on len) than given value.

Accepts a single argument: the reference object to compare against.

It can be passed either as a single positional parameter, or as a single keyword argument – preferably with a readable name, for example:

some_mock.assert_called_with(Number() & LessOrEqual(to=42))
argmatch.operators.ShorterThan

alias of Shorter

class argmatch.operators.ShorterOrEqual(*args, **kwargs)[source]

Matches values that are shorter than, or equal in length to (as per <= operator), given object.

Accepts a single argument: the reference object to compare against.

It can be passed either as a single positional parameter, or as a single keyword argument – preferably with a readable name, for example:

some_mock.assert_called_with(Number() & LessOrEqual(to=42))
argmatch.operators.ShorterOrEqualTo

alias of ShorterOrEqual

class argmatch.operators.Longer(*args, **kwargs)[source]

Matches values that are longer (as per > comparison on len) than given value.

Accepts a single argument: the reference object to compare against.

It can be passed either as a single positional parameter, or as a single keyword argument – preferably with a readable name, for example:

some_mock.assert_called_with(Number() & LessOrEqual(to=42))
argmatch.operators.LongerThan

alias of Longer

class argmatch.operators.LongerOrEqual(*args, **kwargs)[source]

Matches values that are longer than, or equal in length to (as per >= operator), given object.

Accepts a single argument: the reference object to compare against.

It can be passed either as a single positional parameter, or as a single keyword argument – preferably with a readable name, for example:

some_mock.assert_called_with(Number() & LessOrEqual(to=42))
argmatch.operators.LongerOrEqualTo

alias of LongerOrEqual

Memberships

class argmatch.operators.Contains(value)[source]

Matches values that contain (as per the in operator) given reference object.

class argmatch.operators.In(container)[source]

Matches values that are within the reference object (as per the in operator).

Identity

class argmatch.operators.Is(value)[source]

Matches a value using the identity (is) operator.

class argmatch.operators.IsNot(value)[source]

Matches a value using the negated identity (is not) operator.

Equality

Note

You will most likely never use the following matcher, but it’s included for completeness.

class argmatch.operators.Eq(value)[source]

Matches a value exactly using the equality (==) operator.

This is already the default mode of operation for assert_called_with methods on mocks, making this matcher redundant in most situations:

mock_foo.assert_called_with(bar)
mock_foo.assert_called_with(Eq(bar))  # equivalent

In very rare and specialized cases, however, if the tested code treats argmatch matcher objects in some special way, using Eq may be necessary.

Those situations shouldn’t generally arise outside of writing tests for code that is itself a test library or helper.

Parameters:

value – Value to match against