• Home
Name Date Size #Lines LOC

..--

testModule/06-May-2025-2,4751,892

hdc_recv_all_test.pyD06-May-20259.5 KiB295235

main.pyD06-May-20251.9 KiB5730

prepare.pyD06-May-20254.7 KiB13996

pytest.iniD06-May-2025728 2523

readme.mdD06-May-20253.1 KiB12083

requirements.txtD06-May-2025138 1010

readme.md

1# pytest 测试框架 for hdc auto test
2
3## 参数指导
4
5参数可在pytest.ini中配置,也可以在命令行中配置,优先级:命令行 > pytest.ini
6-s: 显示输出调试信息,包括print打印的信息,但是不会写入到report
7-v: 显示更详细的信息
8-n:支持多线程运行脚本(需要保持用例彼此独立)
9--reruns NUM:失败用例重跑次数
10-x:表示只要有一个用例报错,那么测试停止
11-k:模糊匹配字符串进行用例跑测
12-m: 标记用例Level执行级别
13
14## 目录结构
15
16目录结构如下:
17
18```shell
19~/hdc/test/scripts/
20├─reports # 测试报告目录
21├─resource # 测试资源文件,存放测试过程中使用到的文件,可使用prepare.py进行资源准备
22├─testModule
23|   ├─output # 命令行输出文件,例如:重定向文件,zip文件
24|   ├─test_hdc_base.py # hdc基础测试
25|   ├─test_hdc_file.py # hdc文件测试
26|   └─utils.py # 工具
27├─main.py # 测试用例执行入口
28├─prepare.py # 资源准备脚本
29├─pytest.ini # pytest配置文件
30└─requirements.txt # 依赖文件
31```
32
33## 测试用例编写
34
35### 1. 新建测试套(py文件)
36
37建议文件名格式为:test_hdc_模块名.py
38
39### 2. 编写测试用例
40
41```python
42import pytest
43from utils import GP, check_version, load_gp # 注意这里load_gp不能缺省
44
45class TestClassExample:
46    test_table = ['A', 'B', 'C']
47    @classmethod
48    def setup_class(self):
49        print("这里配置测试类执行前的操作")
50
51
52    @classmethod
53    def teardown_class(self):
54        print("这里配置测试类执行后的操作")
55
56
57    @pytest.mark.自定义标签 # 这里配置用例自定义标签,在pytest.ini中可配置自定义标签参数,以运行标记的测试用例
58    @pytest.mark.repeat(2) # 这里配置用例重跑次数
59    @check_version("Ver: 3.1.0a") # 这里配置用例最低可执行的版本,低于该版本的环境将跳过用例
60    @pytest.mark.parametrize("test_item", file_table) # 这里配置参数化,支持列表,元组,字典
61    def test_example(self, test_item):
62        assert test_item in self.test_table # 这里编写测试用例,支持assert断言
63```
64
65## 测试资源配置
66
67进入scripts目录,配置环境准备,执行以下命令,新环境执行一次资源环境配置即可,重跑无须二次执行:
68
69```shell
70python prepare.py
71```
72
73## 测试执行
74
75配置完成环境后即可执行测试用例,执行用例有以下两种方式
76
77### 方式一
78
79```shell
80python main.py
81```
82
83执行参数在pytest.main中配置
84
85### 方式二
86
87- 执行所有用例
88
89```shell
90pytest ./
91```
92
93- 执行指定目录下所有用例
94
95```shell
96pytest ./testModule/
97```
98
99- 执行指定测试文件
100
101```shell
102pytest ./testModule/test_hdc_base.py
103```
104
105- 执行指定测试用例类
106
107```shell
108pytest testModule/test_hdc_base.py::TestClassExample
109```
110
111- 执行指定测试用例
112
113```shell
114pytest testModule/test_hdc_base.py::TestClassExample::test_example
115```
116
117## 测试报告
118
119执行python main.py后,会在reports目录下生成测试报告
120