# NOTE: Generated By HttpRunner v3.1.6 # FROM: har\.\xiaoqu.har import allure from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase class TestCaselogin(HttpRunner): config = Config("testcase description").verify(False).variables(**{"url": "http://iot.homecommunity.cn"}) teststeps = [ Step(RunRequest("============登录============").post("${url}/api/user/login") .with_json( {'username': 'admin2', 'password': 'hc123456'}) .extract().with_jmespath("body.data.token", "token").validate().assert_equal( "body.msg", "成功", "返回登录成功").assert_equal("body.code", 0, "返回code正确"))] if __name__ == '__main__': TestCaselogin().test_start()我要做登录账号密码的数据驱动,实现多条验证case该怎么写
时间: 2024-04-05 12:32:19 浏览: 120
你可以使用 HttpRunner 的数据驱动功能,将多组账号密码数据存储在 Yaml 文件中,然后在测试用例中通过数据驱动来实现多条验证 case。示例代码如下:
```python
# -*- coding: utf-8 -*-
# 该代码仅为示例,具体实现需要根据你的测试环境和需求进行修改
import os
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase, RunTestSuite
class TestLogin(HttpRunner):
config = Config("登录接口测试")
testcases = []
# 读取 Yaml 文件中的数据
with open(os.path.join(os.getcwd(), "test_data.yaml"), encoding='utf-8') as f:
test_data = yaml.safe_load(f)
# 循环遍历数据,生成多条测试用例
for data in test_data:
testcase = {
"name": data["case_name"],
"teststeps": [
Step(
RunRequest("登录接口")
.post("${config.host}/api/user/login")
.with_headers(**{"Content-Type": "application/json"})
.with_json(data["request_body"])
.extract()
.with_jmespath("body.data.token", "token")
.validate()
.assert_equal("body.code", 0, "返回 code 正确")
.assert_equal("body.msg", "成功", "返回登录成功")
)
]
}
testcases.append(testcase)
if __name__ == "__main__":
TestLogin().test_start()
```
其中,`test_data.yaml` 文件中的数据格式如下:
```yaml
- case_name: "用户名和密码正确"
request_body:
username: "admin"
password: "123456"
- case_name: "用户名正确密码错误"
request_body:
username: "admin"
password: "123"
- case_name: "用户名错误密码正确"
request_body:
username: "admin123"
password: "123456"
```
你可以根据实际需要修改数据格式和测试用例名称。
阅读全文