[TIP] testing __init__

Alfredo Deza alfredodeza at gmail.com
Sat Apr 10 06:42:46 PDT 2010


On Fri, Apr 9, 2010 at 9:06 PM, David Stanek <dstanek at dstanek.com> wrote:

> 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)
>
> I overlooked the possibility of using mock, thanks for pointing that out.



>    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
>



-- 
Alfredo Deza
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20100410/821d5257/attachment.html>


More information about the testing-in-python mailing list