def _test_argv(self, label, verbose, extra_argv):
''' Generate argv for nosetest command
Parameters
----------
label : {'fast', 'full', '', attribute identifier}, optional
see ``test`` docstring
verbose : int, optional
Verbosity value for test outputs, in the range 1-10. Default is 1.
extra_argv : list, optional
List with any extra arguments to pass to nosetests.
Returns
-------
argv : list
command line arguments that will be passed to nose
'''
argv = [__file__, self.package_path, '-s']
if label and label != 'full':
if not isinstance(label, basestring):
raise TypeError('Selection label should be a string')
if label == 'fast':
label = 'not slow'
argv += ['-A', label]
argv += ['--verbosity', str(verbose)]
# When installing with setuptools, and also in some other cases, the
# test_*.py files end up marked +x executable. Nose, by default, does
# not run files marked with +x as they might be scripts. However, in
# our case nose only looks for test_*.py files under the package
# directory, which should be safe.
argv += ['--exe']
if extra_argv:
argv += extra_argv
return argv
python类run()的实例源码
def run(*arg, **kw):
"""
Specialized version of nose.run for use inside of doctests that
test test runs.
This version of run() prints the result output to stdout. Before
printing, the output is processed by replacing the timing
information with an ellipsis (...), removing traceback stacks, and
removing trailing whitespace.
Use this version of run wherever you are writing a doctest that
tests nose (or unittest) test result output.
Note: do not use doctest: +ELLIPSIS when testing nose output,
since ellipses ("test_foo ... ok") in your expected test runner
output may match multiple lines of output, causing spurious test
passes!
"""
from nose import run
from nose.config import Config
from nose.plugins.manager import PluginManager
buffer = Buffer()
if 'config' not in kw:
plugins = kw.pop('plugins', [])
if isinstance(plugins, list):
plugins = PluginManager(plugins=plugins)
env = kw.pop('env', {})
kw['config'] = Config(env=env, plugins=plugins)
if 'argv' not in kw:
kw['argv'] = ['nosetests', '-v']
kw['config'].stream = buffer
# Set up buffering so that all output goes to our buffer,
# or warn user if deprecated behavior is active. If this is not
# done, prints and warnings will either be out of place or
# disappear.
stderr = sys.stderr
stdout = sys.stdout
if kw.pop('buffer_all', False):
sys.stdout = sys.stderr = buffer
restore = True
else:
restore = False
warn("The behavior of nose.plugins.plugintest.run() will change in "
"the next release of nose. The current behavior does not "
"correctly account for output to stdout and stderr. To enable "
"correct behavior, use run_buffered() instead, or pass "
"the keyword argument buffer_all=True to run().",
DeprecationWarning, stacklevel=2)
try:
run(*arg, **kw)
finally:
if restore:
sys.stderr = stderr
sys.stdout = stdout
out = buffer.getvalue()
print munge_nose_output_for_doctest(out)
def run_module_suite(file_to_run=None, argv=None):
"""
Run a test module.
Equivalent to calling ``$ nosetests <argv> <file_to_run>`` from
the command line
Parameters
----------
file_to_run : str, optional
Path to test module, or None.
By default, run the module from which this function is called.
argv : list of strings
Arguments to be passed to the nose test runner. ``argv[0]`` is
ignored. All command line arguments accepted by ``nosetests``
will work. If it is the default value None, sys.argv is used.
.. versionadded:: 1.9.0
Examples
--------
Adding the following::
if __name__ == "__main__" :
run_module_suite(argv=sys.argv)
at the end of a test module will run the tests when that module is
called in the python interpreter.
Alternatively, calling::
>>> run_module_suite(file_to_run="numpy/tests/test_matlib.py")
from an interpreter will run all the test routine in 'test_matlib.py'.
"""
if file_to_run is None:
f = sys._getframe(1)
file_to_run = f.f_locals.get('__file__', None)
if file_to_run is None:
raise AssertionError
if argv is None:
argv = sys.argv + [file_to_run]
else:
argv = argv + [file_to_run]
nose = import_nose()
from .noseclasses import KnownFailurePlugin
nose.run(argv=argv, addplugins=[KnownFailurePlugin()])
def run_module_suite(file_to_run=None, argv=None):
"""
Run a test module.
Equivalent to calling ``$ nosetests <argv> <file_to_run>`` from
the command line
Parameters
----------
file_to_run : str, optional
Path to test module, or None.
By default, run the module from which this function is called.
argv : list of strings
Arguments to be passed to the nose test runner. ``argv[0]`` is
ignored. All command line arguments accepted by ``nosetests``
will work. If it is the default value None, sys.argv is used.
.. versionadded:: 1.9.0
Examples
--------
Adding the following::
if __name__ == "__main__" :
run_module_suite(argv=sys.argv)
at the end of a test module will run the tests when that module is
called in the python interpreter.
Alternatively, calling::
>>> run_module_suite(file_to_run="numpy/tests/test_matlib.py")
from an interpreter will run all the test routine in 'test_matlib.py'.
"""
if file_to_run is None:
f = sys._getframe(1)
file_to_run = f.f_locals.get('__file__', None)
if file_to_run is None:
raise AssertionError
if argv is None:
argv = sys.argv + [file_to_run]
else:
argv = argv + [file_to_run]
nose = import_nose()
from .noseclasses import KnownFailurePlugin
nose.run(argv=argv, addplugins=[KnownFailurePlugin()])
def run(*arg, **kw):
"""
Specialized version of nose.run for use inside of doctests that
test test runs.
This version of run() prints the result output to stdout. Before
printing, the output is processed by replacing the timing
information with an ellipsis (...), removing traceback stacks, and
removing trailing whitespace.
Use this version of run wherever you are writing a doctest that
tests nose (or unittest) test result output.
Note: do not use doctest: +ELLIPSIS when testing nose output,
since ellipses ("test_foo ... ok") in your expected test runner
output may match multiple lines of output, causing spurious test
passes!
"""
from nose import run
from nose.config import Config
from nose.plugins.manager import PluginManager
buffer = Buffer()
if 'config' not in kw:
plugins = kw.pop('plugins', [])
if isinstance(plugins, list):
plugins = PluginManager(plugins=plugins)
env = kw.pop('env', {})
kw['config'] = Config(env=env, plugins=plugins)
if 'argv' not in kw:
kw['argv'] = ['nosetests', '-v']
kw['config'].stream = buffer
# Set up buffering so that all output goes to our buffer,
# or warn user if deprecated behavior is active. If this is not
# done, prints and warnings will either be out of place or
# disappear.
stderr = sys.stderr
stdout = sys.stdout
if kw.pop('buffer_all', False):
sys.stdout = sys.stderr = buffer
restore = True
else:
restore = False
warn("The behavior of nose.plugins.plugintest.run() will change in "
"the next release of nose. The current behavior does not "
"correctly account for output to stdout and stderr. To enable "
"correct behavior, use run_buffered() instead, or pass "
"the keyword argument buffer_all=True to run().",
DeprecationWarning, stacklevel=2)
try:
run(*arg, **kw)
finally:
if restore:
sys.stderr = stderr
sys.stdout = stdout
out = buffer.getvalue()
print munge_nose_output_for_doctest(out)
nosetester.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def run_module_suite(file_to_run=None, argv=None):
"""
Run a test module.
Equivalent to calling ``$ nosetests <argv> <file_to_run>`` from
the command line
Parameters
----------
file_to_run : str, optional
Path to test module, or None.
By default, run the module from which this function is called.
argv : list of strings
Arguments to be passed to the nose test runner. ``argv[0]`` is
ignored. All command line arguments accepted by ``nosetests``
will work. If it is the default value None, sys.argv is used.
.. versionadded:: 1.9.0
Examples
--------
Adding the following::
if __name__ == "__main__" :
run_module_suite(argv=sys.argv)
at the end of a test module will run the tests when that module is
called in the python interpreter.
Alternatively, calling::
>>> run_module_suite(file_to_run="numpy/tests/test_matlib.py")
from an interpreter will run all the test routine in 'test_matlib.py'.
"""
if file_to_run is None:
f = sys._getframe(1)
file_to_run = f.f_locals.get('__file__', None)
if file_to_run is None:
raise AssertionError
if argv is None:
argv = sys.argv + [file_to_run]
else:
argv = argv + [file_to_run]
nose = import_nose()
from .noseclasses import KnownFailure
nose.run(argv=argv, addplugins=[KnownFailure()])
def run_module_suite(file_to_run=None, argv=None):
"""
Run a test module.
Equivalent to calling ``$ nosetests <argv> <file_to_run>`` from
the command line
Parameters
----------
file_to_run : str, optional
Path to test module, or None.
By default, run the module from which this function is called.
argv : list of strings
Arguments to be passed to the nose test runner. ``argv[0]`` is
ignored. All command line arguments accepted by ``nosetests``
will work. If it is the default value None, sys.argv is used.
.. versionadded:: 1.9.0
Examples
--------
Adding the following::
if __name__ == "__main__" :
run_module_suite(argv=sys.argv)
at the end of a test module will run the tests when that module is
called in the python interpreter.
Alternatively, calling::
>>> run_module_suite(file_to_run="numpy/tests/test_matlib.py")
from an interpreter will run all the test routine in 'test_matlib.py'.
"""
if file_to_run is None:
f = sys._getframe(1)
file_to_run = f.f_locals.get('__file__', None)
if file_to_run is None:
raise AssertionError
if argv is None:
argv = sys.argv + [file_to_run]
else:
argv = argv + [file_to_run]
nose = import_nose()
from .noseclasses import KnownFailure
nose.run(argv=argv, addplugins=[KnownFailure()])
def run_module_suite(file_to_run=None, argv=None):
"""
Run a test module.
Equivalent to calling ``$ nosetests <argv> <file_to_run>`` from
the command line
Parameters
----------
file_to_run : str, optional
Path to test module, or None.
By default, run the module from which this function is called.
argv : list of strings
Arguments to be passed to the nose test runner. ``argv[0]`` is
ignored. All command line arguments accepted by ``nosetests``
will work. If it is the default value None, sys.argv is used.
.. versionadded:: 1.9.0
Examples
--------
Adding the following::
if __name__ == "__main__" :
run_module_suite(argv=sys.argv)
at the end of a test module will run the tests when that module is
called in the python interpreter.
Alternatively, calling::
>>> run_module_suite(file_to_run="numpy/tests/test_matlib.py")
from an interpreter will run all the test routine in 'test_matlib.py'.
"""
if file_to_run is None:
f = sys._getframe(1)
file_to_run = f.f_locals.get('__file__', None)
if file_to_run is None:
raise AssertionError
if argv is None:
argv = sys.argv + [file_to_run]
else:
argv = argv + [file_to_run]
nose = import_nose()
from .noseclasses import KnownFailurePlugin
nose.run(argv=argv, addplugins=[KnownFailurePlugin()])
def __init__(self, name, app):
"""
Init command
:param name: Name of the command
:type name: str
:param app: The application
:type app: edmunds.application.Application
"""
super(TestCommand, self).__init__(name, app)
# Fetch nose options
config = Config(env=os.environ, files=all_config_files(), plugins=DefaultPluginManager())
nose_options = config.getParser(doc=TestProgram.usage()).option_list
# Override run-function to be able to load the click-options dynamically
# Dynamic click.option does not work for class-methods because of __click_params__
original_function = self.run
def run_wrapper(**kwargs):
return original_function(**kwargs)
self.run = run_wrapper
# Don't show --help
nose_options = filter(lambda nose_option: '--help' not in nose_option._long_opts, nose_options)
for nose_option in nose_options:
args = nose_option._short_opts + nose_option._long_opts
if not args:
continue
type_mapping = {
'string': str,
'int': int,
'long': int,
'float': float,
# 'complex': str,
# 'choice': str,
}
unsupported_attr = ['action', 'dest', 'const']
kwargs = {}
for attr in OptParseOption.ATTRS:
if attr in unsupported_attr:
continue
attr_value = getattr(nose_option, attr)
if attr_value is None:
continue
if attr == 'type':
attr_value = type_mapping[attr_value]
if nose_option.nargs > 1:
attr_value = click.Tuple([attr_value])
if attr == 'default':
if attr_value == optparse.NO_DEFAULT:
continue
kwargs[attr] = attr_value
click.option(*args[:2], **kwargs)(run_wrapper)
def get_nose_runner(report_folder, parallel=True, process_timeout=600, process_restart=True,
xunit_report=False, exclude_integration=True, code_coverage=False):
"""Create a nose execution method"""
def _run_nose(name, working_dir):
import nose
import os.path
if not report_folder \
or not os.path.exists(report_folder) \
or not os.path.isdir(report_folder):
raise ValueError('Report folder {} does not exist'.format(report_folder))
arguments = ['-w', working_dir, '-v']
if parallel:
arguments += ['--processes=-1', '--process-timeout={}'.format(process_timeout)]
if process_restart:
arguments += ['--process-restartworker']
if xunit_report:
log_file = os.path.join(report_folder, name + '-report.xml')
arguments += ['--with-xunit', '--xunit-file', log_file]
else:
log_file = ''
if exclude_integration:
arguments += ['--ignore-files=integration*']
if code_coverage:
# coverage_config = os.path.join(os.path.dirname(__file__), '.coveragerc')
# coverage_folder = os.path.join(report_folder, 'code_coverage')
# make_dirs(coverage_folder)
# if not os.path.exists(coverage_folder) or not os.path.isdir(coverage_folder):
# raise Exception('Failed to create folder {} for code coverage result'
# .format(coverage_folder))
arguments += ['--with-coverage']
debug_file = os.path.join(report_folder, name + '-debug.log')
arguments += ['--debug-log={}'.format(debug_file)]
print('\n')
print('<<< Run {} >>>'.format(name))
start = datetime.now()
result = nose.run(argv=arguments)
end = datetime.now()
return result, start, end, log_file
return _run_nose
def run_module_suite(file_to_run=None, argv=None):
"""
Run a test module.
Equivalent to calling ``$ nosetests <argv> <file_to_run>`` from
the command line
Parameters
----------
file_to_run : str, optional
Path to test module, or None.
By default, run the module from which this function is called.
argv : list of strings
Arguments to be passed to the nose test runner. ``argv[0]`` is
ignored. All command line arguments accepted by ``nosetests``
will work. If it is the default value None, sys.argv is used.
.. versionadded:: 1.9.0
Examples
--------
Adding the following::
if __name__ == "__main__" :
run_module_suite(argv=sys.argv)
at the end of a test module will run the tests when that module is
called in the python interpreter.
Alternatively, calling::
>>> run_module_suite(file_to_run="numpy/tests/test_matlib.py")
from an interpreter will run all the test routine in 'test_matlib.py'.
"""
if file_to_run is None:
f = sys._getframe(1)
file_to_run = f.f_locals.get('__file__', None)
if file_to_run is None:
raise AssertionError
if argv is None:
argv = sys.argv + [file_to_run]
else:
argv = argv + [file_to_run]
nose = import_nose()
from .noseclasses import KnownFailurePlugin
nose.run(argv=argv, addplugins=[KnownFailurePlugin()])