Property based testing against mocks?

More of a quick splurge than anything – I was looking over some old code and noticed a unit test that creates a lazy list of tests, each of which asserts some properties about a bisection based search over a set of stub objects which are all based on an existing superclass and have a property defined which returns a randomised value each time you interact with it. Unusual but kind of cool. Here’s the code…

    subject=integers(low=0, high=1010))
def check_detect_using_bisect(searchbase, subject):
  searchbase = list(searchbase)
  item = searchbase[max(0, min(subject, len(searchbase) -1))]
  universe = sorted(searchbase, key=salsa_key)
  search_result = detect(cmp, item, universe, key=salsa_key)
  reason = "item (%i) mapped_to (%i)" % (universe.index(item), universe.index(search_result))
  assert_that(search_result, is_(not_none()), reason)
  assert_that(search_result.salsaId, is_(equal_to(item.salsaId)), reason)

def test_detect_using_bisect():
  [ (yield testcase) for testcase in check_detect_using_bisect() ]

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: