views:

97

answers:

1

I like doctest but when you have complex arguments that you need to set before you pass to a function it become really hard to read.. Hence, you start using multiple lines assigning then calling the function that you would like to test.. This approach however, will report that you have multiple tests rather then the real number of tests that you have.. An example will illustrate what I mean..

def returnme(x):
   """
   Returns what you pass

   >>> y = (2, 3, 5, 7)
   >>> returnme(y)
   (2, 3, 5, 7)
   """
   return x

In the above snippet, there is only one test and the other is just a variable assignment, however, this is what gets reported..

    Trying:
       y = (2, 3, 5, 7)
    Expecting nothing
    ok
    Trying:
       returnme(y)
    Expecting:
       (2, 3, 5, 7)
    ok
    
    2 tests in 2 items.
    2 passed and 0 failed.

I've looked at the flags documented, surely I missing something..

+5  A: 

Prepend three periods to indicate that you want to continue the current line, like so:

def returnme(x):
   """
   Returns what you pass

   >>> y = (2, 3, 5, 7)
   ... returnme(y)        # Note the difference here.
   ...                    # Another blank line ends this test.
   (2, 3, 5, 7)
   """
   return x

That should do the trick. You can read more about how doctest interprets the individual tests here.

John Feminella
Okay. I copied an pasted that to my file. The test fails now, because the function returns nothing. You need a new line after the "...".. I think! Thanks for the pointer anyway :)
beidy
Sorry, I did forget to add that. You can add another "..." line and that'll end things.
John Feminella
Nope you still need a newline :) unless am doing something horribly horribly wrong. Do you want to check?
beidy
Hmm. I'm not on a machine with python right now, but the documentation definitely suggests that this is the way it's supposed to work. (Check out the link.) Maybe the docs are wrong?
John Feminella
I've read the documentation page almost twice before I posted the question, but still the "..." didn't occur to me. So either sleep deprivation symptoms are showing up, or that documentation needs some tweaking. Without a blank line I get no output. :)
beidy
Sorry John.. Am working with python2.5 on multiple machines.. and the above doesn't provide a solution.
beidy