Mercurial > python-cmd2
comparison cmd2.py @ 327:e9eea93c777c
initial args working
author | catherine@dellzilla |
---|---|
date | Thu, 11 Feb 2010 18:22:17 -0500 |
parents | 237a89d5a4a9 |
children | 7b2bca3951a7 |
comparison
equal
deleted
inserted
replaced
326:237a89d5a4a9 | 327:e9eea93c777c |
---|---|
1476 def tearDown(self): | 1476 def tearDown(self): |
1477 if self.CmdApp: | 1477 if self.CmdApp: |
1478 self.outputTrap.tearDown() | 1478 self.outputTrap.tearDown() |
1479 | 1479 |
1480 def run(app): | 1480 def run(app): |
1481 class TestMyAppCase(Cmd2TestCase): | |
1482 CmdApp = app.__class__ | |
1483 parser = optparse.OptionParser() | 1481 parser = optparse.OptionParser() |
1484 parser.add_option('-t', '--test', dest='test', action="store_true", | 1482 parser.add_option('-t', '--test', dest='test', action="store_true", |
1485 help='Test against transcript(s) in FILE (wildcards OK)') | 1483 help='Test against transcript(s) in FILE (wildcards OK)') |
1486 (callopts, callargs) = parser.parse_args() | 1484 (callopts, callargs) = parser.parse_args() |
1487 if callopts.test: | 1485 if callopts.test: |
1486 class TestMyAppCase(Cmd2TestCase): | |
1487 CmdApp = app.__class__ | |
1488 app.__class__.testfiles = callargs | 1488 app.__class__.testfiles = callargs |
1489 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() | 1489 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() |
1490 testcase = TestMyAppCase() | 1490 testcase = TestMyAppCase() |
1491 runner = unittest.TextTestRunner() | 1491 runner = unittest.TextTestRunner() |
1492 result = runner.run(testcase) | 1492 result = runner.run(testcase) |
1493 result.printErrors() | 1493 result.printErrors() |
1494 else: | 1494 else: |
1495 # hold onto the args and run .onecmd with them | |
1496 # in sqlpython, first arg has implied \connect | |
1497 for initial_command in callargs: | |
1498 if app.onecmd(initial_command + '\n') == app._STOP_AND_EXIT: | |
1499 return | |
1495 app.cmdloop() | 1500 app.cmdloop() |
1496 | 1501 |
1497 if __name__ == '__main__': | 1502 if __name__ == '__main__': |
1498 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) | 1503 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) |
1499 | 1504 |
1500 ''' | 1505 ''' |
1501 To make your application transcript-testable, add text like this to your .py file | 1506 To make your application transcript-testable, replace |
1502 (replacing CmdLineApp with your own application class's name). Then, a cut-and-pasted | 1507 |
1503 version of a successful session with your application, saved as a text file, can serve | 1508 :: |
1504 as a test for future | 1509 |
1505 | 1510 app = MyApp() |
1506 Invoke the test later with `python myapplication.py --test mytranscripttestfile.ext` | 1511 app.cmdloop() |
1512 | |
1513 with | |
1514 | |
1515 :: | |
1516 | |
1517 app = MyApp() | |
1518 cmd2.run(app) | |
1519 | |
1520 Then run a session of your application and paste the entire screen contents | |
1521 into a file, ``transcript.test``, and invoke the test like:: | |
1522 | |
1523 python myapp.py --test transcript.test | |
1524 | |
1507 Wildcards can be used to test against multiple transcript files. | 1525 Wildcards can be used to test against multiple transcript files. |
1508 | |
1509 | |
1510 class TestMyAppCase(Cmd2TestCase): | |
1511 CmdApp = CmdLineApp | |
1512 parser = optparse.OptionParser() | |
1513 parser.add_option('-t', '--test', dest='test', action="store_true", | |
1514 help='Test against transcript(s) in FILE (wildcards OK)') | |
1515 (callopts, callargs) = parser.parse_args() | |
1516 if callopts.test: | |
1517 CmdLineApp.testfiles = callargs | |
1518 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() | |
1519 unittest.main() | |
1520 else: | |
1521 CmdLineApp().cmdloop() | |
1522 ''' | 1526 ''' |
1527 | |
1528 |