Using Comparitors in assertions.

6 views
Skip to first unread message

Simon Brunning

unread,
Aug 19, 2008, 3:38:14 PM8/19/08
to mox-discuss
I'd really like to be able to use Mox's comparitors in assertions, as
well at in mock setup, so, for instance, I could do something like:

self.mox.AssertThat(revisions, mox.SameElementsAs([123, 456, 789]))

Whereas I think I have to do:

self.assertEquals(revisions, [123, 456, 789])

This doesn't read as well, and is more fragile. The right set of
Comparitors - ContainsAllOf, ContainsInOrder, ContainsNoneOf for
example - makes for expressive tests.

Hmmm. Perhaps this doesn't belong in Mox, though. EasyMock uses
Hamcrest to provide this sort of functionality, so perhaps Mox needs
Spamcrest! There's a project to keep me out of trouble for a while.

Thoughts?

Cheers,
Simon B.
si...@brunningonline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues |
Twitter: brunns

Manuel Holtgrewe

unread,
Aug 19, 2008, 3:45:24 PM8/19/08
to mox-d...@googlegroups.com
Hi Simon.

In the past, I extended unittest.TestCase for my project and added
things like

def assertSetEquals(self, left, right, msg)
self.assertEquals(set(self), set(right),
msg or ('Iterables %s and %r do not contain different elements' %
(left, right))

I guess you could implement the assertions mentioned below using
list() constructors and set operations.

HTH
Manuel

steve middlekauff

unread,
Aug 19, 2008, 5:14:16 PM8/19/08
to mox-d...@googlegroups.com
You can use Comparators in your assertions, as long as you are careful
to keep them on the LHS (you may or may not get some strange behavior
otherwise).

def testUsingComparatorsInAssertions(self):
self.assertEquals(mox.IsA(str), "test string)

... or for your example ...

self.assertEquals(mox.SameElementsAs([123, 456, 789]), revisions)

I'm not sure if this is as expressive as you'd like, but it gets the
job done. :)

--
Steve Middlekauff
smid...@gmail.com

Simon Brunning

unread,
Aug 19, 2008, 10:04:33 PM8/19/08
to mox-discuss
On Aug 19, 10:14 pm, "steve middlekauff" <smidd...@gmail.com> wrote:
> You can use Comparators in your assertions, as long as you are careful
> to keep them on the LHS (you may or may not get some strange behavior
> otherwise).
>
> def testUsingComparatorsInAssertions(self):
>   self.assertEquals(mox.IsA(str), "test string)
>
> ... or for your example ...
>
> self.assertEquals(mox.SameElementsAs([123, 456, 789]), revisions)
>
> I'm not sure if this is as expressive as you'd like, but it gets the
> job done. :)

It certainly does. That's really nice.
Reply all
Reply to author
Forward
0 new messages