test_ipunittest.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. """Tests for IPython's test support utilities.
  2. These are decorators that allow standalone functions and docstrings to be seen
  3. as tests by unittest, replicating some of nose's functionality. Additionally,
  4. IPython-syntax docstrings can be auto-converted to '>>>' so that ipython
  5. sessions can be copy-pasted as tests.
  6. This file can be run as a script, and it will call unittest.main(). We must
  7. check that it works with unittest as well as with nose...
  8. Notes:
  9. - Using nosetests --with-doctest --doctest-tests testfile.py
  10. will find docstrings as tests wherever they are, even in methods. But
  11. if we use ipython syntax in the docstrings, they must be decorated with
  12. @ipdocstring. This is OK for test-only code, but not for user-facing
  13. docstrings where we want to keep the ipython syntax.
  14. - Using nosetests --with-doctest file.py
  15. also finds doctests if the file name doesn't have 'test' in it, because it is
  16. treated like a normal module. But if nose treats the file like a test file,
  17. then for normal classes to be doctested the extra --doctest-tests is
  18. necessary.
  19. - running this script with python (it has a __main__ section at the end) misses
  20. one docstring test, the one embedded in the Foo object method. Since our
  21. approach relies on using decorators that create standalone TestCase
  22. instances, it can only be used for functions, not for methods of objects.
  23. Authors
  24. -------
  25. - Fernando Perez <Fernando.Perez@berkeley.edu>
  26. """
  27. #-----------------------------------------------------------------------------
  28. # Copyright (C) 2009-2011 The IPython Development Team
  29. #
  30. # Distributed under the terms of the BSD License. The full license is in
  31. # the file COPYING, distributed as part of this software.
  32. #-----------------------------------------------------------------------------
  33. #-----------------------------------------------------------------------------
  34. # Imports
  35. #-----------------------------------------------------------------------------
  36. from IPython.testing.ipunittest import ipdoctest, ipdocstring
  37. from IPython.utils.py3compat import doctest_refactor_print
  38. #-----------------------------------------------------------------------------
  39. # Test classes and functions
  40. #-----------------------------------------------------------------------------
  41. @ipdoctest
  42. @doctest_refactor_print
  43. def simple_dt():
  44. """
  45. >>> print 1+1
  46. 2
  47. """
  48. @ipdoctest
  49. @doctest_refactor_print
  50. def ipdt_flush():
  51. """
  52. In [20]: print 1
  53. 1
  54. In [26]: for i in range(4):
  55. ....: print i
  56. ....:
  57. ....:
  58. 0
  59. 1
  60. 2
  61. 3
  62. In [27]: 3+4
  63. Out[27]: 7
  64. """
  65. @ipdoctest
  66. @doctest_refactor_print
  67. def ipdt_indented_test():
  68. """
  69. In [20]: print 1
  70. 1
  71. In [26]: for i in range(4):
  72. ....: print i
  73. ....:
  74. ....:
  75. 0
  76. 1
  77. 2
  78. 3
  79. In [27]: 3+4
  80. Out[27]: 7
  81. """
  82. class Foo(object):
  83. """For methods, the normal decorator doesn't work.
  84. But rewriting the docstring with ip2py does, *but only if using nose
  85. --with-doctest*. Do we want to have that as a dependency?
  86. """
  87. @ipdocstring
  88. @doctest_refactor_print
  89. def ipdt_method(self):
  90. """
  91. In [20]: print 1
  92. 1
  93. In [26]: for i in range(4):
  94. ....: print i
  95. ....:
  96. ....:
  97. 0
  98. 1
  99. 2
  100. 3
  101. In [27]: 3+4
  102. Out[27]: 7
  103. """
  104. @doctest_refactor_print
  105. def normaldt_method(self):
  106. """
  107. >>> print 1+1
  108. 2
  109. """