コード例 #1
0
ファイル: main.py プロジェクト: tsingjinyun/Theano
    def prepare_test_args(self,
                          verbose=1,
                          extra_argv=None,
                          coverage=False,
                          capture=True,
                          knownfailure=True):
        """
        Prepare arguments for the `test` method.

        Takes the same arguments as `test`.
        """
        # fail with nice error message if nose is not present
        nose = import_nose()

        # compile argv
        argv = self._test_argv(verbose, extra_argv)

        # numpy way of doing coverage
        if coverage:
            argv += [
                '--cover-package=%s' % self.package_name, '--with-coverage',
                '--cover-tests', '--cover-inclusive', '--cover-erase'
            ]

        # Capture output only if needed
        if not capture:
            argv += ['-s']

        # construct list of plugins
        plugins = []
        if knownfailure:
            plugins.append(KnownFailure())
        plugins += [p() for p in nose.plugins.builtin.plugins]

        return argv, plugins
コード例 #2
0
def main_function():
    # Handle the --theano arguments
    if "--theano" in sys.argv:
        i = sys.argv.index("--theano")
        import theano
        sys.argv[i] = theano.__path__[0]

    # Many Theano tests suppose device=cpu, so we need to raise an
    # error if device==gpu.
    # I don't know how to do this check only if we use theano-nose on
    # Theano tests.  So I make an try..except in case the script get
    # reused elsewhere.
    # We should not import theano before call nose.main()
    # As this cause import problem with nosetests.
    # Should we find a way to don't modify sys.path?
    if not os.path.exists('theano/__init__.py'):
        try:
            from theano import config
            if config.device != "cpu":
                raise ValueError(
                    "Theano tests must be run with device=cpu."
                    " This will also run GPU tests when possible.\n"
                    " If you want GPU-related tests to run on a"
                    " specific GPU device, and not the default one,"
                    " you should use the init_gpu_device theano flag.")
        except ImportError:
            pass

    # Handle --batch[=n] arguments
    batch_args = [arg for arg in sys.argv if arg.startswith('--batch')]
    for arg in batch_args:
        sys.argv.remove(arg)
    batch_size = None
    if len(batch_args):
        if len(batch_args) > 1:
            _logger.warn(
                'Multiple --batch arguments detected, using the last one '
                'and ignoring the first ones.')

        batch_arg = batch_args[-1]
        elems = batch_arg.split('=', 1)
        if len(elems) == 2:
            batch_size = int(elems[1])

    # Handle the --debug-batch argument.
    display_batch_output = False
    if '--debug-batch' in sys.argv:
        if not batch_args:
            raise AssertionError(
                'You can only use the --debug-batch argument with the '
                '--batch[=n] option')
        while '--debug-batch' in sys.argv:
            sys.argv.remove('--debug-batch')
        sys.argv += ['--verbose', '--nocapture', '--detailed-errors']
        display_batch_output = True

    # Handle --time_prof arguments
    time_prof_args = [arg for arg in sys.argv if arg == '--time-profile']
    for arg in time_prof_args:
        sys.argv.remove(arg)

    # Time-profiling and batch modes
    if time_prof_args or batch_args:
        from theano.tests import run_tests_in_batch
        return run_tests_in_batch.main(
            theano_nose=os.path.realpath(__file__),
            batch_size=batch_size,
            time_profile=bool(time_prof_args),
            display_batch_output=display_batch_output)

    # Non-batch mode.
    addplugins = []
    # We include KnownFailure plugin by default, unless
    # it is disabled by the "--without-knownfailure" arg.
    if '--without-knownfailure' not in sys.argv:
        try:
            from numpy.testing.noseclasses import KnownFailure
            addplugins.append(KnownFailure())
        except ImportError:
            _logger.warn(
                'KnownFailure plugin from NumPy could not be imported. '
                'Use --without-knownfailure to disable this warning.')
    else:
        sys.argv.remove('--without-knownfailure')

    # When 'theano-nose' is called-back under the time-profile option, an
    # instance of the custom Nosetests plugin class 'DisabDocString' (see
    # below) is loaded. The latter ensures that the test name will not be
    # replaced in display by the first line of the documentation string.
    if '--disabdocstring' in sys.argv:
        addplugins.append(DisabDocString())

    try:
        if addplugins:
            ret = nose.main(addplugins=addplugins)
        else:
            ret = nose.main()
        return ret
    except TypeError as e:
        if "got an unexpected keyword argument 'addplugins'" in e.message:
            # This means nose is too old and does not support plugins
            _logger.warn('KnownFailure plugin from NumPy can\'t'
                         ' be used as nosetests is too old. '
                         'Use --without-knownfailure to disable this warning.')
            nose.main()
        else:
            raise