changeset 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
files cmd2.py example/example.py
diffstat 2 files changed, 35 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Thu Apr 02 17:22:02 2009 -0400
+++ b/cmd2.py	Thu Apr 02 18:07:04 2009 -0400
@@ -27,7 +27,6 @@
 from code import InteractiveConsole, InteractiveInterpreter, softspace
 from optparse import make_option
 __version__ = '0.5.1'
-import logging
 
 class OptionParser(optparse.OptionParser):
     def exit(self, status=0, msg=None):
@@ -1115,10 +1114,13 @@
     CmdApp = None
     def fetchTranscripts(self):
         self.transcripts = {}
-        for fname in glob.glob(self.CmdApp.testfile):
-            tfile = open(fname)
-            self.transcripts[fname] = iter(tfile.readlines())
-            tfile.close()
+        if not self.CmdApp.testfiles:
+            raise optparse.OptionError, "No test files named - nothing to test."
+        for fileset in self.CmdApp.testfiles:
+            for fname in glob.glob(fileset):
+                tfile = open(fname)
+                self.transcripts[fname] = iter(tfile.readlines())
+                tfile.close()
             
     def setUp(self):
         if self.CmdApp:
@@ -1174,4 +1176,28 @@
             self.outputTrap.tearDown()
         
 if __name__ == '__main__':
-    doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE)
\ No newline at end of file
+    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 
+
+Invoke the test later with `python myapplication.py --test mytranscripttestfile.ext`
+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()
+'''
\ No newline at end of file
--- a/example/example.py	Thu Apr 02 17:22:02 2009 -0400
+++ b/example/example.py	Thu Apr 02 18:07:04 2009 -0400
@@ -32,18 +32,12 @@
 
 class TestMyAppCase(Cmd2TestCase):
     CmdApp = CmdLineApp
-import logging
-logging.basicConfig(level=logging.DEBUG,
-                    filename='exlog.txt',
-                    filemode='w')
 parser = optparse.OptionParser()
-parser.add_option('-t', '--test', dest='test', metavar='FILE', 
-                  help='''Test against transcript(s) in FILE (wildcards OK)''')                  
+parser.add_option('-t', '--test', dest='test', action="store_true", 
+                  help='Test against transcript(s) in FILE (wildcards OK)')
 (callopts, callargs) = parser.parse_args()
-logging.error(callopts)
 if callopts.test:
-    CmdLineApp.testfile = callopts.test
-    logging.error(CmdLineApp.testfile)
+    CmdLineApp.testfiles = callargs
     sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
     unittest.main()
 else: