/doc/writing_tests.rst

https://bitbucket.org/jpellerin/nose/ · ReStructuredText · 172 lines · 131 code · 41 blank · 0 comment · 0 complexity · 6255d87a359de1dc0d8473c7e7cd8e19 MD5 · raw file

  1. Writing tests
  2. -------------
  3. As with py.test_, nose tests need not be subclasses of
  4. :class:`unittest.TestCase`. Any function or class that matches the configured
  5. testMatch regular expression (``(?:^|[\\b_\\.-])[Tt]est)`` by default -- that
  6. is, has test or Test at a word boundary or following a - or _) and lives in a
  7. module that also matches that expression will be run as a test. For the sake
  8. of compatibility with legacy unittest test cases, nose will also load tests
  9. from :class:`unittest.TestCase` subclasses just like unittest does. Like
  10. py.test, nose runs functional tests in the order in which they appear in the
  11. module file. TestCase-derived tests and other test classes are run in
  12. alphabetical order.
  13. .. _py.test: http://codespeak.net/py/current/doc/test.html
  14. .. _fixtures:
  15. Fixtures
  16. ========
  17. nose supports fixtures (setup and teardown methods) at the package,
  18. module, class, and test level. As with py.test or unittest fixtures,
  19. setup always runs before any test (or collection of tests for test
  20. packages and modules); teardown runs if setup has completed
  21. successfully, regardless of the status of the test run. For more detail
  22. on fixtures at each level, see below.
  23. Test packages
  24. =============
  25. nose allows tests to be grouped into test packages. This allows
  26. package-level setup; for instance, if you need to create a test database
  27. or other data fixture for your tests, you may create it in package setup
  28. and remove it in package teardown once per test run, rather than having to
  29. create and tear it down once per test module or test case.
  30. To create package-level setup and teardown methods, define setup and/or
  31. teardown functions in the ``__init__.py`` of a test package. Setup methods may
  32. be named `setup`, `setup_package`, `setUp`, or `setUpPackage`; teardown may
  33. be named `teardown`, `teardown_package`, `tearDown` or `tearDownPackage`.
  34. Execution of tests in a test package begins as soon as the first test
  35. module is loaded from the test package.
  36. Test modules
  37. ============
  38. A test module is a python module that matches the testMatch regular
  39. expression. Test modules offer module-level setup and teardown; define the
  40. method `setup`, `setup_module`, `setUp` or `setUpModule` for setup,
  41. `teardown`, `teardown_module`, or `tearDownModule` for teardown. Execution
  42. of tests in a test module begins after all tests are collected.
  43. Test classes
  44. ============
  45. A test class is a class defined in a test module that matches testMatch or is
  46. a subclass of :class:`unittest.TestCase`. All test classes are run the same
  47. way: Methods in the class that match testMatch are discovered, and a test
  48. case is constructed to run each method with a fresh instance of the test
  49. class. Like :class:`unittest.TestCase` subclasses, other test classes can
  50. define setUp and tearDown methods that will be run before and after each test
  51. method. Test classes that do not descend from `unittest.TestCase` may also
  52. include generator methods and class-level fixtures. Class-level setup fixtures
  53. may be named `setup_class`, `setupClass`, `setUpClass`, `setupAll` or
  54. `setUpAll`; teardown fixtures may be named `teardown_class`, `teardownClass`,
  55. `tearDownClass`, `teardownAll` or `tearDownAll`. Class-level setup and teardown
  56. fixtures must be class methods.
  57. Test functions
  58. ==============
  59. Any function in a test module that matches testMatch will be wrapped in a
  60. `FunctionTestCase` and run as a test. The simplest possible failing test is
  61. therefore::
  62. def test():
  63. assert False
  64. And the simplest passing test::
  65. def test():
  66. pass
  67. Test functions may define setup and/or teardown attributes, which will be
  68. run before and after the test function, respectively. A convenient way to
  69. do this, especially when several test functions in the same module need
  70. the same setup, is to use the provided `with_setup` decorator::
  71. def setup_func():
  72. "set up test fixtures"
  73. def teardown_func():
  74. "tear down test fixtures"
  75. @with_setup(setup_func, teardown_func)
  76. def test():
  77. "test ..."
  78. For python 2.3 or earlier, add the attributes by calling the decorator
  79. function like so::
  80. def test():
  81. "test ... "
  82. test = with_setup(setup_func, teardown_func)(test)
  83. or by direct assignment::
  84. test.setup = setup_func
  85. test.teardown = teardown_func
  86. Please note that `with_setup` is useful *only* for test functions, not
  87. for test methods in `unittest.TestCase` subclasses or other test
  88. classes. For those cases, define `setUp` and `tearDown` methods in the
  89. class.
  90. Test generators
  91. ===============
  92. nose supports test functions and methods that are generators. A simple
  93. example from nose's selftest suite is probably the best explanation::
  94. def test_evens():
  95. for i in range(0, 5):
  96. yield check_even, i, i*3
  97. def check_even(n, nn):
  98. assert n % 2 == 0 or nn % 2 == 0
  99. This will result in five tests. nose will iterate the generator, creating a
  100. function test case wrapper for each tuple it yields. As in the example, test
  101. generators must yield tuples, the first element of which must be a callable
  102. and the remaining elements the arguments to be passed to the callable.
  103. By default, the test name output for a generated test in verbose mode
  104. will be the name of the generator function or method, followed by the
  105. args passed to the yielded callable. If you want to show a different test
  106. name, set the ``description`` attribute of the yielded callable.
  107. Setup and teardown functions may be used with test generators. However, please
  108. note that setup and teardown attributes attached to the *generator function*
  109. will execute only once. To *execute fixtures for each yielded test*, attach
  110. the setup and teardown attributes to the function that is yielded, or yield a
  111. callable object instance with setup and teardown attributes.
  112. For example::
  113. @with_setup(setup_func, teardown_func)
  114. def test_generator():
  115. # ...
  116. yield func, arg, arg # ...
  117. Here, the setup and teardown functions will be executed *once*. Compare to::
  118. def test_generator():
  119. # ...
  120. yield func, arg, arg # ...
  121. @with_setup(setup_func, teardown_func)
  122. def func(arg):
  123. assert something_about(arg)
  124. In the latter case the setup and teardown functions will execute once for each
  125. yielded test.
  126. For generator methods, the setUp and tearDown methods of the class (if any)
  127. will be run before and after each generated test case. The setUp and tearDown
  128. methods *do not* run before the generator method itself, as this would cause
  129. setUp to run twice before the first test without an intervening tearDown.
  130. Please note that method generators *are not* supported in `unittest.TestCase`
  131. subclasses.