Mercurial > python-cmd2
diff cmd2.py @ 327:e9eea93c777c
initial args working
author | catherine@dellzilla |
---|---|
date | Thu, 11 Feb 2010 18:22:17 -0500 |
parents | 237a89d5a4a9 |
children | 7b2bca3951a7 |
line wrap: on
line diff
--- a/cmd2.py Thu Feb 11 17:47:29 2010 -0500 +++ b/cmd2.py Thu Feb 11 18:22:17 2010 -0500 @@ -1478,13 +1478,13 @@ self.outputTrap.tearDown() def run(app): - class TestMyAppCase(Cmd2TestCase): - CmdApp = app.__class__ parser = optparse.OptionParser() parser.add_option('-t', '--test', dest='test', action="store_true", help='Test against transcript(s) in FILE (wildcards OK)') (callopts, callargs) = parser.parse_args() if callopts.test: + class TestMyAppCase(Cmd2TestCase): + CmdApp = app.__class__ app.__class__.testfiles = callargs sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() testcase = TestMyAppCase() @@ -1492,31 +1492,37 @@ result = runner.run(testcase) result.printErrors() else: + # hold onto the args and run .onecmd with them + # in sqlpython, first arg has implied \connect + for initial_command in callargs: + if app.onecmd(initial_command + '\n') == app._STOP_AND_EXIT: + return app.cmdloop() if __name__ == '__main__': doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) ''' -To make your application transcript-testable, add text like this to your .py file -(replacing CmdLineApp with your own application class's name). Then, a cut-and-pasted -version of a successful session with your application, saved as a text file, can serve -as a test for future +To make your application transcript-testable, replace + +:: + + app = MyApp() + app.cmdloop() + +with + +:: -Invoke the test later with `python myapplication.py --test mytranscripttestfile.ext` + app = MyApp() + cmd2.run(app) + +Then run a session of your application and paste the entire screen contents +into a file, ``transcript.test``, and invoke the test like:: + + python myapp.py --test transcript.test + Wildcards can be used to test against multiple transcript files. +''' -class TestMyAppCase(Cmd2TestCase): - CmdApp = CmdLineApp -parser = optparse.OptionParser() -parser.add_option('-t', '--test', dest='test', action="store_true", - help='Test against transcript(s) in FILE (wildcards OK)') -(callopts, callargs) = parser.parse_args() -if callopts.test: - CmdLineApp.testfiles = callargs - sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() - unittest.main() -else: - CmdLineApp().cmdloop() -'''