[TIP] testing __init__

David Stanek dstanek at dstanek.com
Fri Apr 9 18:06:11 PDT 2010


On Fri, Apr 9, 2010 at 8:54 PM, Alfredo Deza <alfredodeza at gmail.com> wrote:
> Hi
> I am having issues testing the constructor of a class that has a try/except
> block.
> Should __init__ raise an exception so I can catch via self.assertRaises to
> make it easier to test? Or are there other ways
> to make __init__ "testable"?
> What is the way you guys use the most (or like the best) for testing
> __init__?
>
>

Do you have a code snippet?

If I understand you correctly I'd write at least two tests. Something like:

    @raises(SomeException)
    def test_an_error_is_thrown():
        # setup some mock stuff to make the __init__ raise an exception
        ClassUnderTest(some mocks or something)

    def test_good_behavior():
        # setup some mock stuff in a good way
        x = ClassUnderTest(some mocks or something)
        # assert some behavior

-- 
David
blog: http://www.traceback.org
twitter: http://twitter.com/dstanek



More information about the testing-in-python mailing list