Yelp / Testify

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

Test for warnings raised within a function or `with` block #190

Closed mesozoic closed 11 years ago

mesozoic commented 11 years ago

This branch adds an assert_warns method to the testify.assertions module. This allows the following syntax:

class MyTest(T.TestCase):
    def test_some_method_generates_a_warning(self):
        T.assert_warns(UserWarning, some_method, *args, **kwargs)

    def test_warning_generated_within_context_manager(self):
        with T.assert_warns():
            warnings.warn('Hey!')
mesozoic commented 11 years ago

@bukzor These two commits should address your earlier feedback. This now supports custom assertion functions:

class MyTest(T.TestCase):

    def throw_user_warning(self):
        warnings.warn('Whoa!')

    def assert_three_user_warnings(self, warnings):
        T.assert_equal(['Whoa!'], [str(w) for w in warnings])

    def test_with_custom_assertions(self):
        T.assert_warns_such_that(self.assert_three_user_warnings):
            self.throw_user_warning()
bukzor commented 11 years ago

Looks right.

milki commented 11 years ago

lgtm. shipit.

If no other concerns, will merge 10/14

mrtyler commented 11 years ago

Since I had to tweak some other warning-related stuff to make some tests pass in Python 2.7, I verified that these new warning-related tests work with 2.7 as well. \o/

Thanks for the patch @aclevy!