dtexample.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. """Simple example using doctests.
  2. This file just contains doctests both using plain python and IPython prompts.
  3. All tests should be loaded by nose.
  4. """
  5. from __future__ import print_function
  6. def pyfunc():
  7. """Some pure python tests...
  8. >>> pyfunc()
  9. 'pyfunc'
  10. >>> import os
  11. >>> 2+3
  12. 5
  13. >>> for i in range(3):
  14. ... print(i, end=' ')
  15. ... print(i+1, end=' ')
  16. ...
  17. 0 1 1 2 2 3
  18. """
  19. return 'pyfunc'
  20. def ipfunc():
  21. """Some ipython tests...
  22. In [1]: import os
  23. In [3]: 2+3
  24. Out[3]: 5
  25. In [26]: for i in range(3):
  26. ....: print(i, end=' ')
  27. ....: print(i+1, end=' ')
  28. ....:
  29. 0 1 1 2 2 3
  30. Examples that access the operating system work:
  31. In [1]: !echo hello
  32. hello
  33. In [2]: !echo hello > /tmp/foo_iptest
  34. In [3]: !cat /tmp/foo_iptest
  35. hello
  36. In [4]: rm -f /tmp/foo_iptest
  37. It's OK to use '_' for the last result, but do NOT try to use IPython's
  38. numbered history of _NN outputs, since those won't exist under the
  39. doctest environment:
  40. In [7]: 'hi'
  41. Out[7]: 'hi'
  42. In [8]: print(repr(_))
  43. 'hi'
  44. In [7]: 3+4
  45. Out[7]: 7
  46. In [8]: _+3
  47. Out[8]: 10
  48. In [9]: ipfunc()
  49. Out[9]: 'ipfunc'
  50. """
  51. return 'ipfunc'
  52. def ranfunc():
  53. """A function with some random output.
  54. Normal examples are verified as usual:
  55. >>> 1+3
  56. 4
  57. But if you put '# random' in the output, it is ignored:
  58. >>> 1+3
  59. junk goes here... # random
  60. >>> 1+2
  61. again, anything goes #random
  62. if multiline, the random mark is only needed once.
  63. >>> 1+2
  64. You can also put the random marker at the end:
  65. # random
  66. >>> 1+2
  67. # random
  68. .. or at the beginning.
  69. More correct input is properly verified:
  70. >>> ranfunc()
  71. 'ranfunc'
  72. """
  73. return 'ranfunc'
  74. def random_all():
  75. """A function where we ignore the output of ALL examples.
  76. Examples:
  77. # all-random
  78. This mark tells the testing machinery that all subsequent examples should
  79. be treated as random (ignoring their output). They are still executed,
  80. so if a they raise an error, it will be detected as such, but their
  81. output is completely ignored.
  82. >>> 1+3
  83. junk goes here...
  84. >>> 1+3
  85. klasdfj;
  86. >>> 1+2
  87. again, anything goes
  88. blah...
  89. """
  90. pass
  91. def iprand():
  92. """Some ipython tests with random output.
  93. In [7]: 3+4
  94. Out[7]: 7
  95. In [8]: print('hello')
  96. world # random
  97. In [9]: iprand()
  98. Out[9]: 'iprand'
  99. """
  100. return 'iprand'
  101. def iprand_all():
  102. """Some ipython tests with fully random output.
  103. # all-random
  104. In [7]: 1
  105. Out[7]: 99
  106. In [8]: print('hello')
  107. world
  108. In [9]: iprand_all()
  109. Out[9]: 'junk'
  110. """
  111. return 'iprand_all'