Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31701
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'url:pypi': 0.03; 'output': 0.04; '"""': 0.05; 'doctest': 0.07; 'framework.': 0.07; 'matches': 0.07; 'testing,': 0.07; 'python': 0.09; '[1,': 0.09; 'behave': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '*always*': 0.16; 'docstring': 0.16; 'finney': 0.16; 'foo()': 0.16; 'function()': 0.16; 'globals.': 0.16; 'outputs': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:Add': 0.16; 'certainly': 0.17; 'test.': 0.17; 'thorough': 0.17; 'tests': 0.18; '>>>': 0.18; 'feature': 0.24; 'testing': 0.24; 'pass': 0.25; 'header:User-Agent:1': 0.26; 'appear': 0.26; 'header:X-Complaints- To:1': 0.28; 'writes:': 0.29; 'e.g.': 0.30; 'function': 0.30; 'code': 0.31; 'url:python': 0.32; 'switch': 0.32; 'instead,': 0.33; 'much.': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'ben': 0.35; 'false': 0.35; 'subject:?': 0.35; 'something': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'generation': 0.36; 'test': 0.36; 'should': 0.36; 'thank': 0.36; 'passed': 0.37; 'subject:: ': 0.38; 'unit': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'hello,': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'your': 0.60; 'different': 0.63; 'subject:...': 0.63; 'more': 0.63; '8bit%:38': 0.65; 'matthew': 0.65; 'readers': 0.65; 'state,': 0.65; 'skip:\xe2 10': 0.66; 'complex,': 0.84; 'examples.': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Ben Finney <ben+python@benfinney.id.au> |
| Subject | Re: Add if...else... switch to doctest? |
| Date | Fri, 19 Oct 2012 12:29:25 +1100 |
| References | <c500a76c-3e6b-4dfc-97ca-8b24f0628620@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| X-Gmane-NNTP-Posting-Host | rasputin.madmonks.org |
| X-Public-Key-ID | 0xBD41714B |
| X-Public-Key-Fingerprint | 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B |
| X-Public-Key-URL | http://www.benfinney.id.au/contact/bfinney-gpg.asc |
| X-Post-From | Ben Finney <bignose+hates-spam@benfinney.id.au> |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) |
| Cancel-Lock | sha1:g/qIe2xjLv4UUMmP04RKMJiW7Fo= |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2487.1350610181.27098.python-list@python.org> (permalink) |
| Lines | 61 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1350610181 news.xs4all.nl 6858 [2001:888:2000:d::a6]:53473 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:31701 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web