From a931c0bf6287e5dc80e4a7b968456f98242f3766 Mon Sep 17 00:00:00 2001 From: caojiale1 Date: Fri, 3 Mar 2023 15:50:53 +0800 Subject: [PATCH 1/2] Signed-off-by:caojiale1 --- README_zh.md | 49 ++++++++++++++++++- .../codeCoverage/codeCoverage_gcov_lcov.py | 8 +-- .../codeCoverage/mutilProcess_CodeCoverage.py | 8 +-- localCoverage/coverage_tools.py | 11 +++-- src/core/driver/drivers.py | 7 ++- src/core/utils.py | 14 ++++-- 6 files changed, 79 insertions(+), 18 deletions(-) diff --git a/README_zh.md b/README_zh.md index 7dae01a..58ce3dc 100755 --- a/README_zh.md +++ b/README_zh.md @@ -1134,6 +1134,53 @@ reports/platform_log_xxxx_xx_xx_xx_xx_xx.log reports/latest ``` +### 覆盖率用户指导 +1. (可选执行)为了屏蔽非核心代码产生的冗余分支数据,可以在源码编译之前进入/test/testfwk/developer_test/localCoverage/restore_comment目录下执行: + + python3 build_before_generate.py + + 选择对应的部件,执行命令例如: + + run -tp partname + run -tp partname1 partname2 +2. 编译版本之前首先修改编译选项,涉及到自己子系统的build.gn文件cflags或者cflags_cc及idflags选项都需要加--coverage字段: + + idflags = [ "--coverage" ] + C: cflags = [ "--coverage" ] + C++: cflags_cc = [ "--coverage" ] + +**推荐:** 也可以参考窗口子系统的方式(推荐这种方式),参考链接:https://gitee.com/openharmony/window_window_manager/pulls/1274/files +3. 执行覆盖率需要安装以下依赖包: + + 1)安装lcov. + 2)安装dos2unix, 安装命令:apt install dos2unix. + +4. 远程映射设备,修改usr_config.xml中的ip号,设备映射方式查看上面介绍的远程端口映射, + +``` + + + + + + +``` +5. 执行 + + ./start.sh + + 命令例如下: + run -t UT -tp 部件名 -cov coverage + run -t UT -ss 子系统名 -cov coverage + run -t UT -ss 子系统名 -tp 部件名 -cov coverage + run -t UT MST ST -tp 部件名 -cov coverage + +> **注意:** 必须添加 -cov coverage 参数 + +6. 覆盖率报告路径 + +/test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html + ### 涉及仓 [test\_xdevice](https://gitee.com/openharmony/testfwk_xdevice) @@ -1144,7 +1191,7 @@ reports/latest | ---------- | ------------------------------------------------------------ | | 3.2.1.0 | 1、增加框架对接执行ACTS测试用例能力
2、增加ACTS多种颗粒度执行能力,如子系统、部件等 | | 3.2.2.0 | 1、增加测试任务统计能力,最多统计10个任务
2、增加按照指定测试任务ID执行测试任务能力
3、增加复测上次任务失败用例能力 | - +| 3.2.3.0 | 1、增加覆盖率执行
| diff --git a/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py b/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py index 588c420..2bc6b57 100644 --- a/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py +++ b/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py @@ -57,10 +57,12 @@ def call(cmd_list, is_show_cmd=False, out=None, err=None): def execute_command(command, printflag=False): try: cmd_list = shlex.split(command) - with open("coverage.log", 'a') as fd: + coverage_log_path = os.path.join( + CODEPATH, "/test/testfwk/developer_test/localCoverage", "coverage.log") + with open(coverage_log_path, 'a') as fd: call(cmd_list, printflag, fd, fd) - except IOError as err: - print("Error: Exception occur in open err") + except IOError: + print("Error: Exception occur in open err") def get_subsystem_config_info(): diff --git a/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py b/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py index e5dd3c3..aaf73b1 100644 --- a/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py +++ b/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py @@ -63,10 +63,12 @@ def call(cmd_list, is_show_cmd=False, out=None, err=None): def execute_command(command, printflag=False): try: cmd_list = shlex.split(command) - with open("coverage.log", 'a') as fd: + coverage_log_path = os.path.join( + CODEPATH, "/test/testfwk/developer_test/localCoverage", "coverage.log") + with open(coverage_log_path, 'a') as fd: call(cmd_list, printflag, fd, fd) - except IOError as err: - print("Error: Exception occur in open: %s", err.message) + except IOError: + print("Error: Exception occur in open error") def get_subsystem_config_info(): diff --git a/localCoverage/coverage_tools.py b/localCoverage/coverage_tools.py index 84b8e72..ad036c7 100644 --- a/localCoverage/coverage_tools.py +++ b/localCoverage/coverage_tools.py @@ -117,10 +117,11 @@ def get_subsystem_name(test_part_list, product_name): def generate_product_name(root_path): - # 获取产品形态 - out_path = os.path.join(root_path, "out") - _, gcno_path = subprocess.getstatusoutput("find %s -name '*.gcno' | head -n 1" % out_path) - product_name = gcno_path.split(out_path)[1].strip("/").split("/")[0] + # 获取输出路径 + ohos_config_path = os.path.join(root_path, "ohos_config.json") + with open(ohos_config_path, 'r') as json_file: + json_info = json.load(json_file) + product_name = json_info.get("out_path").split("out")[1].strip("/") return product_name @@ -176,4 +177,4 @@ if __name__ == '__main__': subprocess.run("python3 %s" % restore_source_code_path, shell=True) print(r"See the code coverage report in: " - r"\test\testfwk\developer_test\localCoverage\codeCoverage\results\coverage\reports\cxx\html") + r"/test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html") diff --git a/src/core/driver/drivers.py b/src/core/driver/drivers.py index 0809239..723c856 100644 --- a/src/core/driver/drivers.py +++ b/src/core/driver/drivers.py @@ -549,8 +549,11 @@ class CppTestDriver(IDriver): if coverage_outpath: strip_num = len(coverage_outpath.strip("/").split("/")) else: - root_path = (sys.framework_root_dir.split("test/testfwk/developer_test")[0]) - strip_num = len(root_path.strip("/").split("/")) + 2 + ohos_config_path = os.path.join(sys.source_code_root_path, "ohos_config.json") + with open(ohos_config_path, 'r') as json_file: + json_info = json.load(json_file) + out_path = json_info.get("out_path") + strip_num = len(out_path.strip("/").split("/")) if "fuzztest" == self.config.testtype[0]: self._push_corpus_cov_if_exist(suite_file) command = f"cd {self.config.target_test_path}; tar zxf {filename}_corpus.tar.gz; \ diff --git a/src/core/utils.py b/src/core/utils.py index da73226..7be7853 100755 --- a/src/core/utils.py +++ b/src/core/utils.py @@ -126,6 +126,15 @@ def scan_support_product(): return productform_list +def get_output_path(): + # 获取输出路径 + ohos_config_path = os.path.join(sys.source_code_root_path, "ohos_config.json") + with open(ohos_config_path, 'r') as json_file: + json_info = json.load(json_file) + out_name = json_info.get("out_path").split("out")[1].strip("/") + return out_name + + def parse_device_name(product_form): device_json_file = os.path.join(sys.source_code_root_path, "productdefine", "common", "products", @@ -139,10 +148,7 @@ def parse_device_name(product_form): return device_name = json_info.get('product_device') if not device_name: - if product_form.startswith("rk"): - device_name = product_form - else: - device_name = "baltimore" + device_name = get_output_path() return device_name -- Gitee From 370ba4d2f8245c81dc3b5429f5c50bc24f1def3a Mon Sep 17 00:00:00 2001 From: caojiale1 Date: Fri, 3 Mar 2023 17:00:45 +0800 Subject: [PATCH 2/2] Signed-off-by:caojiale1 --- localCoverage/codeCoverage/codeCoverage_gcov_lcov.py | 2 +- localCoverage/codeCoverage/mutilProcess_CodeCoverage.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py b/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py index 2bc6b57..ed6ab11 100644 --- a/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py +++ b/localCoverage/codeCoverage/codeCoverage_gcov_lcov.py @@ -58,7 +58,7 @@ def execute_command(command, printflag=False): try: cmd_list = shlex.split(command) coverage_log_path = os.path.join( - CODEPATH, "/test/testfwk/developer_test/localCoverage", "coverage.log") + CODEPATH, "test/testfwk/developer_test/localCoverage", "coverage.log") with open(coverage_log_path, 'a') as fd: call(cmd_list, printflag, fd, fd) except IOError: diff --git a/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py b/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py index aaf73b1..4ece28c 100644 --- a/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py +++ b/localCoverage/codeCoverage/mutilProcess_CodeCoverage.py @@ -64,7 +64,7 @@ def execute_command(command, printflag=False): try: cmd_list = shlex.split(command) coverage_log_path = os.path.join( - CODEPATH, "/test/testfwk/developer_test/localCoverage", "coverage.log") + CODEPATH, "test/testfwk/developer_test/localCoverage", "coverage.log") with open(coverage_log_path, 'a') as fd: call(cmd_list, printflag, fd, fd) except IOError: -- Gitee