469,352 Members | 2,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

my module and unittest contend over commandline options...

using the unittest module in Python 2.3.5, I've written some test code
that ends up with

if __name__ == "__main__":
unittest.main()

Since I want to run this code in various environments, I'd initially
added some commandline options, e.g. to specify a configuration file
like so

test.py -c devtest.conf
or
test.py -c localtest.conf

etc.
However, unittest also looks for options on the commandline, and it
was complaining about unrecognized options and quitting.

I've poked around to see if I could delete the options my earlier code
consumed from the commandline buffer, before invoking unittest, but
that seems klugy. Instead, I hardwired in a testing config file name,
that always has to be local. That works pretty well, but it leaves me
wonderfing whether there would have been another clean way to allow
both my test code and unittest to have options without interfering
with one another.
Jun 27 '08 #1
2 1214
chrisber <ch***************@gmail.comwrote:
I've poked around to see if I could delete the options my earlier code
consumed from the commandline buffer, before invoking unittest, but
that seems klugy. Instead, I hardwired in a testing config file name,
that always has to be local. That works pretty well, but it leaves me
wonderfing whether there would have been another clean way to allow
both my test code and unittest to have options without interfering
with one another.
You can pass argv as a parameter to main(), so I think the best bet is
simply to build up a new argv with the options that you want to pass
through.

Another option would be to subclass unittest.TestProgram and override
parseArgs, but you'd have to copy & modify it as it doesn't seem to have
been designed to be easily extendable.
Jun 27 '08 #2
On May 2, 4:54 am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
chrisber<christophberen...@gmail.comwrote:
...it leaves me
wonderfing whether there would have been another clean way to allow
both my test code and unittest to have options without interfering
with one another.

You can pass argv as a parameter to main(), so I think the best bet is
simply to build up a new argv with the options that you want to pass
through.
Aha! I scanned through my python 2.4 installataion and found this in
unittest
def __init__(self, module='__main__', defaultTest=None,
argv=None, testRunner=None,
testLoader=defaultTestLoader):
if type(module) == type(''):
self.module = __import__(module)
for part in module.split('.')[1:]:
self.module = getattr(self.module, part)
else:
self.module = module
if argv is None:
argv = sys.argv

so I see what you mean. Thanks.

Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Active8 | last post: by
reply views Thread by Remy Blank | last post: by
8 posts views Thread by Steve Erickson | last post: by
2 posts views Thread by MrBlueSky | last post: by
10 posts views Thread by Ben Finney | last post: by
reply views Thread by axlq | last post: by
1 post views Thread by Tom Harris | last post: by
3 posts views Thread by james | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.