PageRenderTime 26ms CodeModel.GetById 16ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 1Excluding Unwanted Packages
 2---------------------------
 3
 4Normally, nose discovery descends into all packages. Plugins can
 5change this behavior by implementing :meth:`IPluginInterface.wantDirectory()`.
 6
 7In this example, we have a wanted package called ``wanted_package``
 8and 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
18When 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
40To exclude the tests in the unwanted package, we can write a simple
41plugin that implements :meth:`IPluginInterface.wantDirectory()` and returns ``False`` if
42the basename of the directory is ``"unwanted_package"``. This will
43prevent 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
60In the next test run we use the plugin, and the unwanted package is
61not 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