Wednesday, October 29, 2008

cmd2 0.4, now with testing

cmd2 0.4 has been released. Its biggest new feature is transcript-based testing. Basically, you can write a test suite for your cmd2-based application just this easily:

1. Add to your application script (suppose it's

from cmd2 import Cmd2TestCase
class TestMyAppCase(Cmd2TestCase):
CmdApp = CmdLineApp
transcriptFileName = 'exampleSession.txt'

parser = optparse.OptionParser()
parser.add_option('-t', '--test', dest='unittests', action='store_true', default=False, help='Run unit test suite')
(callopts, callargs) = parser.parse_args()
if callopts.unittests:
sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
app = CmdLineApp()
2. Run a session of your application. Run all the commands you want to test.

3. Cut-and-paste your entire session into exampleSession.txt.

4. Run python -t

Ta-da! cmd2 runs runs your app, issues all the commands saved in exampleSession.txt, and verifies that they produce the same output as in your transcript. Now you can change your app fearlessly without bugs sneaking in.

Finally, cmd2 is now available for Python 2.4 through 2.6.


Unknown said...

Thanks for the great module!

I was wondering: what is the quickest way to make a non-interactive command with cmd2? i.e. just_this_command_and_stop would execute command "just_this_command_and_stop" and return control to the shell.

Chris Heller said...

Hi Catherine, thanks for this - very nice.

I didn't see a quick way to file bug reports at Assembla (no access to create tickets even after registering), so I thought I'd post a quick one here.

When you check whether xclip is installed, the shell prints to stderr that it can't find xclip.

You can point stderr to the same location as stdout by passing "stderr=subprocess.STDOUT" as an additional parameter to check_call.

Chris Heller said...

By the way Jean-Lou, the right way to do what you want can be found in Doug Hellman's great Python Module of the Week blog entry on cmd.

Look for the InteractiveOrCommandLine class example. It applies equally well to cmd2.