1 Excluding Unwanted Packages
2 ---------------------------
4 Normally, nose discovery descends into all packages. Plugins can
5 change this behavior by implementing :meth:`IPluginInterface.wantDirectory()`.
7 In this example, we have a wanted package called ``wanted_package``
8 and an unwanted package called ``unwanted_package``.
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()
16 ['unwanted_package', 'wanted_package']
18 When we run nose normally, tests are loaded from both packages.
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.
26 >>> from nose.plugins.plugintest import run_buffered as run
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
35 ----------------------------------------------------------------------
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.
45 >>> from nose.plugins import Plugin
46 >>> class UnwantedPackagePlugin(Plugin):
47 ... # no command line arg needed to activate plugin
49 ... name = "unwanted-package"
51 ... def configure(self, options, conf):
54 ... def wantDirectory(self, dirname):
56 ... if os.path.basename(dirname) == "unwanted_package":
60 In the next test run we use the plugin, and the unwanted package is
64 ... plugins=[UnwantedPackagePlugin()]) # doctest: +REPORT_NDIFF
65 wanted_package.test_eggs.test_eggs ... ok
67 ----------------------------------------------------------------------