Mercurial > python-cmd2
comparison cmd2.py @ 259:5147fa4166b0
multiple transcript tests working - sinister BASH trick defeated
author | Catherine Devlin <catherine.devlin@gmail.com> |
---|---|
date | Thu, 02 Apr 2009 18:07:04 -0400 |
parents | 7a98d7f2da30 |
children | 2b69c4d72cd8 |
comparison
equal
deleted
inserted
replaced
258:7a98d7f2da30 | 259:5147fa4166b0 |
---|---|
25 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest | 25 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest |
26 import unittest, string, datetime, urllib, glob | 26 import unittest, string, datetime, urllib, glob |
27 from code import InteractiveConsole, InteractiveInterpreter, softspace | 27 from code import InteractiveConsole, InteractiveInterpreter, softspace |
28 from optparse import make_option | 28 from optparse import make_option |
29 __version__ = '0.5.1' | 29 __version__ = '0.5.1' |
30 import logging | |
31 | 30 |
32 class OptionParser(optparse.OptionParser): | 31 class OptionParser(optparse.OptionParser): |
33 def exit(self, status=0, msg=None): | 32 def exit(self, status=0, msg=None): |
34 self.values._exit = True | 33 self.values._exit = True |
35 if msg: | 34 if msg: |
1113 that will execute the commands in a transcript file and expect the results shown. | 1112 that will execute the commands in a transcript file and expect the results shown. |
1114 See example.py''' | 1113 See example.py''' |
1115 CmdApp = None | 1114 CmdApp = None |
1116 def fetchTranscripts(self): | 1115 def fetchTranscripts(self): |
1117 self.transcripts = {} | 1116 self.transcripts = {} |
1118 for fname in glob.glob(self.CmdApp.testfile): | 1117 if not self.CmdApp.testfiles: |
1119 tfile = open(fname) | 1118 raise optparse.OptionError, "No test files named - nothing to test." |
1120 self.transcripts[fname] = iter(tfile.readlines()) | 1119 for fileset in self.CmdApp.testfiles: |
1121 tfile.close() | 1120 for fname in glob.glob(fileset): |
1121 tfile = open(fname) | |
1122 self.transcripts[fname] = iter(tfile.readlines()) | |
1123 tfile.close() | |
1122 | 1124 |
1123 def setUp(self): | 1125 def setUp(self): |
1124 if self.CmdApp: | 1126 if self.CmdApp: |
1125 self.outputTrap = OutputTrap() | 1127 self.outputTrap = OutputTrap() |
1126 self.cmdapp = self.CmdApp() | 1128 self.cmdapp = self.CmdApp() |
1173 if self.CmdApp: | 1175 if self.CmdApp: |
1174 self.outputTrap.tearDown() | 1176 self.outputTrap.tearDown() |
1175 | 1177 |
1176 if __name__ == '__main__': | 1178 if __name__ == '__main__': |
1177 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) | 1179 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) |
1180 | |
1181 ''' | |
1182 To make your application transcript-testable, add text like this to your .py file | |
1183 (replacing CmdLineApp with your own application class's name). Then, a cut-and-pasted | |
1184 version of a successful session with your application, saved as a text file, can serve | |
1185 as a test for future | |
1186 | |
1187 Invoke the test later with `python myapplication.py --test mytranscripttestfile.ext` | |
1188 Wildcards can be used to test against multiple transcript files. | |
1189 | |
1190 | |
1191 class TestMyAppCase(Cmd2TestCase): | |
1192 CmdApp = CmdLineApp | |
1193 parser = optparse.OptionParser() | |
1194 parser.add_option('-t', '--test', dest='test', action="store_true", | |
1195 help='Test against transcript(s) in FILE (wildcards OK)') | |
1196 (callopts, callargs) = parser.parse_args() | |
1197 if callopts.test: | |
1198 CmdLineApp.testfiles = callargs | |
1199 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() | |
1200 unittest.main() | |
1201 else: | |
1202 CmdLineApp().cmdloop() | |
1203 ''' |