unittest: collecting tests from many modules?

George Sakkis gsakkis at rutgers.edu
Sun Jun 26 17:17:35 EDT 2005


"Bengt Richter" <bokr at oz.net> wrote:

> On 12 Jun 2005 08:06:18 -0700, "George Sakkis" <gsakkis at rutgers.edu> wrote:
>
> >I had written a script to do something close to this; currently it
> >doesn't do any kind of aggregation, but it should be easy to extend it
> >as you like. What I don't like is the way it currently works: it
> >replaces sys.modules['__main__'] for each unit test and then it
> >execfile()s it, which seems like a hack. I didn't look into unittest's
> >internals in case there is a more elegant way around this; if there
> >isn't, a future version of unittest should address the automatic
> >aggregation of tests, as py.test does already.
> >
> I think if you execfile a script and supply the global dict initialized
> to {'__name__':'__main__'} then I think that will satisfy the if __name__ ... condition
> and the whole thing will run as if executed interactively from the command line.

Yes, execfile is called with {'__name__':'__main__'} for globals but
that's not enough because __main__ is already bound to the test.py
script, not the tested module. Even worse, after each test,
sys.modules['__main__'] seems to have to be reset to the original
__main__; otherwise os (and I guess other globals) is bound to None !
I'm not sure if this is normal or I missed something basic.

Regards,
George


PS: I reposted the script at
http://rafb.net/paste/results/3f1PIZ70.html.




More information about the Python-list mailing list