Skip to main content

approximations for equality testing

Project description

__eq__ overloading for simpler approximate equality testing


pip install roughly


Testing a function that returns usually looks like this:

import arrow

def function_under_test():

def test_function_returns_now():
    now =
    actual = function_under_test()
    assert now.replace(seconds=-2) <= actual <= now.replace(seconds=2)

It gets worse when you want to check two objects that have a datetime field that should be approximately equal, but you still want the class’s __eq__ to check exact equality. There’s a better way:

import arrow
import roughly

def test_function_returns_now():
    now = roughly.near(, seconds=2)
    assert function_under_test() == now


Approximate equality should be handled carefully. You can introduce subtle errors when two dates are close by one part of the system, but not close in another.

Most frequently in testing, however, we’d really like to use the existing equality-based tests for objects that have datetime attributes, without patching the system that vends datetimes.

Consider a test that stores an object in a mock database, updating an item with the current time. We’d really like to use the helpers provided by unittest.mock like assert_called_once_with but that requires us to use exact values. We need something that looks like equality to another system.

Without roughly, here’s the workaround to ensure a date is nearby. Note that all of the other arguments could have been checked with mock’s assert calls:

assert == 1
(item, *_), kwargs =
assert item is key
assert kwargs["atomic"] is True

# :( One of the the fields in this object is a datetime, so we can't
# do an exact match.  We could mock but that's really an
# implementation detail that we shouldn't need to know.
condition = Key.until >=
assert kwargs["condition"].column is condition.column
assert kwargs["condition"].value >= condition.value

Here’s exactly the same check, but with a roughly.near datetime for the condition (the condition being tested is a bloop ConditionalExpression):

condition = Key.until >= near(, seconds=5), atomic=True, condition=condition)

The approximate parts of the object are injected into the arguments we expect, and when the unittest.mock machinery performs the comparison self._call_matcher((args, kwargs)) == self._call_matcher(self.call_args) and iterates through the (args, kwargs) tuples, it will compare the condition object with our approximate datetime to the actual condition with a real datetime. Inside that class’s __eq__, it will check self.value == other.value which holds for our ApproximateArrow compared to the actual Arrow.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for roughly, version 0.0.6
Filename, size File type Python version Upload date Hashes
Filename, size roughly-0.0.6.tar.gz (4.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page