[TIP] Functional Test Runner for Command Line Tools?

Noah Gift noah.gift at gmail.com
Mon Apr 13 22:27:44 PDT 2009


On Tue, Apr 14, 2009 at 4:39 PM, Robert Collins
<robertc at robertcollins.net> wrote:
> 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).

That sounds like a great start to a generic testing library for
testing command line tools.  Any chance that will happen?

>
> 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
>



-- 
Cheers,

Noah



More information about the testing-in-python mailing list