HTTPRunner实践二——数据驱动
基于文档http://cn.httprunner.org/advanced/data-driven/
场景一:用例集+独立参数+参数列表
1、原始用例文件savePatient_01.yml
- config: name: 患者管理-新增患者 request: base_url: 'http://192.168.14.76:8080' headers: User-Agent: null variables: [] - test: name: /myinfo/pinyin4j/converterToFirstSpell request: method: GET params: detail: '{"body":{"synCode":"","content":{},"param":{"name":"施耐庵"},"code":{}},"header":{"v":"v_pc_1.0.3.24","doctorMainId":"ff80808162bc9a2f0162f1c2322627ab","clinicId":"8a9a9c745f9b87c3015fdcc1e36b4271","type":"4","imei":"661D-6DF1-B196-3DFE-DE7D-3A01-3AA1-471F","userToken":"20180803105231387f8ueqor"}}' url: http://192.168.14.76:8080/myinfo/pinyin4j/converterToFirstSpell validate: - eq: - status_code - 200 - eq: - headers.Content-Type - text/html;charset=UTF-8
2、分析
需要参数化的数据,患者姓名name,如上红色加粗部分。
步骤
1、在config模块中,将需要参数化的数据name,配置为全局参数,并使用列表赋值。
parameters: - name: ["杜甫", "曹雪芹", "施耐庵"]
2、在test请求中引用name的地方,使用参数替换${name}
params: detail: '{"body":{"synCode":"","content":{},"param":{"name":"$name"},"code":{}},"header":{"v":"v_pc_1.0.3.24","doctorMainId":"ff80808162bc9a2f0162f1c2322627ab","clinicId":"8a9a9c745f9b87c3015fdcc1e36b4271","type":"4","imei":"661D-6DF1-B196-3DFE-DE7D-3A01-3AA1-471F","userToken":"20180803105231387f8ueqor"}}' url: http://192.168.14.76:8080/myinfo/pinyin4j/converterToFirstSpell
3、断言中如有需要验证name的地方,也进行替换
validate: - eq: - status_code - 200 - eq: - content.body.code - "0000" - eq: - content.body.param.name - $name
全部代码实例:
1 - config: 2 name: 患者管理-新增患者 3 request: 4 base_url: 'http://192.168.14.76:8080' 5 headers: 6 User-Agent: null 7 variables: [] 8 parameters: 9 - name: ["杜甫", "曹雪芹", "施耐庵"] 10 - test: 11 name: /myinfo/pinyin4j/converterToFirstSpell 12 request: 13 method: GET 14 params: 15 detail: '{"body":{"synCode":"","content":{},"param":{"name":"$name"},"code":{}},"header":{"v":"v_pc_1.0.3.24","doctorMainId":"ff80808162bc9a2f0162f1c2322627ab","clinicId":"8a9a9c745f9b87c3015fdcc1e36b4271","type":"4","imei":"661D-6DF1-B196-3DFE-DE7D-3A01-3AA1-471F","userToken":"20180803105231387f8ueqor"}}' 16 url: http://192.168.14.76:8080/myinfo/pinyin4j/converterToFirstSpell 17 validate: 18 - eq: 19 - status_code 20 - 200 21 - eq: 22 - content.body.code 23 - "0000" 24 - eq: 25 - content.body.param.name 26 - $name
4、运行代码,查看结果
场景二、用例集+关联参数+参数列表
1、找到需要参数化的信息
姓名-缩写-出生日期-性别
2、config中配置参数
parameters: - userName-userShortName-birthday-sex: - ["李白","LB","1988-09-09",1] - ["白居易","BJY","1978-05-09",1] - ["杜牧","DM","1998-03-09",1]
3、替换参数
request: method: GET params: detail: '{"body":{"synCode":"","content":{},"param":{"id":"9cc01ecefb784f03bfcc5a87c3ad6a71","userName":"$userName","userShortName":"$userShortName","birthday":"$birthday","sex":$sex,"phone":"18911572511","isDelete":0,"illHistory":"","allergicHistory":"","address":"","homeAddress":"","base_version":"","totalArrears":"0","clinicId":"","doctorId":"","idCardNo":"","agency":"","folk":"","validitytime":"","fingerPrint":"0","deviceType":"0","diagnose":"","patientSource":""},"code":{}},"header":{"v":"v_pc_1.0.3.24","doctorMainId":"ff80808162bc9a2f0162f1c2322627ab","clinicId":"8a9a9c745f9b87c3015fdcc1e36b4271","type":"4","imei":"661D-6DF1-B196-3DFE-DE7D-3A01-3AA1-471F","userToken":"20180803105231387f8ueqor"}}' url: http://192.168.14.76:8080/patient/clinicPatient/savePatientFromPad
4、优化断言
validate: - eq: - status_code - 200 - eq: - content.body.code - "0000" - eq: - content.body.param.userName - $userName
完整代码
1 - config: 2 name: 患者管理-新增患者 3 parameters: 4 - userName-userShortName-birthday-sex: 5 - ["李白","LB","1988-09-09",1] 6 - ["白居易","BJY","1978-05-09",1] 7 - ["杜牧","DM","1998-03-09",1] 8 request: 9 base_url: 'http://192.168.14.76:8080' 10 headers: 11 User-Agent: null 12 variables: [] 13 14 15 - test: 16 name: /patient/clinicPatient/savePatientFromPad 17 request: 18 method: GET 19 params: 20 detail: '{"body":{"synCode":"","content":{},"param":{"id":"9cc01ecefb784f03bfcc5a87c3ad6a71","userName":"$userName","userShortName":"$userShortName","birthday":"$birthday","sex":$sex,"phone":"18911572511","isDelete":0,"illHistory":"","allergicHistory":"","address":"","homeAddress":"","base_version":"","totalArrears":"0","clinicId":"","doctorId":"","idCardNo":"","agency":"","folk":"","validitytime":"","fingerPrint":"0","deviceType":"0","diagnose":"","patientSource":""},"code":{}},"header":{"v":"v_pc_1.0.3.24","doctorMainId":"ff80808162bc9a2f0162f1c2322627ab","clinicId":"8a9a9c745f9b87c3015fdcc1e36b4271","type":"4","imei":"661D-6DF1-B196-3DFE-DE7D-3A01-3AA1-471F","userToken":"20180803105231387f8ueqor"}}' 21 url: http://192.168.14.76:8080/patient/clinicPatient/savePatientFromPad 22 validate: 23 - eq: 24 - status_code 25 - 200 26 - eq: 27 - content.body.code 28 - "0000" 29 - eq: 30 - content.body.param.userName 31 - $userName
5、运行查看结果
场景三、用例集+关联参数+CSV文件
1、特别注意:
对于 CSV 数据文件,需要遵循如下几项约定的规则:
- 文件需放置在与测试用例文件相同的目录中;
- CSV 文件中的第一行必须为参数名称,从第二行开始为参数值,每个(组)值占一行;
- 若同一个 CSV 文件中具有多个参数,则参数名称和数值的间隔符需实用英文逗号。
- 文件中如何含有中文,需要保存为UTF-8格式
2、步骤
1)新建CSV文件,命名为patientInfo.csv,编码方式UTF-8
userName,userShortName,birthday,sex
莫言,MY,2011-01-09,1
吴佩慈,WPC,1996-09-08,2
2) 在config模块中,配置参数
parameters:
- userName-userShortName-birthday-sex: ${P(patientInfo.csv)}
3)在test模块中,替换参数
request: method: GET params: detail: '{"body":{"synCode":"","content":{},"param":{"id":"9cc01ecefb784f03bfcc5a87c3ad6a71","userName":"$userName","userShortName":"$userShortName","birthday":"$birthday","sex":$sex,"phone":"18911572511","isDelete":0,"illHistory":"","allergicHistory":"","address":"","homeAddress":"","base_version":"","totalArrears":"0","clinicId":"","doctorId":"","idCardNo":"","agency":"","folk":"","validitytime":"","fingerPrint":"0","deviceType":"0","diagnose":"","patientSource":""},"code":{}},"header":{"v":"v_pc_1.0.3.24","doctorMainId":"ff80808162bc9a2f0162f1c2322627ab","clinicId":"8a9a9c745f9b87c3015fdcc1e36b4271","type":"4","imei":"661D-6DF1-B196-3DFE-DE7D-3A01-3AA1-471F","userToken":"20180803105231387f8ueqor"}}' url: http://192.168.14.76:8080/patient/clinicPatient/savePatientFromPad
4)运行代码,查看结果
完整代码实例
- config: name: 患者管理-新增患者 parameters: - userName-userShortName-birthday-sex: ${P(patientInfo.csv)} request: base_url: 'http://192.168.14.76:8080' headers: User-Agent: null variables: [] - test: name: /patient/clinicPatient/savePatientFromPad request: method: GET params: detail: '{"body":{"synCode":"","content":{},"param":{"id":"9cc01ecefb784f03bfcc5a87c3ad6a71","userName":"$userName","userShortName":"$userShortName","birthday":"$birthday","sex":$sex,"phone":"18911572511","isDelete":0,"illHistory":"","allergicHistory":"","address":"","homeAddress":"","base_version":"","totalArrears":"0","clinicId":"","doctorId":"","idCardNo":"","agency":"","folk":"","validitytime":"","fingerPrint":"0","deviceType":"0","diagnose":"","patientSource":""},"code":{}},"header":{"v":"v_pc_1.0.3.24","doctorMainId":"ff80808162bc9a2f0162f1c2322627ab","clinicId":"8a9a9c745f9b87c3015fdcc1e36b4271","type":"4","imei":"661D-6DF1-B196-3DFE-DE7D-3A01-3AA1-471F","userToken":"20180803105231387f8ueqor"}}' url: http://192.168.14.76:8080/patient/clinicPatient/savePatientFromPad validate: - eq: - status_code - 200 - eq: - content.body.code - "0000" - eq: - content.body.param.userName - $userName
场景四、用例+关联参数+CSV文件
将场景三中,config中的参数配置,移到到test模块即可
场景五、用例集+关联参数+自定义函数生成列表
特别注意:需要注意的是,在自定义函数中,生成的参数列表必须为 list of dict
的数据结构,该设计主要是为了与 CSV 文件的处理机制保持一致。
1、在 debugtalk.py 中定义函数 get_patient_info,可生成指定数量的参数列表。
def get_patient_info(num): patients = [] for index in range(1, num+1): patients.append( {"userName": gen_random_name(), "sex": gen_random_sex(),"birthday":gen_random_date()}, ) return patients
其中:gen_random_name()、gen_random_sex()、gen_random_date()是上面定义的方法
那么在 YAML/JSON 的 parameters 中就可以调用自定义函数生成指定数量的参数列表。
parameters:
- userName-sex-birthday-sex: ${get_patient_info(3)}