Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54643
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-09-23 07:45 -0700 |
| References | (5 earlier) <523e5838$0$29988$c3e8da3$5496439d@news.astraweb.com> <48fb04f5-168e-4c91-83fe-f7a45efced25@googlegroups.com> <523edec3$0$29988$c3e8da3$5496439d@news.astraweb.com> <d116e6db-5bd7-4d33-8097-8377dec5c171@googlegroups.com> <baauplFmluiU1@mid.individual.net> |
| Message-ID | <fddfbf8b-5dde-4644-8394-7d1a43c608ad@googlegroups.com> (permalink) |
| Subject | Re: Sphinx Doctest: test the code without comparing the output. |
| From | Luca Cerone <luca.cerone@gmail.com> |
> It won't be very good documenation any more but nothing stops you
>
> from examining the result in the next doctest and making yourself
>
> happy about it.
>
>
>
> >>> x = input("indeterminate:")
>
> >>> result = "'{}'".format(x))
>
> >>> result.startswith("'") and result.endswith("'")
>
> True
>
Hi Neil, thanks for the hint, but this won't work.
The problem is that the function displays some output informing you of what steps are being performed (some of which are displayed by a 3rd party function that I don't control).
This output "interferes" with the output that should be checked by doctest.
For example, you can check that the following doctest would fail:
.. doctest:: example_fake
>>> def myfun(x,verbose):
... print "random output"
... return x
>>> myfun(10)
10
When you run make doctest the test fails with this message:
File "tutorial.rst", line 11, in example_fake
Failed example:
myfun(10)
Expected:
10
Got:
random output
10
In this case (imagine that "random output" is really random, therefore I can not easily filter it, if not ignoring several lines. This would be quite easy if ellipsis and line continuation wouldn't have the same sequence of characters, but unfortunately this is not the case.
The method you proposed still is not applicable, because I have no way to use startswith() and endswith()...
The following code could do what I want if I could ignore the output...
>>> def myfun(x,verbose):
... print "random output"
... return x
>>> result = myfun(10) #should ignore the output here!
>>> print result
10
fails with this message:
File "tutorial.rst", line 11, in example_fake
Failed example:
result = myfun(10)
Expected nothing
Got:
random output
(line 11 contains: >>> result = myfun(10))
A SKIP directive is not feasible either:
.. doctest:: example_fake
>>> def myfun(x):
... print "random output"
... return x
>>> result = myfun(10) # doctest: +SKIP
>>> result
10
fails with this error message:
File "tutorial.rst", line 12, in example_fake
Failed example:
result
Exception raised:
Traceback (most recent call last):
File "/usr/lib/python2.7/doctest.py", line 1289, in __run
compileflags, 1) in test.globs
File "<doctest example_fake[2]>", line 1, in <module>
result
NameError: name 'result' is not defined
As you can see is not that I want something too weird, is just that sometimes you can't control what the function display and ignoring the output is a reasonable way to implement a doctest.
Hope these examples helped to understand better what my problem is.
Thanks all of you guys for the hints, suggestions and best practices :)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-21 03:47 -0700
Re: Sphinx Doctest: test the code without comparing the output. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-21 12:27 +0000
Re: Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-21 05:44 -0700
Re: Sphinx Doctest: test the code without comparing the output. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-21 15:48 +0000
Re: Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-21 09:25 -0700
Re: Sphinx Doctest: test the code without comparing the output. Chris Angelico <rosuav@gmail.com> - 2013-09-22 11:33 +1000
Re: Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-21 21:09 -0700
Re: Sphinx Doctest: test the code without comparing the output. Chris Angelico <rosuav@gmail.com> - 2013-09-22 14:25 +1000
Re: Sphinx Doctest: test the code without comparing the output. Ned Batchelder <ned@nedbatchelder.com> - 2013-09-22 09:39 -0400
Re: Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-22 07:26 -0700
Re: Sphinx Doctest: test the code without comparing the output. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-23 00:36 +0000
Re: Sphinx Doctest: test the code without comparing the output. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-22 02:38 +0000
Re: Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-21 21:15 -0700
Re: Sphinx Doctest: test the code without comparing the output. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-22 12:12 +0000
Re: Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-22 07:24 -0700
Re: Sphinx Doctest: test the code without comparing the output. Neil Cerutti <neilc@norwich.edu> - 2013-09-23 13:42 +0000
Re: Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-23 07:45 -0700
Re: Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-23 07:51 -0700
Re: Sphinx Doctest: test the code without comparing the output. Skip Montanaro <skip@pobox.com> - 2013-09-23 10:14 -0500
Re: Sphinx Doctest: test the code without comparing the output. Neil Cerutti <neilc@norwich.edu> - 2013-09-23 16:44 +0000
Re: Sphinx Doctest: test the code without comparing the output. Neil Cerutti <neilc@norwich.edu> - 2013-09-23 16:53 +0000
Re: Sphinx Doctest: test the code without comparing the output. Luca Cerone <luca.cerone@gmail.com> - 2013-09-23 11:54 -0700
csiph-web