[Tutor] unwanted 'zero' ending
Oscar Benjamin
oscar.j.benjamin at gmail.com
Fri Jun 28 02:08:01 CEST 2013
On 28 June 2013 00:39, Jim Mooney <cybervigilante at gmail.com> 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 agree. I prefer them for smaller applications.
> I tried a simple one and it worked, but a puzzlement. Does it Only
> test what you've hard-coded? 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.
I've never run the docstringtest.py script before. I would do as
Steven suggested and use python -m mymod.py:
oscar at lwench:~/current/tmp/unit$ cat mymod.py
# mymod.py
def doubler(inp):
'''Return a doubled number or string
>>> doubler(24)
48
>>> doubler('zark')
'zarkzark'
>>> doubler('foo')
let's fail this test
'''
return inp * 2
oscar at lwench:~/current/tmp/unit$ python -m doctest mymod.py
**********************************************************************
File "mymod.py", line 9, in mymod.doubler
Failed example:
doubler('foo')
Expected:
let's fail this test
Got:
'foofoo'
**********************************************************************
1 items had failures:
1 of 3 in mymod.doubler
***Test Failed*** 1 failures.
oscar at lwench:~/current/tmp/unit$
The output there seems pretty well explained to me.
> 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.
How would it know about things that you haven't hard-coded?
> 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?
No. It tests the hard-coded values: that's the point. You, as the
programmer, say I expect exactly this output from exactly this input
and it tests that the code does what you wanted. If it did anything
more clever it would defeat the point of unit tests which is that they
enable you to be confident that exactly this input gives exactly that
output for each of the cases that you carefully considered.
Yes a lot of hard-coding is needed in any comprehensive set of unit
tests. But sometimes comprehensive testing just isn't needed. For
simple cases it's often enough just to check that it works for a
couple of common cases and perhaps a few corner cases and then that's
fine (if the tests pass).
Oscar
More information about the Tutor
mailing list