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

Roy Smith roy at panix.com
Thu Apr 7 14:56:26 MEST 2005


On Apr 7, 2005, at 7:29 AM, Armin Rigo wrote:

> Hi Holger,
>
> 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.

Yes, that was exactly my point.  Although, after thinking about it 
more, I came to the realization that there's two different classes 
we're talking about.  There's the test driver class, and there's the 
class you're testing.  When I wrote my original note, I was blurring 
the two.

In unittest, you get a new instances of TestDriver automatically, and 
it's up to you to create your own instance of ClassBeingTested in 
TestDriver.SetUp().  In py.test, you get a hand-me-down instance of 
TestDriver, and it's still up to you to create (or not, I suppose) your 
own instance of ClassBeingTested in TestDriver.setup_method().

So, given that new-found understanding, I think I'm OK with what 
py.test is doing.  Py.test's re-use of the TestDriver instance gives 
you the opportunity to store state between individual test cases, 
should you desire to do so.  Although, to be fair, you could do the 
same in unitest, by stashing the state in a global.

>
> In general however what kind of automatic instantiation should occur 
> for
> TestXxx classes is just a matter of convention.  Py's is not 
> Unittest's and
> both make some sense...
>
>
> Armin



More information about the py-dev mailing list