Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31694 > unrolled thread
| Started by | David <zhushenli@gmail.com> |
|---|---|
| First post | 2012-10-18 17:08 -0700 |
| Last post | 2012-10-19 09:27 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
Add if...else... switch to doctest? David <zhushenli@gmail.com> - 2012-10-18 17:08 -0700
Re: Add if...else... switch to doctest? Ben Finney <ben+python@benfinney.id.au> - 2012-10-19 12:29 +1100
Re: Add if...else... switch to doctest? Terry Reedy <tjreedy@udel.edu> - 2012-10-18 21:31 -0400
Re: Add if...else... switch to doctest? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-19 01:57 +0000
Re: Add if...else... switch to doctest? Duncan Booth <duncan.booth@invalid.invalid> - 2012-10-19 09:27 +0000
| From | David <zhushenli@gmail.com> |
|---|---|
| Date | 2012-10-18 17:08 -0700 |
| Subject | Add if...else... switch to doctest? |
| Message-ID | <c500a76c-3e6b-4dfc-97ca-8b24f0628620@googlegroups.com> |
Hello, how to add if...else... switch to doctest? E.g. function outputs different value when global_var change. """ if (global_var == True): >>> function() [1,2] else: >>> function() [1,2,3] """ Thank you very much.
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2012-10-19 12:29 +1100 |
| Message-ID | <mailman.2487.1350610181.27098.python-list@python.org> |
| In reply to | #31694 |
David <zhushenli@gmail.com> writes:
> Hello, how to add if...else... switch to doctest?
> E.g. function outputs different value when global_var change.
>
> """
> if (global_var == True):
> >>> function()
> [1,2]
> else:
> >>> function()
> [1,2,3]
> """
>
> Thank you very much.
You write the code in a doctest as it would appear at a standard Python
interactive prompt.
>>> if global_thing:
... foo()
[1, 2]
But you need it to be deterministic, so that the output *always*
matches what your docstring declares.
So if you want the result to depend on some state, you need to ensure
that state in your test.
>>> global_thing = True
>>> foo()
[1, 2]
>>> global_thing = False
>>> foo()
[1, 2, 3]
Because this is gnarly, it's yet another reason not to depend so much on
globals. Instead, change the function so its state is passed in
explicitly::
>>> foo(bar=True)
[1, 2]
>>> foo(bar=False)
[1, 2, 3]
Once your functions and tests get complex, you should be using a more
sophisticated testing framework. Don't put complex tests in your
documentation. Instead, put *examples* for readers in the documentation,
and use doctest to test your documentation.
Doctest is for testing explanatory code examples. For more thorough
testing, don't use doctests. Use unit tests with ‘unittests’, feature
tests with Behave <URL:http://pypi.python.org/pypi/behave> or something
similar.
--
\ “I tell you the truth: this generation will certainly not pass |
`\ away until all these things [the end of the world] have |
_o__) happened.” —Jesus Christ, c. 30 CE, as quoted in Matthew 24:34 |
Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-10-18 21:31 -0400 |
| Message-ID | <mailman.2489.1350610506.27098.python-list@python.org> |
| In reply to | #31694 |
On 10/18/2012 8:08 PM, David wrote: > Hello, how to add if...else... switch to doctest? > E.g. function outputs different value when global_var change. > > """ > if (global_var == True): >>>> function() > [1,2] > else: >>>> function() > [1,2,3] > """ doctests should/must be self contained. IE, you would have to set the 'global_var' in the doctext code itself. >>> def function(): ... if global_var: return [1,2] ... else: return [1,2,3] >>> global_var = True >>>function() [1,2] >>> global_var = False >>> function() [1,2,3] -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-10-19 01:57 +0000 |
| Message-ID | <5080b391$0$29985$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #31694 |
On Thu, 18 Oct 2012 17:08:37 -0700, David wrote:
> Hello, how to add if...else... switch to doctest?
Step 1):
Write your doctest with the switch. Don't forget to test both branches:
>>> global_var = True
>>> if global_var:
... function()
... else:
... function()
[1, 2]
>>> global_var = False
>>> if global_var:
... function()
... else:
... function()
[1, 2, 3]
Step 2): realise that this is a stupid thing to do, and re-write the
doctest without the if test:
>>> global_var = True
>>> function()
[1, 2]
>>> global_var = False
>>> function()
[1, 2, 3]
Step 3): realise that this is a stupid design for a function, you're not
writing BASIC in 1976, and global variables are harmful. Redesign the
function to take an argument and re-write the doctest:
>>> function(True)
[1, 2]
>>> function(False)
[1, 2, 3]
And now you have good code and a good doctest.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Duncan Booth <duncan.booth@invalid.invalid> |
|---|---|
| Date | 2012-10-19 09:27 +0000 |
| Message-ID | <XnsA0F169B9B360Cduncanbooth@127.0.0.1> |
| In reply to | #31694 |
David <zhushenli@gmail.com> wrote:
> Hello, how to add if...else... switch to doctest?
> E.g. function outputs different value when global_var change.
>
> """
> if (global_var == True):
>>>> function()
> [1,2]
> else:
>>>> function()
> [1,2,3]
> """
>
> Thank you very much.
One other case the other replies don't seem to have covered:
If the global variable is determined by the environment, outside your
control, and by implication doesn't change while your program is running,
then you should use two separate functions:
if sys.platform=='win32':
def function():
"""
>>> function()
[1, 2]
"""
return [1, 2]
else:
def function():
"""
>>> function()
[1, 2, 3]
"""
return [1, 2, 3]
and if it's more than one such function use separate modules.
--
Duncan Booth http://kupuguy.blogspot.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web