skills/check-test-code-quality/rules/R016/SKILL.md
# R016: testcase命名规范 - **规则编号**: R016 - **严重级别**: Warning - **规则复杂度**: complex(需要代码分析) - **问题类型**: testcase名称包含特殊字符(仅允许英文字母、数字、下划线、连字符) - **修复方式**: 移除特殊字符,追加`Adapt`+三位数字后缀,同步修改`@tc.name` - **预期问题数量**: 400+ ## 扫描范围 仅扫描测试文件: - `.test.ets` - ArkTS测试文件 - `.test.ts` - TypeScript测试文件 - `.test.js` - JavaScript测试文件 ## 问题描述 testcase名称(`it()`的第一个参数)中包含特殊字符。仅允许使用英文字母(`a-zA-Z`)、数字(`0-9`)、下划线(`_`)和连字符(`-`)。 **检测对象**: 以`it()`的第一个参数为准,不检测`@tc.name`的值。`@tc.name`仅在修复阶段同步修改。 ## 检测逻辑 ### 核心算法 ```python im
npx skillsauth add openharmonyinsight/openharmony-skills skills/check-test-code-quality/rules/R016Install this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Adapt+三位数字后缀,同步修改@tc.name仅扫描测试文件:
.test.ets - ArkTS测试文件.test.ts - TypeScript测试文件.test.js - JavaScript测试文件testcase名称(it()的第一个参数)中包含特殊字符。仅允许使用英文字母(a-zA-Z)、数字(0-9)、下划线(_)和连字符(-)。
检测对象: 以it()的第一个参数为准,不检测@tc.name的值。@tc.name仅在修复阶段同步修改。
import re
TC_NAME_PATTERN = re.compile(r'^[a-zA-Z0-9_-]+$')
IT_PATTERN = re.compile(r'\bit\s*\(\s*([\'"])([^\'"]+)\1')
TC_NAME_ANNOTATION = re.compile(r'@tc\.name\s+(.+)')
def scan_r016(file_path: str, content: str) -> list:
issues = []
lines = content.split('\n')
it_entries = extract_all_it_entries(lines)
for entry in it_entries:
tc_name = entry['it_tc_name']
it_line_idx = entry['line_idx']
if TC_NAME_PATTERN.match(tc_name):
continue
# testcase名称包含特殊字符
snippet = lines[it_line_idx].strip()
if len(snippet) > 120:
snippet = snippet[:120] + '...'
# 生成修复建议
suggestion = build_r016_suggestion(file_path, it_line_idx + 1, tc_name, lines, entry)
issues.append({
'rule': 'R016',
'type': 'testcase命名规范',
'severity': 'Warning',
'file': file_path,
'line': it_line_idx + 1,
'testcase': tc_name,
'snippet': snippet,
'suggestion': suggestion,
})
return issues
def extract_all_it_entries(lines: list) -> list:
"""
提取文件中所有it()函数调用及其对应的@tc.name注解。
Returns:
[{'line_idx': int, 'tc_name': str, 'tc_name_line_idx': int|None, 'has_tc_annotation': bool}]
"""
entries = []
i = 0
while i < len(lines):
line = lines[i]
# 匹配it()函数调用
match = IT_PATTERN.search(line)
if match:
it_tc_name = match.group(2)
tc_name_line_idx = None
has_tc_annotation = False
final_tc_name = it_tc_name
# 向上查找最近的 @tc.name 注解
for back in range(i - 1, max(i - 20, -1), -1):
back_line = lines[back].strip()
if not back_line:
continue
if back_line.startswith('* @tc.name') or back_line.startswith('@tc.name'):
tc_match = re.search(r'@tc\.name\s+(.+)', back_line)
if tc_match:
annotated_name = tc_match.group(1).strip()
final_tc_name = annotated_name
tc_name_line_idx = back
has_tc_annotation = True
break
# 遇到注释块开始或非注释行,停止向上搜索
if back_line.startswith('/**') or back_line.startswith('/*'):
break
if not back_line.startswith('*') and not back_line.startswith('//'):
break
entries.append({
'line_idx': i,
'tc_name': final_tc_name,
'it_tc_name': it_tc_name,
'tc_name_line_idx': tc_name_line_idx,
'has_tc_annotation': has_tc_annotation,
})
i += 1
return entries
def build_r016_suggestion(file_path: str, line_num: int, tc_name: str,
lines: list, entry: dict) -> str:
"""
生成R016修复建议,包含具体的修复方案。
修复规则:
1. 移除testcase名称中所有非[a-zA-Z0-9_-]的字符
2. 移除后追加Adapt+三位数字后缀(从001开始)
3. 如果追加后命名存在重复,数字递增
4. 同步修改@tc.name值(如果有)
5. 仅修改it()和@tc.name,不修改@tc.number、@tc.desc等
"""
# 步骤1:移除特殊字符
cleaned = re.sub(r'[^a-zA-Z0-9_-]', '', tc_name)
# 步骤2:收集文件中已有的testcase名称,确保不重复
existing_names = set()
for e in extract_all_it_entries(lines):
existing_names.add(e['tc_name'])
# 步骤3:追加Adapt+三位数字后缀,确保唯一
new_name = None
for suffix_num in range(1, 1000):
candidate = f"{cleaned}Adapt{suffix_num:03d}"
if candidate not in existing_names:
new_name = candidate
break
if new_name is None:
new_name = f"{cleaned}Adapt999"
suggestion_parts = [
f"路径: {file_path}, 行号: {line_num}, ",
f"问题描述: testcase名称 '{tc_name}' 包含特殊字符。",
f"仅允许英文字母、数字、下划线、连字符。",
]
if entry['has_tc_annotation']:
suggestion_parts.append(
f"修复: 将it()参数和@tc.name修改为 '{new_name}'。"
)
else:
suggestion_parts.append(
f"修复: 将it()参数修改为 '{new_name}'。"
)
return ''.join(suggestion_parts)
[a-zA-Z0-9_-]的字符Adapt+三位数字后缀(从001开始)001 -> 002 -> 003...)it()参数变动时,必须同步修改对应的@tc.name值,保持一致it()和@tc.name,不修改@tc.number、@tc.desc等其他字段// 修复前
/**
* @tc.name test.name@001
* @tc.number SUB_TEST_0100
* @tc.desc 测试用例描述
*/
it('test.name@001', Level.LEVEL0, () => {
// ✗ 错误:包含@和.字符
});
// 修复后
/**
* @tc.name testname001Adapt001
* @tc.number SUB_TEST_0100 ← 不修改
* @tc.desc 测试用例描述 ← 不修改
*/
it('testname001Adapt001', Level.LEVEL0, () => {
// ✓ 正确:移除了特殊字符并追加Adapt后缀
});
// 错误1:包含@和.字符
it('test.name@001', Level.LEVEL0, () => {
// ✗ 错误:包含@和.字符
});
// 错误2:包含空格
it('test name 001', Level.LEVEL0, () => {
// ✗ 错误:包含空格
});
// 错误3:包含#字符
it('test#name001', Level.LEVEL0, () => {
// ✗ 错误:包含#字符
});
// 错误4:包含中文
it('测试用例001', Level.LEVEL0, () => {
// ✗ 错误:包含中文字符
});
// 错误5:包含括号
it('test(001)', Level.LEVEL0, () => {
// ✗ 错误:包含括号
});
// 错误6:包含冒号和斜杠
it('test:api/v1', Level.LEVEL0, () => {
// ✗ 错误:包含冒号和斜杠
});
// 正确1:仅使用英文字母和数字
it('testName001', Level.LEVEL0, () => {
// ✓ 正确:符合命名规范
});
// 正确2:使用下划线分隔
it('test_name_001', Level.LEVEL0, () => {
// ✓ 正确:符合命名规范
});
// 正确3:使用连字符分隔
it('test-name-001', Level.LEVEL0, () => {
// ✓ 正确:符合命名规范
});
// 正确4:混合使用字母、数字、下划线和连字符
it('testFunc_API_v2-001', Level.LEVEL0, () => {
// ✓ 正确:符合命名规范
});
{
'rule': 'R016',
'type': 'testcase命名规范',
'severity': 'Warning',
'file': 'rel/path.test.ets',
'line': 25,
'testcase': 'test.name@001',
'snippet': "it('test.name@001', Level.LEVEL0, () => {",
'suggestion': '路径: rel/path.test.ets, 行号: 25, 问题描述: testcase名称 \'test.name@001\' 包含特殊字符。仅允许英文字母、数字、下划线、连字符。修复: 将it()参数和@tc.name修改为 \'testname001Adapt001\'。'
}
| 列序 | 列名 | 示例 |
|------|------|------|
| 1 | 问题ID | R016 |
| 2 | 问题类型 | testcase命名规范 |
| 3 | 严重级别 | Warning |
| 4 | 文件路径 | web/DFX/log/entry/src/ohosTest/ets/test/Test.test.ets |
| 5 | 行号 | 25 |
| 6 | 所属用例 | test.name@001 |
| 7 | 代码片段 | it('test.name@001', Level.LEVEL0, () => { |
| 8 | 修复建议 | 路径: ..., 行号: 25, 问题描述: testcase名称 'test.name@001' 包含特殊字符... |
有些文件中@tc.name的值与it()第一个参数不一致,此时应以@tc.name为准进行检测。
修复时只修改it()的第一个参数和对应的@tc.name注解值,不得修改:
@tc.number - 用例编号@tc.desc - 用例描述@tc.size - 用例规模@tc.type - 用例类型@tc.level - 用例级别追加Adapt后缀后,需要在当前文件范围内检查是否与已有testcase名称冲突。如果冲突,数字递增。
如果移除所有特殊字符后名称为空(如@#$%),则使用unnamedTest作为基础名称,再追加Adapt后缀。
testXxx或IT_xxx格式",用正则 ^(test|IT|it)[A-Z]\w*$ 做格式匹配。^[a-zA-Z0-9_-]+$ 做正向字符集匹配。详见 references/TRAPS.md 陷阱8。
R016的检测对象是it()的第一个参数,不是@tc.name注解的值。@tc.name仅在修复阶段同步修改。
详见 references/TRAPS.md 陷阱9。
来源: EXAMPLES.md
以下为补充示例(与上方已有示例互补):
错误示例:
// 错误:包含特殊字符
it('test.name@001', () => { // ✗ 错误:包含@字符
console.info('test001');
});
// 错误:包含空格
it('test name 001', () => { // ✗ 错误:包含空格
console.info('test001');
});
// 错误:包含其他特殊字符
it('test#name001', () => { // ✗ 错误:包含#字符
console.info('test001');
});
正确示例:
// 正确:仅使用英文字母、数字、下划线和连字符
it('test_name_001', () => { // ✓ 正确:符合命名规范
console.info('test001');
});
it('testName001', () => { // ✓ 正确:符合命名规范
console.info('test001');
});
it('test-name-001', () => { // ✓ 正确:符合命名规范
console.info('test001');
});
来源: IMPLEMENTATION_DETAILS.md
命名规范: testcase名称只能包含英文字母、数字、下划线、连字符
def check_testcase_naming(tc_name: str):
# 只允许英文字母、数字、下划线、连字符
if not re.match(r'^[a-zA-Z0-9_-]+$', tc_name):
report_issue("testcase名称包含特殊字符")
来源: V3_UPGRADE_GUIDE.md
development
Run local code quality checks covering a subset of OpenHarmony gate CI (copyright, CodeArts C/C++) plus additional local checks (pylint/flake8, shellcheck/bashate, gn format). Use before committing to reduce gate failures. Triggers on: /oh-precommit-codecheck, "门禁检查", "门禁预检", "检查代码", "run codecheck", "check code quality", "lint my code", "代码检查", or after completing code implementation. WHEN to use: before git commit, before creating PR, after modifying C/C++/Python/Shell/GN files, when gate CI fails with codecheck defects, or when you want to preview what gate will flag.
development
OpenHarmony PR full lifecycle workflow. Five modes: - Commit: standardized commit with DCO sign-off and Issue linking - Create PR: commit + push to fork + create Issue + create PR on upstream - Fix Codecheck: fetch gate CI codecheck defects from a PR and auto-fix them - Review PR: fetch a PR's changes to local for code review - Fix Review: fetch unresolved review comments from a PR and auto-fix them Triggers on: /oh-pr-workflow, "提交代码", "创建PR", "提个PR", "commit", "修复告警", "修复门禁", "修复codecheck", "fix codecheck", "review pr", "review这个pr", "看下这个pr", "检视pr", "修复review", "修复检视意见", "fix review", or a GitCode PR URL with fix/review intent.
testing
分析 HM Desktop PRD 文档,提取需求信息、验证完整性、检查章节顺序(需求来源→需求背景→需求价值分析→竞品分析→需求描述)、检查 KEP 定义、检测需求冲突并生成结构化分析报告。适用于用户请求:(1) 分析或审查 PRD 文档, (2) 从需求中提取 KEP 列表, (3) 检查 PRD 完整性或一致性, (4) 将需求映射到模块架构, (5) 验证 PRD 格式合规性, (6) 验证竞品分析章节完整性。关键词:PRD分析, requirement extraction, KEP验证, completeness check, chapter order validation, 竞品分析检查, analyze PRD, 需求提取, 完整性检查, 章节顺序验证
development
基于 PRD 文档自动生成鸿蒙系统设计文档,包括架构设计文档和功能设计文档。生成前会分析 OpenHarmony 存量代码结构,确保与现有架构兼容。架构设计文档第2章必须为竞品方案分析,位于需求背景之后。适用于用户请求:(1) 生成架构设计文档, (2) 生成功能设计文档, (3) 从 PRD 生成设计文档, (4) 创建系统架构设计, (5) 编写功能规格说明, (6) 分析 OH 代码结构。关键词:architecture design, functional design, design doc, 竞品方案分析, OpenHarmony code analysis, 架构设计, 功能设计, 设计文档生成, OH代码分析, analyze codebase, competitor analysis