[Tutor] unwanted 'zero' ending
Dave Angel
davea at davea.name
Fri Jun 28 02:05:31 CEST 2013
On 06/27/2013 07:39 PM, Jim Mooney wrote:
> On 27 June 2013 05:38, Steven D'Aprano <steve at pearwood.info> wrote:
>
>> Unit tests are great, but the learning curve is rather steep. I recommend that you start with doctests.
>
> I tried a simple one and it worked, but a puzzlement. Does it Only
> test what you've hard-coded?
Precisely. There's no way that it could test anything else, since it
has no way of reading your mind to see what you intended.
> That is, here is a simple test:
>
> def doubler(inp):
> '''Return a doubled number or string
> >>> doubler(24)
> 48
> >>> doubler('zark')
> 'zarkzark'
> '''
> return inp * 2
>
> This works on 24 or 'zark' as input when I run
> C:\Python33\Jimprogs>python -m doctest
> "C:/python33/jimprogs/docstringtest.py" --verbose'
> and doctest prints:
> 48
> zarkzark
>
> And it fails if I put 'this doesn't work' as the return value of the function:
>
> 1 items had failures:
> 2 of 2 in docstringtest.
> ***Test Failed*** 2 failures.
>
> Although that doesn't tell me much.
>
> But it also works on different input --> 189 and 'plantagenet,' to print:
> 378
> plantagenetplantagenet
>
> It's odd to me that it doesn't fail on what I haven't hardcoded.
>
> I don't see how docstring could figure what I might be doing, so I
> assume that although it returns anything valid, it Only tests on the
> hardcoded values, 24 and 'zark'. Is this correct? In which case it
> seems like a lot of hard coding would be needed unless you tested only
> endpoints or what might be problematic.
>
> Or is it doing something more?
>
Nope. it is limited to the tests you write. And those tests are
necessarily fairly simple. That's why there are other testing frameworks
which support much more, with a much higher learning curve. But you'd
be surprised how much code that's written that gets no useful repeatable
testing at all. Or how much software that has tests which are seldom
run before a release.
I've seen software released for which the source code didn't even exist,
and couldn't readily exist... "Hack something together, just get it
'working'" That was when i was just transferring into a department, and
it never happened again.
--
DaveA
More information about the Tutor
mailing list