示例#1
0
 def printyaml(self):
     """Print the test YAML for jinja2 debugging purposes."""
     for test_module in self.test_modules:
         log("\n# {}\n{}\n".format(
             test_module.filename,
             test_module.test_yaml_text
         ))
示例#2
0
def cli(filenames, yaml, quiet, tags, settings, extra):
    """Run test files or entire directories containing .test files."""
    # .hitch/virtualenv/bin/python <- 4 directories up from where the python exec resides
    engine_folder = path.abspath(path.join(executable, "..", "..", "..", ".."))
    filenames = [path.abspath(path.relpath(filename, engine_folder)) for filename in filenames]
    chdir(engine_folder)

    if quiet:
        warn((
            "The --quiet switch has been deprecated. You can make your tests run quietly "
            "by setting the property quiet to True via --extra or in a settings file.\n\n"
            "See here for more details on the change : \n"
            "https://hitchtest.readthedocs.org/en/latest/faq/why_was_hitch_behavior_changed.html\n"
        ))
        exit(1)

    #new_default_settings_filename = path.join(engine_folder, "all.settings")
    #settings_filename = None if settings is None else path.join(engine_folder, settings)

    #if path.exists(new_default_settings_filename):
        #_overwrite_settings_with_yaml(new_default_settings_filename, settings_dict)

    #if settings_filename is not None:
        #if not path.exists(settings_filename):
            #warn("Settings file '{}' could not be found!\n".format(settings_filename))
            #exit(1)
        #else:
            ## Load settings from specified file, if it exists
            #_overwrite_settings_with_yaml(settings_filename, settings_dict)

    ## Load extra settings from command line JSON and overwrite what's already set
    #if extra is not None:
        #try:
            #settings_dict.update(json.loads(extra).items())
        #except ValueError as error:
            #warn("""{} in:\n ==> --extra '{}' (must be valid JSON)\n""".format(str(error), extra))
            #exit(1)
        #except AttributeError:
            #warn("""Error in:\n ==> --extra '{}' (must be valid JSON and not a list)\n""".format(extra))
            #exit(1)

    settings_dict = Settings(engine_folder, settings, extra)
    settings_dict['engine_folder'] = engine_folder
    if 'quiet' not in settings_dict:
        settings_dict['quiet'] = False

    if len(filenames) == 0:
        warn("No tests specified.\n")
        exit(1)

    # Get list of files from specified files/directories
    matches = []
    test_not_found = False
    for filename in filenames:
        if not path.exists(filename):
            warn("Test '{}' not found.\n".format(filename))
            test_not_found = True
        if path.isdir(filename):
            for root, dirnames, filenames_in_dir in walk(filename):
                for filename_in_dir in fnmatch.filter(filenames_in_dir, '*.test'):
                    if ".hitch" not in root: # Ignore everything in .hitch
                        matches.append(path.join(root, filename_in_dir))
        else:
            matches.append(filename)

    if test_not_found:
        exit(1)

    # Get list of modules from matching directly specified files from command line
    # and indirectly (in the directories of) directories specified from cmd line
    test_modules = []
    for filename in matches:
        if filename.endswith(".test"):
            test_modules.append(module.Module(filename, settings_dict))
        else:
            warn(
                "Tests must have the extension .test"
                "- '{}' doesn't have that extension\n".format(filename)
            )
            exit(1)

    test_suite = suite.Suite(test_modules, settings_dict, tags)

    if yaml:
        test_suite.printyaml()
    else:
        returned_results = test_suite.run(quiet=quiet)

        # Lines must be split to prevent stdout blocking
        result_lines = returned_results.to_template(
            template=settings_dict.get('results_template', None)
        ).split('\n')

        for line in result_lines:
            log("{}\n".format(line))

        exit(1 if len(returned_results.failures()) > 0 else 0)
示例#3
0
    def run(self):
        start_time = time.time()
        stacktrace = None
        engine = None
        failure = False
        show_hitch_stacktrace = self.settings.get("show_hitch_stacktrace", False)

        try:
            engine = self.engine_class(self.settings, self.preconditions)
            engine.name = self.name
            engine.aborted = False
            engine.stacktrace = None
            engine._test = self
        except Exception as e:
            stacktrace = HitchStacktrace(self, TestPosition.SETUP, show_hitch_stacktrace)
            failure = True

        if not failure:
            log("RUNNING TEST {}\n".format(self.name))

            try:
                verify(self.settings.get("environment", []))
                engine.set_up()
            except Exception as e:
                stacktrace = HitchStacktrace(self, TestPosition.SETUP, show_hitch_stacktrace)
                failure = True

            if not failure:
                for step in self.scenario.steps:
                    try:
                        step.run(engine)
                    except Exception as e:
                        stacktrace = HitchStacktrace(self, TestPosition.STEP, show_hitch_stacktrace, step=step)
                        failure = True
                        break

            if not engine.aborted:
                if failure:
                    try:
                        engine.stacktrace = stacktrace
                        engine.on_failure()
                    except Exception as e:
                        stacktrace = HitchStacktrace(self, TestPosition.ON_FAILURE, show_hitch_stacktrace)
                else:
                    try:
                        engine.on_success()
                    except Exception as e:
                        failure = True
                        stacktrace = HitchStacktrace(self, TestPosition.ON_SUCCESS, show_hitch_stacktrace)

            try:
                engine.tear_down()
            except Exception as e:
                failure = True
                stacktrace = HitchStacktrace(self, TestPosition.TEARDOWN, show_hitch_stacktrace)

        duration = time.time() - start_time
        dict_stacktrace = stacktrace.to_dict() if stacktrace is not None else None
        aborted = engine.aborted if engine is not None else False
        result = Result(self, failure, duration, stacktrace=dict_stacktrace, aborted=aborted)
        return result