コード例 #1
0
def prettify_json_file(file_list):
    """ prettify JSON testcase format
    """
    for json_file in set(file_list):
        if not json_file.endswith(".json"):
            logger.log_warning(
                "Only JSON file format can be prettified, skip: {}".format(
                    json_file))
            continue

        logger.color_print("Start to prettify JSON file: {}".format(json_file),
                           "GREEN")

        dir_path = os.path.dirname(json_file)
        file_name, file_suffix = os.path.splitext(os.path.basename(json_file))
        outfile = os.path.join(dir_path, "{}.pretty.json".format(file_name))

        with io.open(json_file, 'r', encoding='utf-8') as stream:
            try:
                obj = json.load(stream)
            except ValueError as e:
                raise SystemExit(e)

        with io.open(outfile, 'w', encoding='utf-8') as out:
            json.dump(obj, out, indent=4, separators=(',', ': '))
            out.write('\n')

        print("success: {}".format(outfile))
コード例 #2
0
ファイル: validator.py プロジェクト: 237700627/PythonGit
def validate_json_file(file_list):
    """ validate JSON testcase format
    """
    for json_file in set(file_list):
        if not json_file.endswith(".json"):
            logger.log_warning(
                "Only JSON file format can be validated, skip: {}".format(
                    json_file))
            continue

        logger.color_print("Start to validate JSON file: {}".format(json_file),
                           "GREEN")

        with io.open(json_file) as stream:
            try:
                json.load(stream)
            except ValueError as e:
                raise SystemExit(e)

        print("OK")
コード例 #3
0
def dump_json_file(json_data, pwd_dir_path, dump_file_name):
    """ dump json data to file
    """
    class PythonObjectEncoder(json.JSONEncoder):
        def default(self, obj):
            try:
                return super().default(self, obj)
            except TypeError:
                return str(obj)

    logs_dir_path = os.path.join(pwd_dir_path, "logs")
    if not os.path.isdir(logs_dir_path):
        os.makedirs(logs_dir_path)

    dump_file_path = os.path.join(logs_dir_path, dump_file_name)

    try:
        with io.open(dump_file_path, 'w', encoding='utf-8') as outfile:
            if is_py2:
                outfile.write(
                    unicode(
                        json.dumps(json_data,
                                   indent=4,
                                   separators=(',', ':'),
                                   ensure_ascii=False,
                                   cls=PythonObjectEncoder)))
            else:
                json.dump(json_data,
                          outfile,
                          indent=4,
                          separators=(',', ':'),
                          ensure_ascii=False,
                          cls=PythonObjectEncoder)

        msg = "dump file: {}".format(dump_file_path)
        logger.color_print(msg, "BLUE")

    except TypeError as ex:
        msg = "Failed to dump json file: {}\nReason: {}".format(
            dump_file_path, ex)
        logger.color_print(msg, "RED")
コード例 #4
0
 def create_file(path, file_content=""):
     with open(path, 'w') as f:
         f.write(file_content)
     msg = "created file: {}".format(path)
     logger.color_print(msg, "BLUE")
コード例 #5
0
 def create_folder(path):
     os.makedirs(path)
     msg = "created folder: {}".format(path)
     logger.color_print(msg, "BLUE")
コード例 #6
0
def create_scaffold(project_name):
    """ create scaffold with specified project name.
    """
    if os.path.isdir(project_name):
        logger.log_warning(
            u"Folder {} exists, please specify a new folder name.".format(
                project_name))
        return

    logger.color_print("Start to create new project: {}".format(project_name),
                       "GREEN")
    logger.color_print("CWD: {}\n".format(os.getcwd()), "BLUE")

    def create_folder(path):
        os.makedirs(path)
        msg = "created folder: {}".format(path)
        logger.color_print(msg, "BLUE")

    def create_file(path, file_content=""):
        with open(path, 'w') as f:
            f.write(file_content)
        msg = "created file: {}".format(path)
        logger.color_print(msg, "BLUE")

    def create_path(path, ptype, file_content=""):
        if ptype == "folder":
            os.makedirs(path)
        elif ptype == "file":
            with open(path, 'w') as f:
                f.write(file_content)

            demo_api_content = """
        name: demo api
        variables:
            var1: value1
            var2: value2
        request:
            url: /api/path/$var1
            method: POST
            headers:
                Content-Type: "application/json"
            json:
                key: $var2
        validate:
            - eq: ["status_code", 200]
        """
            demo_testcase_content = """
        config:
            name: "demo testcase"
            variables:
                device_sn: "ABC"
                username: ${ENV(USERNAME)}
                password: ${ENV(PASSWORD)}
            base_url: "http://127.0.0.1:5000"

        teststeps:
        -
            name: demo step 1
            api: path/to/api1.yml
            variables:
                user_agent: 'iOS/10.3'
                device_sn: $device_sn
            extract:
                - token: content.token
            validate:
                - eq: ["status_code", 200]
        -
            name: demo step 2
            api: path/to/api2.yml
            variables:
                token: $token
        """
            demo_testsuite_content = """
        config:
            name: "demo testsuite"
            variables:
                device_sn: "XYZ"
            base_url: "http://127.0.0.1:5000"

        testcases:
        -
            name: call demo_testcase with data 1
            testcase: path/to/demo_testcase.yml
            variables:
                device_sn: $device_sn
        -
            name: call demo_testcase with data 2
            testcase: path/to/demo_testcase.yml
            variables:
                device_sn: $device_sn
        """
            ignore_content = "\n".join([
                ".env", "reports/*", "__pycache__/*", "*.pyc",
                ".python-version", "logs/*"
            ])
            demo_debugtalk_content = """
        import time

        def sleep(n_secs):
            time.sleep(n_secs)
        """
            demo_env_content = "\n".join(
                ["USERNAME=leolee", "PASSWORD=123456"])

            create_folder(project_name)
            create_folder(os.path.join(project_name, "api"))
            create_folder(os.path.join(project_name, "testcases"))
            create_folder(os.path.join(project_name, "testsuites"))
            create_folder(os.path.join(project_name, "reports"))
            create_file(os.path.join(project_name, "api", "demo_api.yml"),
                        demo_api_content)
            create_file(
                os.path.join(project_name, "testcases", "demo_testcase.yml"),
                demo_testcase_content)
            create_file(
                os.path.join(project_name, "testsuites", "demo_testsuite.yml"),
                demo_testsuite_content)
            create_file(os.path.join(project_name, "debugtalk.py"),
                        demo_debugtalk_content)
            create_file(os.path.join(project_name, ".env"), demo_env_content)
            create_file(os.path.join(project_name, ".gitignore"),
                        ignore_content)
コード例 #7
0
ファイル: report.py プロジェクト: 237700627/PythonGit
 def startTest(self, test):
     """ add start test time """
     super(HtmlTestResult, self).startTest(test)
     logger.color_print(test.shortDescription(), "yellow")