1# pytest命令行参数 2-s: 显示输出调试信息,包括print打印的信息 3-v: 显示更详细的信息 4-n:支持多线程运行脚本(需要保持用例彼此独立) 5--reruns NUM:失败用例重跑次数 6-x:表示只要有一个用例报错,那么测试停止 7-k:模糊匹配字符串进行用例跑测 8 9# 目录结构 10``` 11/hidumper/test 12├─reports # 测试报告目录 13├─testModule 14| ├─output # 命令行输出文件,例如:重定向文件,zip文件 15| ├─resource # 测试资源文件,存放测试过程中使用到的文件 16| └─tests # 测试用例目录 17| ├─test_case1.py # 测试套件1 18| ├─test_case2.py # 测试套件2 19| └─utils.py # 工具 20├─main.py # 测试用例执行入口 21├─pytest.ini # pytest配置文件 22└─requirements.txt # 依赖文件 23``` 24 25# 测试用例编写 26### 1. 新建测试套(py文件) 27``` 28# test_base_command.py 29class TestBaseCommand: 30 def test_hidumper_c_all(self): 31 # 校验命令行输出 32 output = subprocess.check_output(f"hdc shell \"hidumper -c\"", shell=True, text=True, encoding="utf-8") 33 assert check_output(output, check_function = check_hidumper_c_output) 34 35 # 校验命令行重定向输出 36 hidumper_c_file = f"{OUTPUT_PATH}/hidumper_c.txt" 37 subprocess.check_output(f"hdc shell \"hidumper -c\" > {hidumper_c_file}", shell=True, text=True, encoding="utf-8") 38 assert check_file(hidumper_c_file, check_function = check_hidumper_c_output) 39 40 # 校验命令行输出到zip文件 41 output = subprocess.check_output(f"hdc shell \"hidumper -c --zip\"", shell=True, text=True, encoding="utf-8") 42 hidumper_c_zip_source_file = re.search("The result is:(.+)", output).group(1) 43 hidumper_c_zip_target_file = f"{OUTPUT_PATH}/" + os.path.basename(hidumper_c_zip_source_file) 44 subprocess.check_output(f"hdc file recv {hidumper_c_zip_source_file} {hidumper_c_zip_target_file}", shell=True, text=True, encoding="utf-8") 45 assert check_zip_file(hidumper_c_zip_target_file, check_function = check_hidumper_c_output) 46``` 47### 2. 编写测试用例 48``` 49# test_base_command.py 50# 自定义校验函数 51@print_check_result 52def CheckOsVersion(output) -> bool: 53 # 校验OsVersion:5.0.0.1233 54 ret = re.search("OsVersion: (.+?)[\d].[\d].[\d].[\d]", output) 55 return ret is not None 56 57@print_check_result 58def CheckWakeUpSource(output) -> bool: 59 # 校验/sys/kernel/debug/wakeup_sources至少输出四行 60 ret = re.search("/sys/kernel/debug/wakeup_sources\n\n([^\n]+\n){4,}", output) 61 return ret is not None 62...... 63 64def check_hidumper_c_output(output): 65 results = [check(output) for check in [CheckBuildId, CheckOsVersion, CheckProcVersion, CheckCmdline, CheckWakeUpSource, 66 CheckUpTime, CheckPrintEnv, CheckLsmod, CheckSlabinfo, CheckZoneinfo, CheckVmstat, CheckVmallocinfo]] 67 return all(results) 68``` 69 70## 测试用例执行 71windows环境下执行测试用例: 72进入scripts目录 73### 方式一: 74 75 ``` 76 python main.py 77 ``` 78执行参数在pytest.main中配置 79 80### 方式二: 81- 执行所有用例 82 ``` 83 pytest ./ 84 ``` 85- 执行指定测试文件 86 ``` 87 pytest ./testModule/test_base_command.py 88 ``` 89- 执行指定测试用例 90 ``` 91 pytest -k "test_hidumper_c_all" 92 ``` 93- 执行模糊匹配test_hidumper_c所有用例 94 ``` 95 pytest -k "test_hidumper_c" 96 ``` 97 98> **user设备需要将预先准备好的测试hap包推入resource目录,且需要手动跳过开机阶段** 99 100## 测试报告 101执行python main.py后,会在reports目录下生成测试报告 102