[TIP] Functional Test Runner for Command Line Tools?

Robert Collins robertc at robertcollins.net
Mon Apr 13 21:39:42 PDT 2009


On Tue, 2009-04-14 at 00:33 -0400, Gary Bernhardt wrote:
> On Tue, Apr 14, 2009 at 12:15 AM, Noah Gift <noah.gift at gmail.com> wrote:
> > In looking at http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy
> > I don't see something that automates writing functional tests for
> > command line tools.  Hopefully, I am wrong, but I am looking for
> > something that will fully exercise a command line tool easily.  How is
> > everyone else testing their command line tools?
> 
> I've had good success just shelling out with subprocess:
> 
> output = Popen(command, stderr=STDOUT, stdout=PIPE, shell=True).read()
> 
> My projects tend to have a bit of glue for doing that (maybe 20-30
> lines). From there they just make assertions about the full or partial
> output. I haven't felt enough pain to look for (or write) libraries
> that help out with this stuff. But maybe there is goodness that I
> don't know I'm missing. :)

I structure the command line tools I write so that there is a _very_
small interface that actually requires a new process. Then you can test
that by Popen - typically 3 or 4 tests total. The rest you can then test
by calling into it within python. StringIO provides a good way to
simulate stdin/stdout/stderr etc.

In bzr we have a bunch of assertions built up about return codes, output
content etc. (But then we're dealing with more than a couple of
commands, so its worth putting effort into making this easy to work
with).

For instance, here is a unit test of the command line:
    def test_push_without_tree(self):
        # bzr push from a branch that does not have a checkout should
work.
        b = self.make_branch('.')
        out, err = self.run_bzr('push pushed-location')
        self.assertEqual('', out)
        self.assertEqual('Created new branch.\n', err)
        b2 = Branch.open('pushed-location')
        self.assertEndsWith(b2.base, 'pushed-location/')

-Rob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
Url : http://lists.idyll.org/pipermail/testing-in-python/attachments/20090414/7706de9d/attachment.pgp 


More information about the testing-in-python mailing list