Exemplo n.º 1
0
    def test_json_report_retry(self):
        old_cwd = os.getcwd()
        try:
            test_name = "tests.sampletest.hellotest.HelloTest tests.sampletest.hellotest.TimeoutTest"
            time_str = get_time_str()
            working_dir = test_name + "_" + time_str
            os.makedirs(working_dir)
            os.chdir(working_dir)
            self.addCleanup(shutil.rmtree, working_dir, True)

            test_report_name = "test_json_report_%s.json" % time_str
            retry_count = 2
            with codecs_open(test_report_name, "w", encoding="utf-8") as fd:
                test_report = report_types["json"](fd=fd,
                                                   title="test_json_report")
                test_runner = runner_types["multithread"](test_report,
                                                          retries=retry_count)
                test_runner.run(test_name)
            with codecs_open(test_report_name, "r", encoding="utf-8") as fd:
                content = fd.read()
                report_json = json.loads(content)
                summary = report_json["summary"]
                self.assertEqual(summary["testcase_total_run"],
                                 (retry_count + 1) * 2)
                self.assertEqual(summary["testcase_total_count"], 2)
                self.assertTrue("hostname" in summary["environment"])
                self.assertTrue("os" in summary["environment"])
                self.assertTrue("qtaf_version" in summary["environment"])
                self.assertTrue("python_version" in summary["environment"])
        finally:
            os.chdir(old_cwd)
Exemplo n.º 2
0
def _update_pydev_conffile(proj_path, egg_name):
    '''更新pydev配置文件中的Egg名称
    '''
    with codecs_open(os.path.join(proj_path, '.pydevproject'),
                     'r',
                     encoding="utf-8") as fd:
        doc = dom.parse(fd)
        nodes = doc.getElementsByTagName('pydev_pathproperty')
        if len(nodes) == 0:
            propsnode = doc.createElement("pydev_pathproperty")
            propsnode.setAttribute("name",
                                   "org.python.pydev.PROJECT_SOURCE_PATH")
        else:
            propsnode = nodes[0]
            for pathnode in propsnode.getElementsByTagName("path"):
                rc = []
                for node in pathnode.childNodes:
                    if node.nodeType == node.TEXT_NODE:
                        rc.append(node.data)
                path = ''.join(rc)
                if path.endswith('.egg') and 'qtaf-' in path:
                    propsnode.removeChild(pathnode)
                    break
        pathnode = doc.createElement("path")
        pathnode.appendChild(
            doc.createTextNode("/${PROJECT_DIR_NAME}/exlib/%s" % egg_name))
        propsnode.appendChild(pathnode)

    with codecs_open(os.path.join(proj_path, '.pydevproject'), 'w',
                     'utf8') as fd:
        fd.write(doc.toxml(encoding='UTF-8'))
Exemplo n.º 3
0
    def _test_getfile(self, resmgr):
        with codecs_open(resmgr.get_file(self.file_name),
                         encoding="utf-8") as f:
            print(f.read())

        with codecs_open(resmgr.get_file(self.link_file_name[:-5]),
                         encoding="utf-8") as f:
            print(f.read())
Exemplo n.º 4
0
 def test_json_report(self):
     def _clean_json_report(json_report_file, results=None):
         if results is None:
             with codecs_open(json_report_file, "r", encoding="utf-8") as fd:
                 content = fd.read()
                 results = json.loads(content)["results"]
         json_files = results[:]
         json_files.append(json_report_file)
         for json_file in json_files:
             os.remove(json_file)
            
     test_pairs = [("HelloTest", "断言失败"),
                 ("TimeoutTest", "用例执行超时"),
                 ("CrashTest", "App Crash"),
                 ("QT4iTest", "run_test执行失败"),]
     for test_name, reason in test_pairs:
         time_str = get_time_str()
         test_report_name = "%s_%s.json" % (time_str, test_name)
         with codecs_open(test_report_name, "w", encoding="utf-8") as fd:
             test_report = report.report_types["json"](fd=fd)
             test_runner = runner.runner_types["basic"](test_report)
             test_name = "test.sampletest.hellotest.%s" % test_name
             print("json report test for test: " + test_name)
             test_runner.run(test_name)
         with codecs_open(test_report_name, "r", encoding="utf-8") as fd:
             content = fd.read()
             report_json = json.loads(content)
             self.assertEqual(report_json["loaded_testcases"][0]["name"], test_name)
             test_results = report_json["results"]
             self.addCleanup(_clean_json_report, test_report_name, test_results)
             self.assertEqual(len(test_results), 1)
             with codecs_open(test_results[0], "r", encoding="utf-8") as fd2:
                 content = fd2.read()
                 result_json = json.loads(content)
                 self.assertEqual(result_json["succeed"], False)
                 failed_step = result_json["steps"][-1]
                 self.assertEqual(failed_step["succeed"], False)
                 actual_reson = smart_text(failed_step["logs"][0]["message"])
                 self.assertRegexpMatches(actual_reson, reason)
         
     test_name = "test.sampletest.hellotest.HelloTest test.sampletest.hellotest.TimeoutTest"
     time_str = get_time_str()
     test_report_name = "test_json_report_%s.json" % time_str
     retry_count = 2
     with codecs_open(test_report_name, "w", encoding="utf-8") as fd:
         test_report = report.report_types["json"](fd=fd, title="test_json_report")
         test_runner = runner.runner_types["multithread"](test_report, retries=retry_count)
         test_runner.run(test_name)
     with codecs_open(test_report_name, "r", encoding="utf-8") as fd:
         content = fd.read()
         report_json = json.loads(content)
         summary = report_json["summary"]
         self.assertEqual(summary["testcase_total_run"], (retry_count + 1) * 2)
         self.assertEqual(summary["testcase_total_count"], 2)
         self.assertTrue("hostname" in summary)
         self.assertTrue("os" in summary)
         self.addCleanup(_clean_json_report, test_report_name)
Exemplo n.º 5
0
def _create_local_testfile():
    test_dir = os.path.join(root_dir, test_dir_name)
    if not os.path.exists(test_dir):
        os.makedirs(test_dir)
    with codecs_open(os.path.join(test_dir, "foo.txt"), mode="w") as fd:
        fd.write("foo")
    local_file = os.path.join(root_dir, 'a_%s.txt' % suffix)
    with codecs_open(local_file, 'w', encoding="utf-8") as f:
        f.write('abc')

    return local_file, root_dir
Exemplo n.º 6
0
 def begin_report(self):
     super(VerboseOnlineTestReport, self).begin_report()
     with codecs_open(os.path.join(os.getcwd(),
                                   "report_url.txt"),
                      "w",
                      encoding="utf-8") as fd:
         fd.write(self.url)
Exemplo n.º 7
0
    def _merge_requirements(self):
        """Merge exlib & requirements.txt"""
        exlib = os.path.join(settings.PROJECT_ROOT, 'exlib')
        egg_pattern = re.compile(
            r"(?P<name>[a-zA-Z0-9_]+)(\-(?P<version>[0-9a-zA-Z_\.]+)|)(\-.*|)\.egg$"
        )
        reqs_dict = {}

        req_txt = os.path.join(settings.PROJECT_ROOT, "requirements.txt")
        if os.path.isfile(req_txt):
            with codecs_open(req_txt, 'r', encoding="utf-8") as fd:
                for it in pkg_resources.parse_requirements(fd.read()):
                    reqs_dict[it.name] = str(it)

        elif os.path.isdir(exlib):
            for filename in os.listdir(exlib):
                if not filename.endswith(".egg"):
                    continue
                result = egg_pattern.match(filename)
                if not result:
                    continue
                name, version = result.group('name'), result.group('version')
                if version:
                    reqs_dict[name] = "%s==%s" % (name, version)
                else:
                    reqs_dict[name] = name

        if 'qtaf' not in reqs_dict:
            reqs_dict["qtaf"] = "qtaf"
        return reqs_dict.values()
Exemplo n.º 8
0
 def get_file(self):
     file_name = '%s_%s.json' % (self._translated_name, get_time_str())
     if not path_exists(file_name):
         content = json.dumps(self._data)
         with codecs_open(file_name, mode="w", encoding="utf-8") as fd:
             fd.write(content)
     return file_name
Exemplo n.º 9
0
    def get_file(self, relative_path):
        """查找某个文件

        :type relative_path:string
        :param relative_path: ,资源文件相对描述符,相对于setting下的资源目录的路径,支持多级目录
        :return:返回资源文件的绝对路径
        """
        result = []
        relative_path = self._adjust_path(relative_path)
        if relative_path.startswith(os.sep):
            relative_path = relative_path[1:]
        for it in self._resources_dirs:
            file_path = self._adjust_path(os.path.join(it, relative_path))
            file_path = smart_text(file_path)
            file_link = smart_text(file_path + '.link')
            if os.path.isfile(file_path):
                result.append(file_path)
            elif os.path.isfile(file_link):
                with codecs_open(file_link, encoding="utf-8") as f:
                    remote_path = f.read()
                    file_path = self._resolve_link_file(remote_path, file_path)
                    result.append(file_path)
        if len(result) > 1:
            raise Exception("存在多个%s文件" % relative_path)
        elif len(result) < 1:
            raise Exception("%s文件不存在" % relative_path)
        return result[0]
Exemplo n.º 10
0
    def abs_path(self, relative_path):
        """get resource absolute path
:        type relative_path:string
        :param relative_path: ,资源文件相对描述符,相对于setting下的资源目录的路径,支持多级目录
        :return:返回资源文件的绝对路径
        """
        relative_path = self._adjust_path(relative_path)
        if relative_path.startswith(os.sep):
            relative_path = relative_path[1:]

        found_paths = []
        for it in self._resources_dirs:
            file_path = self._adjust_path(os.path.join(it, relative_path))
            file_path = smart_text(file_path)
            file_link = smart_text(file_path + '.link')
            if os.path.exists(file_path):
                found_paths.append(file_path)
            elif os.path.exists(file_link):
                with codecs_open(file_link, encoding="utf-8") as f:
                    remote_path = f.read()
                    file_path = self._resolve_link_file(remote_path, file_path)
                    found_paths.append(file_path)
        if len(found_paths) == 0:
            raise Exception("relative_path=%s not found" % relative_path)
        if len(found_paths) > 1:
            raise Exception("relative_path=%s got multiple results:\n%s" %
                            (relative_path, "\n".join(found_paths)))
        return os.path.abspath(file_path)
Exemplo n.º 11
0
    def test_html_report_content(self):
        test_pairs = [
            ("HelloTest", "断言失败"),
            ("TimeoutTest", "用例执行超时"),
            ("CrashTest", "App Crash"),
            ("QT4iTest", "run_test执行失败"),
        ]

        old_cwd = os.getcwd()
        for test_name, reason in test_pairs:
            try:
                working_dir = test_name + "_" + get_time_str()
                os.makedirs(working_dir)
                os.chdir(working_dir)
                self.addCleanup(shutil.rmtree, working_dir, True)

                test_report = report_types["html"](title="test html report")
                test_runner = runner_types["basic"](test_report)
                test_name = "tests.sampletest.hellotest.%s" % test_name
                print("html report test for test: " + test_name)
                test_runner.run(test_name)
                html_report_file = os.path.join(os.getcwd(), "qta-report.js")
                with codecs_open(html_report_file, encoding="utf-8") as fd:
                    content = fd.read()
                    index = content.find("{")
                    qta_report_data = content[index:]
                    qta_report = json.loads(qta_report_data)
                    failed_test_names = list(qta_report["failed_tests"].keys())
                    self.assertEqual(failed_test_names[0], test_name)
                    failed_tests = qta_report["failed_tests"]
                    self.assertEqual(len(failed_tests), 1)
                    with codecs_open(failed_tests[test_name]["records"][0],
                                     "r",
                                     encoding="utf-8") as fd2:
                        content = fd2.read()
                        index = content.find("{")
                        result_json_data = content[index:]
                        result_json = json.loads(result_json_data)
                        self.assertEqual(result_json["succeed"], False)
                        failed_step = result_json["steps"][-1]
                        self.assertEqual(failed_step["succeed"], False)
                        actual_reson = smart_text(
                            failed_step["logs"][0]["message"])
                        self.assertRegexpMatches(actual_reson, reason)

            finally:
                os.chdir(old_cwd)
Exemplo n.º 12
0
 def get_file(self):
     file_name = '%s_%s.json' % (self._translated_name, get_time_str())
     file_path = os.path.join(os.getcwd(), file_name)
     if not os.path.exists(file_path):
         content = json.dumps(self._data)
         with codecs_open(file_path, mode="w", encoding="utf-8") as fd:
             fd.write(content)
     return file_path
Exemplo n.º 13
0
 def pre_test(self):
     from testbase.conf import settings
     self.resource_root = os.path.join(settings.PROJECT_ROOT, 'resources')
     if not os.path.isdir(self.resource_root):
         os.mkdir(self.resource_root)
     self.file_name = "a_%s%s_resmgr.txt" % (sys.version_info[0],
                                             sys.version_info[1])
     self.link_file_name = "readme_%s%s.txt.link" % (sys.version_info[0],
                                                     sys.version_info[1])
     with codecs_open(os.path.join(self.resource_root, self.file_name),
                      'w',
                      encoding="utf-8") as f:
         f.write('abc')
     with codecs_open(os.path.join(self.resource_root, self.link_file_name),
                      'w',
                      encoding="utf-8") as f:
         f.write(os.path.join(self.resource_root, self.file_name))
Exemplo n.º 14
0
def _create_initpy(dir_path, doc):
    '''创建一个__init__.py
        '''
    with codecs_open(os.path.join(dir_path, '__init__.py'), 'w') as fd:
        fd.write(INITPY_CONTENT % {
            "Doc": doc,
            "Date": datetime.date.today().strftime('%Y/%m/%d')
        })
Exemplo n.º 15
0
    def end_report(self):
        super(HtmlTestReport, self).end_report()
        data = json.dumps(self._data)
        content = "var qta_report_data = %s" % data
        with codecs_open("qta-report.js", "w", encoding="utf-8") as fd:
            fd.write(content)

        qta_report_html = get_inner_resource("qta_statics", "qta-report.html")
        shutil.copy(qta_report_html, os.getcwd())
Exemplo n.º 16
0
 def parse_args(cls, args_string):
     '''通过命令行参数构造对象
     
     :returns: 测试报告
     :rtype: cls
     '''
     args = cls.get_parser().parse_args(args_string)
     fd = codecs_open(args.output, 'w', encoding="utf-8")
     return cls(fd, title=args.title)
Exemplo n.º 17
0
 def __init__(self, output_file):
     if output_file is None:
         self._fd = None
         self._close_fd = False
         self._output_func = logger.info
     else:
         self._fd = codecs_open(output_file, "w")
         self._close_fd = True
         self._output_func = lambda x: self._fd.write(x + "\n")
Exemplo n.º 18
0
def _create_managepy(proj_path):
    """创建manage.py文件
    """
    with codecs_open(os.path.join(proj_path, "manage.py"), "wb") as fd:
        fd.write(
            (
                MANAGE_CONTENT % {"Date": datetime.date.today().strftime("%Y/%m/%d")}
            ).encode("utf8")
        )
Exemplo n.º 19
0
 def _clean_json_report(json_report_file, results=None):
     if results is None:
         with codecs_open(json_report_file, "r", encoding="utf-8") as fd:
             content = fd.read()
             results = json.loads(content)["results"]
     json_files = results[:]
     json_files.append(json_report_file)
     for json_file in json_files:
         os.remove(json_file)
Exemplo n.º 20
0
 def _download_file(self, url, target_path):
     from six.moves.urllib import request, error
     try:
         rsp = request.urlopen(url, timeout=300)
         rspbuf = rsp.read()
     except error.HTTPError as e:
         raise DownloadFileError(url, e.code, e.msg, e.headers, e.read())
     with codecs_open(target_path, "wb") as fd:
         fd.write(rspbuf)
Exemplo n.º 21
0
 def get_file(self):
     file_name = '%s_%s.js' % (self._translated_name, get_time_str())
     if not path_exists(file_name):
         var_name = os.path.basename(file_name)
         var_name = os.path.splitext(file_name)[0].replace(".", "_")
         content = "var %s = %s" % (var_name, json.dumps(self._data))
         content = smart_binary(content)
         with codecs_open(file_name, mode="wb") as fd:
             fd.write(content)
     return file_name
Exemplo n.º 22
0
    def test_json_report_content(self):
        test_pairs = [("HelloTest", "断言失败"), ("TimeoutTest", "用例执行超时"),
                      ("CrashTest", "App Crash"), ("QT4iTest", "run_test执行失败")]

        old_cwd = os.getcwd()
        for test_name, reason in test_pairs:
            try:
                time_str = get_time_str()
                working_dir = test_name + "_" + time_str
                os.makedirs(working_dir)
                os.chdir(working_dir)
                self.addCleanup(shutil.rmtree, working_dir, True)

                test_report_name = "%s_%s.json" % (time_str, test_name)
                with codecs_open(test_report_name, "w",
                                 encoding="utf-8") as fd:
                    test_report = report_types["json"](fd=fd)
                    test_runner = runner_types["basic"](test_report)
                    test_name = "tests.sampletest.hellotest.%s" % test_name
                    print("json report test for test: " + test_name)
                    test_runner.run(test_name)
                with codecs_open(test_report_name, "r",
                                 encoding="utf-8") as fd:
                    content = fd.read()
                    report_json = json.loads(content)
                    failed_test_names = list(
                        report_json["failed_tests"].keys())
                    self.assertEqual(failed_test_names[0], test_name)
                    failed_tests = report_json["failed_tests"]
                    self.assertEqual(len(failed_tests), 1)
                    with codecs_open(failed_tests[test_name]["records"][0],
                                     "r",
                                     encoding="utf-8") as fd2:
                        content = fd2.read()
                        result_json = json.loads(content)
                        self.assertEqual(result_json["succeed"], False)
                        failed_step = result_json["steps"][-1]
                        self.assertEqual(failed_step["succeed"], False)
                        actual_reson = smart_text(
                            failed_step["logs"][0]["message"])
                        self.assertRegexpMatches(actual_reson, reason)
            finally:
                os.chdir(old_cwd)
Exemplo n.º 23
0
 def __init__(self, output_file):
     if output_file is None:
         self._fd = None
         self._close_fd = False
         self._output_func = logger.info
     else:
         self._fd = codecs_open(output_file, "wb")
         self._close_fd = True
         self._output_func = lambda x: self._fd.write(
             (x if isinstance(x, bytes) else x.encode("utf-8")) + b"\n")
Exemplo n.º 24
0
def _create_initpy(dir_path, doc):
    """创建一个__init__.py
        """
    with codecs_open(os.path.join(dir_path, "__init__.py"), "wb") as fd:
        fd.write(
            (
                INITPY_CONTENT
                % {"Doc": doc, "Date": datetime.date.today().strftime("%Y/%m/%d")}
            ).encode("utf8")
        )
Exemplo n.º 25
0
def _create_sample_lib(dir_path, proj_name):
    '''创建示例lib
    '''
    with codecs_open(os.path.join(dir_path, 'testcase.py'), 'w') as fd:
        fd.write(
            TESTLIB_CONTENT % {
                "Date": datetime.date.today().strftime('%Y/%m/%d'),
                "ProjectName": proj_name,
                "ProjectNameCapUp": proj_name[0].upper() + proj_name[1:]
            })
Exemplo n.º 26
0
def _create_sample_test(dir_path, proj_name):
    '''创建示例测试用例
    '''
    with codecs_open(os.path.join(dir_path, 'hello.py'), 'w') as fd:
        fd.write(
            TESTCASE_CONTENT % {
                "Date": datetime.date.today().strftime('%Y/%m/%d'),
                "ProjectName": proj_name,
                "ProjectNameCapUp": proj_name[0].upper() + proj_name[1:],
                "UserName": getpass.getuser()
            })
Exemplo n.º 27
0
 def handle_test_end(self, passed):
     '''处理一个测试用例执行的结束
     
     :param passed: 测试用例是否通过
     :type passed: boolean
     '''
     self._testnode.setAttribute('result', str(passed))
     self._testnode.setAttribute('endtime', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.end_time)))
     self._testnode.setAttribute('duration', "%02d:%02d:%02.2f\n" % _convert_timelength(self.end_time - self.begin_time))
     if self._file_path:
         with codecs_open(smart_text(self._file_path), 'wb') as fd:
             fd.write(to_pretty_xml(self._xmldoc))
Exemplo n.º 28
0
def _create_pydev_conffile(proj_path, mode):
    '''创建pydev配置文件
    '''
    with codecs_open(os.path.join(proj_path, '.pydevproject'), 'w') as fd:
        if mode == EnumProjectMode.Standalone:
            qtaf_egg_path = os.path.join(
                os.path.dirname(os.path.abspath(__file__)), '..')
            qtaf_egg_path = os.path.abspath(qtaf_egg_path)
            fd.write(PYDEV_CONF_CONTENT_STANDALONE %
                     {"EggName": os.path.basename(qtaf_egg_path)})
        else:
            fd.write(PYDEV_CONF_CONTENT_STANDARD)
Exemplo n.º 29
0
def _copy_qtaf_egg(egg_path, dir_path):
    '''拷贝QTAF egg包
    '''
    shutil.copy(egg_path, dir_path)
    with zipfile.ZipFile(egg_path) as zfile:
        try:
            zfile.getinfo("doc/qtaf.chm")  #chm可能不存在
        except KeyError:
            return
        else:
            with codecs_open(os.path.join(dir_path, "qtaf.chm"), 'wb') as fd:
                fd.write(zfile.read("doc/qtaf.chm"))
Exemplo n.º 30
0
def _create_settingspy(proj_path, proj_name, mode):
    '''创建settings.py文件
    '''
    if mode == EnumProjectMode.Standalone:
        content = SETTINGS_CONTENT_STANDALONE
    else:
        content = SETTINGS_CONTENT_STANDARD
    with codecs_open(os.path.join(proj_path, 'settings.py'), 'w') as fd:
        fd.write(
            content % {
                "Date": datetime.date.today().strftime('%Y/%m/%d'),
                "ProjectName": proj_name
            })