/functional_tests/doc_tests/test_issue089/unwanted_package.rst

https://bitbucket.org/jpellerin/nose/ · ReStructuredText · 70 lines · 57 code · 13 blank · 0 comment · 0 complexity · 1e6133f09021cff93a265a44e2498de3 MD5 · raw file

  1. Excluding Unwanted Packages
  2. ---------------------------
  3. Normally, nose discovery descends into all packages. Plugins can
  4. change this behavior by implementing :meth:`IPluginInterface.wantDirectory()`.
  5. In this example, we have a wanted package called ``wanted_package``
  6. and an unwanted package called ``unwanted_package``.
  7. >>> import os
  8. >>> support = os.path.join(os.path.dirname(__file__), 'support')
  9. >>> support_files = [d for d in os.listdir(support)
  10. ... if not d.startswith('.')]
  11. >>> support_files.sort()
  12. >>> support_files
  13. ['unwanted_package', 'wanted_package']
  14. When we run nose normally, tests are loaded from both packages.
  15. .. Note ::
  16. The function :func:`nose.plugins.plugintest.run` reformats test result
  17. output to remove timings, which will vary from run to run, and
  18. redirects the output to stdout.
  19. >>> from nose.plugins.plugintest import run_buffered as run
  20. ..
  21. >>> argv = [__file__, '-v', support]
  22. >>> run(argv=argv) # doctest: +REPORT_NDIFF
  23. unwanted_package.test_spam.test_spam ... ok
  24. wanted_package.test_eggs.test_eggs ... ok
  25. <BLANKLINE>
  26. ----------------------------------------------------------------------
  27. Ran 2 tests in ...s
  28. <BLANKLINE>
  29. OK
  30. To exclude the tests in the unwanted package, we can write a simple
  31. plugin that implements :meth:`IPluginInterface.wantDirectory()` and returns ``False`` if
  32. the basename of the directory is ``"unwanted_package"``. This will
  33. prevent nose from descending into the unwanted package.
  34. >>> from nose.plugins import Plugin
  35. >>> class UnwantedPackagePlugin(Plugin):
  36. ... # no command line arg needed to activate plugin
  37. ... enabled = True
  38. ... name = "unwanted-package"
  39. ...
  40. ... def configure(self, options, conf):
  41. ... pass # always on
  42. ...
  43. ... def wantDirectory(self, dirname):
  44. ... want = None
  45. ... if os.path.basename(dirname) == "unwanted_package":
  46. ... want = False
  47. ... return want
  48. In the next test run we use the plugin, and the unwanted package is
  49. not discovered.
  50. >>> run(argv=argv,
  51. ... plugins=[UnwantedPackagePlugin()]) # doctest: +REPORT_NDIFF
  52. wanted_package.test_eggs.test_eggs ... ok
  53. <BLANKLINE>
  54. ----------------------------------------------------------------------
  55. Ran 1 test in ...s
  56. <BLANKLINE>
  57. OK