1# ArkTS语法约束规则集测试用例 2 3## 项目说明 4 5本项目旨在对[从TypeScript到ArkTS的适配规则](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/typescript-to-arkts-migration-guide.md)中的约束规则通过脚本方式进行验证并输出用例的测试结果报告。 6 7其中测试用例默认存放在`testcase`目录,运行结果存放在`test_results`目录。 8 9## 项目要求 10 111. 提供对ArkTS语法约束规则集的测试用例集 122. 框架能对用例集中的用例进行测试,并返回测试结果,成功/失败用例详情。并支持可选屏蔽用例 133. 用例集覆盖所有arkTS语法规则集,每条规则要有正例和反例覆盖,平均用例数不少于10个 14 15## 快速开始 16 171. 编译构建typescript包(工作目录:third_party_typescript) 18 ```shell 19 npm install 20 npm run build 21 npm run release 22 npm pack 23 ``` 24 每次修改代码必须执行以上步骤。 25 262. 安装项目依赖,执行(工作目录:third_party_typescript/tests/arkTSTest) 27 ```shell 28 rm -r ./node_modules/ ./package-lock.json # 每次必须执行 29 npm install # 每次必须执行 30 ``` 313. 将测试用例放至 testcase 文件夹,建议使用约束名称作为测试用例目录,如`arkts-no-any-unknown` 324. 运行`run.js`,进行代码测试 33 34 ```nodejs 35 node run.js -v1.0 36 node run.js -v1.1 37 ``` 38 39 指定测试用例文件夹运行: 40 41 ```shell 42 node run.js -P:testcase/arkts-identifiers-as-prop-names // 可修改为当前路径下指定目录的测试用例 43 ``` 44 45 打印详情: 46 47 ```shell 48 node run.js --detail 49 ``` 50 51 屏蔽用例目录或具体用例: 52 53 ```shell 54 node run.js --ignore-list:testcase/arkts-identifiers-as-prop-names/case1.ets,testcase/arkts-no-any-unknown # 通过--ignore-list指定或添加至 ignorecase.json 55 ``` 56 57 `ignorecase.json`示例: 58 59 ```json 60 { 61 "ignoreCase":["testcase/arkts-identifiers-as-prop-names/xxxx.ets", "testcase/arkts-no-any-unknown"] 62 } 63 ``` 64 65 生成测试结果文件: 66 67 ```shell 68 node run.js -e 69 ``` 70 715. 生成的测试结果存放至 `test_results` 目录,命令行打印基本的测试信息,如: 72 73 ```plain 74 Total number of test cases:2 Number of use cases passed:1 The number of use cases that failed:1 Total running time:371ms 75 Failed test cases: 76 testcase/arkts-no-any-unknown/test2.ets 77 ``` 78 79## 用法说明 80 81### 打印失败测试用例详情 82 83支持通过`--detail`或者`-D`指令打印具体失败用例细节,如运行指令: 84 85```shell 86node run.js -D 87``` 88返回未通过用例详情: 89```plain 90==> Expect the error in Line null The null character. Expect exception rules:null Actual error line 20 The 7character. Actual exception rules:Use explicit types instead of "any", "unknown" (arkts-no-any-unknown) Comparison Result:Fail! 91Total number of test cases:2 Number of use cases passed:1 The number of use cases that failed:1 Total running time:371ms 92Failed test cases: 93testcase/arkts-no-any-unknown/test2.ets 94``` 95 96### 屏蔽指定测试用例 97 981. 支持使用 `--ignore-list` 参数传入屏蔽的用例目录或具体用例,如运行指令: 99 100```shell 101# 忽略单个用例 102node run.js --ignore-list:testcase/arkts-no-any-unknown/test2.ets 103``` 104返回用例测试结果: 105``` 106Total number of test cases:1 Number of use cases passed:1 The number of use cases that failed:0 Total running time:342ms 107Ignored test cases: 108testcase/arkts-no-any-unknown/test2.ets 109``` 1102. 忽略多个用例,请使用`,`分割输入(支持屏蔽目录) 111 112```shell 113node run.js --ignore-list:testcase/arkts-no-any-unknown/test2.ets,testcase/arkts-identifiers-as-prop-names 114``` 1153. 支持通过配置文件导入屏蔽用例: 116 117修改根目录下的`ignorecase.json.example`为`ignorecase.json`,配置`ignoreCase`字段为屏蔽用例字段,如: 118```json 119# ignorecase.json 120{ 121"ignoreCase":["testcase/arkts-no-any-unknown/test2.ets","testcase/arkts-identifiers-as-prop-names"] 122} 123``` 124 125执行测试用例指令: 126 127```shell 128node run.js -D 129``` 130运行结果: 131```plain 132// It can be observed that the configuration use case is properly masked 133Total number of test cases:0 Number of use cases passed:0 The number of use cases that failed:0 Total running time:342ms 134Ignored test cases: 135testcase/arkts-no-any-unknown/test2.ets 136testcase/arkts-identifiers-as-prop-names 137``` 138 139### 使用其他代码运行测试用例脚本 140 141以`Python`代码为例,参考文件`run_test.py`: 142```python 143import subprocess 144 145p = subprocess.Popen("node run.js --detail", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 146out, err = p.communicate() 147print(out.decode('utf-8'), err.decode('utf-8')) 148 149# 获取返回值 150return_code = p.returncode 151print(return_code) 152``` 153预期如果有用例不通过,则抛出异常状态码为`1`,如果用例全部通过,则返回正确状态码为`0`。 154