I highly recommend subprocess.check_call over os.system check_call will raise an exception if the return code of the process is not 0, it will take arguments as a list, negating the need to escape arguments, if you don't set shell=True it requires an absolute path to the script, and bypasses the systems shell & PATH variables (adding to security)
On Tue, Oct 8, 2013 at 10:05 AM, Maria McKinley <[email protected]>wrote: > Thanks everyone! > I gave up on sys.argv and opted to not try to import my module into my > test script, so not trying to change the options after the fact, so as to > speak. Now I am just running the python script using the os.system call > from the unittest, and that is working. It is going to take a while to > figure out the best way in general to run unit tests on this code. I like > Matt's suggestion of just importing what I need from their code to run a > particular test, but that could get pretty complicated. But, heh, I'm > making VR games in python, so it's all good! > > cheers, > Maria > > > On Mon, Oct 7, 2013 at 7:46 PM, Christopher Barker <[email protected]>wrote: > >> >> >> On Monday, October 7, 2013, Maria McKinley wrote: >> >>> So, they are using optparse. Is there a way to take advantage of this? >>> >>> When I tried to use the sys.argv=[], I found that having the first >>> option None gave me a TypeError. If I used: >>> >>> import sys >>> sys.argv = ['-stest'] >>> >>> >> I'd use sys.arv.extend or .append, rather than replace it. So the first >> arg is right. >> >>> I get this: >>> >>> Usage: -stest [options] >>> >>> -stest: error: Please specify a subject ID with '-s'. >>> >>> That sure looks like there is supposed to be a space between the s and >> test: -s test >> >> or -stest subject_id >> >> MAke sure you can run it on the command line the way you want before >> emulating it. >> >> So it might be: >> >> sys.argv.append('-s') >> sys.argv.append('Id') >> >> HTH >> >> Chris >> >> >> >> Does this make sense to anyone? Same results for sys.argv.extend >>> >>> I've put the relevant bits of the full trace for the TypeError below >>> (that was the only time I got a full trace). >>> >>> ~m >>> >>> DirectStart: Starting the game. >>> Warning: NodePathCollection.asList() is no longer needed and deprecated. >>> Iterat >>> e on the collection directly instead. >>> Known pipe types: >>> wglGraphicsPipe >>> (all display modules loaded.) >>> Traceback (most recent call last): >>> File "test_goBananas.py", line 2, in <module> >>> import goBananas >>> File "c:\Users\eblab.WANPRC\panda\goBananas\goBananas.py", line 35, in >>> <module >>> > >>> goBananas().start() >>> File "c:\Users\eblab.WANPRC\panda\goBananas\goBananas.py", line 11, in >>> __init_ >>> _ >>> exp = epl.Experiment.getInstance() >>> File >>> "c:\Panda3D-1.8.1\python\lib\site-packages\pandaepl\Experiment.py", line >>> 86, in getInstance >>> Experiment.singletonInstance = Experiment() >>> File >>> "c:\Panda3D-1.8.1\python\lib\site-packages\pandaepl\Experiment.py", line >>> 51, in __init__ >>> Options.getInstance().error("Please specify a subject ID with '-s'.") >>> File "c:\Panda3D-1.8.1\python\lib\optparse.py", line 1582, in error >>> self.print_usage(sys.stderr) >>> File "c:\Panda3D-1.8.1\python\lib\optparse.py", line 1602, in >>> print_usage >>> print >>file, self.get_usage() >>> File "c:\Panda3D-1.8.1\python\lib\optparse.py", line 1588, in get_usage >>> self.expand_prog_name(self.usage)) >>> File "c:\Panda3D-1.8.1\python\lib\optparse.py", line 1565, in >>> expand_prog_name >>> >>> return s.replace("%prog", self.get_prog_name()) >>> File "c:\Panda3D-1.8.1\python\lib\optparse.py", line 1560, in >>> get_prog_name >>> return os.path.basename(sys.argv[0]) >>> File "c:\Panda3D-1.8.1\python\lib\ntpath.py", line 198, in basename >>> return split(p)[1] >>> File "c:\Panda3D-1.8.1\python\lib\ntpath.py", line 170, in split >>> d, p = splitdrive(p) >>> File "c:\Panda3D-1.8.1\python\lib\ntpath.py", line 125, in splitdrive >>> if p[1:2] == ':': >>> TypeError: 'NoneType' object has no attribute '__getitem__' >>> >>> >>> >>> On Mon, Oct 7, 2013 at 4:28 PM, Rohit Patnaik <[email protected]>wrote: >>> >>> Can you see how they're getting the command line options? The sensible >>> thing would be to use something like optparse, but from your description, I >>> suspect they're not doing the sensible thing. >>> On Oct 7, 2013 4:22 PM, "Maria McKinley" <[email protected]> >>> wrote: >>> >>> I couldn't get either the sys.argv.extend or sys.argv = [None...] to >>> work. I just keep getting the error to specify that option. They do specify >>> it kind of strangely. the option is script.py -sName >>> which I've never seen before. They just kind of run the option name and >>> the argument together. I tried all kinds of variations >>> '-sname' >>> '-s', 'name' >>> '-s=name' >>> 's=name' >>> >>> Nothing works. This library doesn't seem to have an option for running >>> it directly from the python API. I'm thinking their library was not tested >>> with actual test code. Meh. >>> >>> I'm contemplating mucking with their code to set a default for their >>> 'mandatory' option, but I'd really rather not do that. >>> >>> And yes, testing it as a script would not be unit testing. I'm not sure >>> what I was imagining there. ;-) >>> >>> thanks, >>> Maria >>> >>> >>> On Mon, Oct 7, 2013 at 4:08 PM, Matt S. <[email protected]> wrote: >>> >>> Chris beat me to the punch but here's what I was thinking... >>> >>> http://docs.python.org/2/library/optparse.html >>> >>> 15.5.3.7 Parsing arguments >>> (options, args) = parser.parse_args(args=None, values=None) >>> >>> >>> >>> where the input parameters are >>> args -- the list of arguments to process (default: sys.argv[1:]) >>> >>> >>> It looks to me like you just need to emulate sys.argv[1:]. >>> >>> ./myscript.py -a val1 -b val2 >>> >>> >>> Maybe a bad idea but I think if you import sys and then define sys.argv >>> as a list whose first item is going to be ignored: >>> >>> if __name__=='__main__': >>> >>> sys.argv = [None, "-a", "val1", "-b", "val2"] >>> >>> start() >>> >>> >>> >>> >>> >>> >>> >>> On Mon, Oct 7, 2013 at 3:31 PM, Maria McKinley >>> <[email protected]>wrote: >>> >>> I am using a python package that has a required command line option I am >>> suppose to use when I invoke my script if I am using their library. Kind of >>> weird, I know, I love required options! I want to load my class from the >>> python interpreter for testing purposes. Is there a way to specify options >>> from the python interpreter? I know that __main__ is run from the command >>> line, so I should be able to add stuff in an if statement that ch >>> >>> >> >> -- >> Christopher Barker, PhD >> >> Python Language Consulting >> - Teaching >> - Scientific Software Development >> - Desktop GUI and Web Development >> - wxPython, numpy, scipy, Cython >> >> > > > -- > Maria Mckinley > Software Developer with Bonus SysAdmin Experience > www.mariakathryn.net > www.linkedin.com/in/mariamckinley >
