[TIP] testing __init__

Mark Sienkiewicz sienkiew at stsci.edu
Thu Apr 15 06:37:20 PDT 2010


Alfredo Deza wrote:

> What is the way you guys use the most (or like the best) for testing
> __init__?
>   


I do not treat it specially in any way.  __init__ acts just like a 
function that returns an object; the only special thing about it is that 
you call it by saying the class name.  So, you can test it just like you 
would test anything else.  For example:

import nose.tools

class foo(object) :
    def __init__(self,a) :
        self.a_plus_1 = a + 1

def test_1() :
   x = foo(1)
   assert x.a_plus_1 == 2

@nose.tools.raises(TypeError)
def test_2() :
   x = foo('a')


Mark S.




More information about the testing-in-python mailing list