[py-dev] Does each test method get called with a new instance?

holger krekel hpk at trillke.net
Thu Apr 7 23:17:15 CEST 2005


Hi Armin, hi Roy, 

On Thu, Apr 07, 2005 at 12:29 +0100, Armin Rigo wrote:
> On Tue, Apr 05, 2005 at 11:34:11PM +0200, holger krekel wrote:
> > Not quite, instead of writing 
> > 
> >     def __init__(self): 
> >         ... 
> > 
> > you just write 
> > 
> >     def setup_method(self, method): 
> >         ... 
> 
> I think the point of Roy was that in the second case the 'self' object
> contains stuff left over by previous tests, which -- if you're not careful --
> might interfere with this new test.

ah, i missed this detail. Probably because such a situation requires 
buggy code, no?  Schematically you would have: 

    def test_1(self): 
        self.x = 3
        ... 

    def test_2(self): 
        ... use self.x ... 

which doesn't sound quite right.  It can be argued that py.test should
be more helpful by instantiating the instance on every test method. 

However, it is possible, as Roy mentions in his reply, that test_2 
_wants_ to use self.x which would allow incremental testing. 
Howering-the-however, such incremental testing is fragile with respect
to "--session" mode which reruns only failing tests. There is no
sane way (short of running PyPy's typeinferencing or something :-) 
to detect that test_2 uses state that was created by test_1. 

Ok, ending these complicated considerations, i am 50:50 in 
instantiating the TestClass on every test method invocation.  
Anybody wants to weigh in on this detail? 

Hum, would it make sense to be able to explicitely specify
incremental testing per-module or per-class?  Sometimes I want
to order tests by complexity and don't want to proceed if
something fundamental fails early.  

cheers, 

    holger



More information about the Pytest-dev mailing list