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