"""Implements nose test program and collector. """
RestrictedPluginManager
'TextTestRunner']
"""Test runner that uses nose's TextTestResult to enable errorClasses, as well as providing hooks for plugins to override or replace the test output stream, results, and the test case itself. """ config=None): config = Config()
self.descriptions, self.verbosity, self.config)
"""Overrides to provide plugin hooks and defer all output to the test result class. """ test = wrapper
# plugins can decorate or capture the output stream self.stream = wrapped
except KeyboardInterrupt: pass
"""Collect and run tests, returning success or failure.
The arguments to TestProgram() are the same as to :func:`main()` and :func:`run()`:
* module: All tests are in this module (default: None) * defaultTest: Tests to load (default: '.') * argv: Command line arguments (default: None; sys.argv is read) * testRunner: Test runner instance (default: None) * testLoader: Test loader instance (default: None) * env: Environment; ignored if config is provided (default: None; os.environ is read) * config: :class:`nose.config.Config` instance (default: None) * suite: Suite or list of tests to run (default: None). Passing a suite or lists of tests will bypass all test discovery and loading. *ALSO NOTE* that if you pass a unittest.TestSuite instance as the suite, context fixtures at the class, module and package level will not be used, and many plugin hooks will not be called. If you want normal nose behavior, either pass a list of tests, or a fully-configured :class:`nose.suite.ContextSuite`. * exit: Exit after running tests and printing report (default: True) * plugins: List of plugins to use; ignored if config is provided (default: load plugins with DefaultPluginManager) * addplugins: List of **extra** plugins to use. Pass a list of plugin instances in this argument to make custom plugins available while still using the DefaultPluginManager. """
testRunner=None, testLoader=None, env=None, config=None, suite=None, exit=True, plugins=None, addplugins=None): config.plugins.addPlugins(extraplugins=addplugins) self, module=module, defaultTest=defaultTest, argv=argv, testRunner=testRunner, testLoader=testLoader, **extra_args)
return [] else:
"""Load a Config, pre-filled with user config files if any are found. """ manager = PluginManager(plugins=plugins) else: env=env, files=cfg_files, plugins=manager)
"""Parse argv and env and configure running environment. """
# quick outs: version, plugins (optparse would have already # caught and exited on help) from nose import __version__ sys.stdout = sys.__stdout__ print("%s version %s" % (os.path.basename(sys.argv[0]), __version__)) sys.exit(0)
self.showPlugins() sys.exit(0)
elif isclass(self.testLoader): self.testLoader = self.testLoader(config=self.config) self.testLoader = plug_loader
# FIXME if self.module is a string, add it to self.testNames? not sure
else: self.testNames = tolist(self.defaultTest)
"""Create the tests to run. If a self.suite is set, then that suite will be used. Otherwise, tests will be loaded from the given test names (self.testNames) using the test loader. """ # We were given an explicit suite to run. Make sure it's # loaded and wrapped correctly. self.test = self.testLoader.suiteClass(self.suite) else:
"""Run Tests. Returns true on success, false on failure, and sets self.success to the same value. """ verbosity=self.config.verbosity, config=self.config) self.testRunner = plug_runner return self.success
"""Print list of available plugins. """ import textwrap
class DummyParser: def __init__(self): self.options = [] def add_option(self, *arg, **kw): self.options.append((arg, kw.pop('help', '')))
v = self.config.verbosity self.config.plugins.sort() for p in self.config.plugins: print("Plugin %s" % p.name) if v >= 2: print(" score: %s" % p.score) print('\n'.join(textwrap.wrap(p.help().strip(), initial_indent=' ', subsequent_indent=' '))) if v >= 3: parser = DummyParser() p.addOptions(parser) if len(parser.options): print() print(" Options:") for opts, help in parser.options: print(' %s' % (', '.join(opts))) if help: print('\n'.join( textwrap.wrap(help.strip(), initial_indent=' ', subsequent_indent=' '))) print()
os.path.dirname(__file__), 'usage.txt')) except AttributeError: f = open(os.path.join( os.path.dirname(__file__), 'usage.txt'), 'r') try: text = f.read() finally: f.close() # Ensure that we return str, not bytes.
# backwards compatibility
"""Collect and run tests, returning success or failure.
The arguments to `run()` are the same as to `main()`:
* module: All tests are in this module (default: None) * defaultTest: Tests to load (default: '.') * argv: Command line arguments (default: None; sys.argv is read) * testRunner: Test runner instance (default: None) * testLoader: Test loader instance (default: None) * env: Environment; ignored if config is provided (default: None; os.environ is read) * config: :class:`nose.config.Config` instance (default: None) * suite: Suite or list of tests to run (default: None). Passing a suite or lists of tests will bypass all test discovery and loading. *ALSO NOTE* that if you pass a unittest.TestSuite instance as the suite, context fixtures at the class, module and package level will not be used, and many plugin hooks will not be called. If you want normal nose behavior, either pass a list of tests, or a fully-configured :class:`nose.suite.ContextSuite`. * plugins: List of plugins to use; ignored if config is provided (default: load plugins with DefaultPluginManager) * addplugins: List of **extra** plugins to use. Pass a list of plugin instances in this argument to make custom plugins available while still using the DefaultPluginManager.
With the exception that the ``exit`` argument is always set to False. """ kw['exit'] = False return TestProgram(*arg, **kw).success
"""Collect and run tests in a single module only. Defaults to running tests in __main__. Additional arguments to TestProgram may be passed as keyword arguments. """ main(defaultTest=name, **kw)
"""TestSuite replacement entry point. Use anywhere you might use a unittest.TestSuite. The collector will, by default, load options from all config files and execute loader.loadTestsFromNames() on the configured testNames, or '.' if no testNames are configured. """ # plugins that implement any of these methods are disabled, since # we don't control the test runner and won't be able to run them # finalize() is also not called, but plugins that use it aren't disabled, # because capture needs it. setuptools_incompat = ('report', 'prepareTest', 'prepareTestLoader', 'prepareTestRunner', 'setOutputStream')
plugins = RestrictedPluginManager(exclude=setuptools_incompat) conf = Config(files=all_config_files(), plugins=plugins) conf.configure(argv=['collector']) loader = defaultTestLoader(conf)
if conf.testNames: suite = loader.loadTestsFromNames(conf.testNames) else: suite = loader.loadTestsFromNames(('.',)) return FinalizingSuiteWrapper(suite, plugins.finalize)
if __name__ == '__main__': main() |