Yelp / Testify

A more pythonic testing framework.
Other
308 stars 67 forks source link

Add assert_true(), assert_false() #178

Closed wting closed 11 years ago

wting commented 11 years ago

Adds assert_true() and assert_false() shortcut wrappers.

milki commented 11 years ago

I saw a suggestion:

assert_that(blah.bla, is_true())

This would allow the user to supply their own truth.

wting commented 11 years ago

@att14: Truthy.

A user can always be explicit with assert_true(x is True).

att14 commented 11 years ago

This is incorrect then.

In [1]: 1 == True
Out[1]: True

In [2]: 'hi' == True
Out[2]: False

In [3]: bool('hi')
Out[3]: True

In [4]: 2 == True
Out[4]: False

In [5]: import testify as T

In [6]: T.assert_equal(2, True)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-6-a96114e6ec5b> in <module>()
----> 1 T.assert_equal(2, True)

/Library/Python/2.7/site-packages/testify-0.3.8-py2.7.egg/testify/assertions.pyc in assert_equal(lval, rval, message)
    239         assert lval == rval, \
    240             "assertion failed: l == r\nl: %r\nr: %r\n\n%s" % \
--> 241                 (lval, rval, _diff_message(lval, rval))
    242
    243 assert_equals = assert_equal

AssertionError: assertion failed: l == r
l: 2
r: True

Diff:
l: 2
r: True

In [7]: T.assert_equal(1, True)
att14 commented 11 years ago

Also, I doubt this will be accepted without tests.

wting commented 11 years ago

Updated. FYI this mimics unittest.assertTrue() and unittest.assertFalse() behavior.

mrtyler commented 11 years ago

ShipIt

Can't automatically merge though so I'm not merging it right now.

wting commented 11 years ago

Rebased on Yelp/Testify/master, only test failures are catbox related.

mrtyler commented 11 years ago

There are still (or again) conflicts. :/. I think you're thumb-wrestling with @bukzor, whose changes are more urgent so they get priority.

wting commented 11 years ago

Rebased off Yelp/Testify/master again.

Whitespace changes are according to Yelp guidelines (2 lines between top level elements, 1 blank line after class definition) for consistency's sake.

wting commented 11 years ago
  1. The original implementation was just a wrapper around assert_equal(), but the wrong message is printed when the assertion fails.
  2. It's a convenience function. It adds value by reducing boilerplate and make it a bit more explicit what you're testing. This seems to be a general consensus since most popular testing frameworks have an equivalent function.