----- Original Message ----- From: Carl Friedrich Bolz <[EMAIL PROTECTED]> Date: Monday, December 17, 2007 10:07 am Subject: Re: [py-dev] py.test generative tests unexpected behavior
> [EMAIL PROTECTED] wrote: > > > > ----- Original Message ----- > > From: Carl Friedrich Bolz <[EMAIL PROTECTED]> > > Date: Monday, December 17, 2007 8:25 am > > Subject: Re: [py-dev] py.test generative tests unexpected behavior > > > >> Hi! > >> > >> j. vickroy wrote: > >> [snip] > >>> The attached file (py-test-generator-trial.py) demonstrates a > >> behavior I > >>> do not understand. > >> [snip] > >> > >> I don't know generative test semantics all that well, I hoped > >> somebody > >> else would answer this. However, my theory is as follows: > >> > >> py.test first collects all the results of the generator and then > >> runs > >> them all. Since your test keeps global state, the state is > already > >> changed when the first test is run. To me this sounds like > >> reasonable > >> behavior, global state is not good in tests anyway. > >> > >> Does that make things clearer? > >> > >> Cheers, > >> > >> Carl Friedrich > >> _______________________________________________ > >> py-dev mailing list > >> py-dev@codespeak.net > >> http://codespeak.net/mailman/listinfo/py-dev > >> > > > > Thanks, Carl. > > > > I do understand (at least) some of the pitfalls of global state > and I do > > *almost always* avoid using it. However, in this case, I am having > > difficulty seeing why it is responsible for the observed behavior. > > I think what py.test does is essentially this: > > l = list(test_filters()) > # at this point source.info['palette'] == 'Gray' > # and that won't change any more > for t in l: > t[0](*t[1:]) # here the check functions are executed > > > > Having said that, prior to using the global *image* object, I > tried to > > use the setup_module(module) function to define the *image* > object but > > was unable to determine how to reference it in the test function. > > You can stick the the image into the module argument that the > setup_module function receives as first arg: > > def setup_module(mod): > mod.image = ... > > After the setup, the global name 'image' is bound to ... > This wouldn't have solved your problem, though, since it is still > global > state. > > [snip] > > Cheers, > > Carl Friedrich OK, it seems that module-level objects are not really suitable for use in the generative framework. Diagnostic statements show each test is actually being run twice and that the second running is where the failures occur. A work-around, in this particular situation, is to create a new test object for each iteration of the generator and that is not too expensive here, but I do have concerns about scalability of this approach. Thanks again for your follow-ups. --jv > _______________________________________________ > py-dev mailing list > py-dev@codespeak.net > http://codespeak.net/mailman/listinfo/py-dev > _______________________________________________ py-dev mailing list py-dev@codespeak.net http://codespeak.net/mailman/listinfo/py-dev