Skip to main content

tms: Test Match Special

tms lets you test values in test code, where you may not be able to test against an exact, known value.

Some quick examples:

Test an object’s class and instance attributes:

assert zoo.get('elephant') == tms.InstanceOf(Elephant, name='nelly')

Match data structures:

assert resultset.any() == {'id': tms.InstanceOf(int),
                           'name': tms.Contains('bob'),
                           'data': tms.Anything()}

Common types have shortcuts, eg:

tms.Bool()
tms.Str()
tms.Unicode()
tms.Bytes()
tms.Int()
tms.Float()

Test for in / not in:

assert 'business' == tms.Contains('sin')
assert 'my lunch' == tms.DoesntContain('nuts')

Test attributes:

assert Circle(1) == tms.InstanceOf(Circle, radius=1)

assert Circle(1) == tms.InstanceOf(Circle, radius=tms.InstanceOf(int))

assert Circle(1) == tms.InstanceOf(Circle, has_attrs=['radius'])

Test for arbitrary conditions:

assert 1 == tms.Int(lambda x: x > 0)
assert Circle(1) == tms.InstanceOf(Circle, lambda x: x.radius == 1)

Test dictionaries or dict-like objects:

# Check that it contains keys and values
assert {'x': 1} == tms.DictLike(x=1)

# Check that it contains certain keys
assert dict(x=1, y=2) == tms.Contains('x', 'y')

# Check that it doesn't contain certain keys
assert dict(x=1, y=2) == tms.DoesntContain('foo', 'bar')

Combining tests:

assert mylunch == tms.InstanceOf(Sandwich) & tms.DoesntContain(cheese)

Unlike hamcrest, tms uses the __eq__ method to evaluate comparisons. This means it works with python’s built in assert statement. This also means it works well with other test libraries: you can drop a tms.Matcher object into any regular equality test and have it work.

Note that this also means that it may not work for objects that override the __eq__ method. If the matcher doesn’t seem to be firing, try putting it on the left hand side of the comparison:

>>> import tms
>>> class MySpecialObject(object):
...     def __eq__(self, other):
...         return False
...
>>> MySpecialObject() == tms.Anything()
False
>>> tms.Anything() == MySpecialObject()
True

CHANGELOG

0.1.2 (released 2015-09-01)

  • Added shortcuts for common builtin types, so you can now write tms.Int() instead of tms.InstanceOf(int).

  • Added tms.Passes. This takes an arbitrary function that is expected to return a boolean, eg assert random.randrange(1, 3) == tms.Passes(lambda x: 1 <= x < 3). This is integrated with tms.InstanceOf and tms.Anything, so you can write eg tms.InstanceOf(MyClass, lambda x: x.name.startswith('foo')).

0.1.1

  • Initial release

Metadata

Release files for tms 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tms 0.1.2
File Size Uploaded
tms-0.1.2.tar.gz 4.6 kB Details

Release files / tms-0.1.2.tar.gz

Download URL tms-0.1.2.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ab53400a0266f93feb78cc4fdf0edf2e7720797caa6cc4f521453f54243bc40c
BLAKE2b-256 checksum
How to use checksums
1bf98da51becccbf8992192070e5fb2c0692693b3ab147bc5f7c9dc2a0746a9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

1 release file

0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page