HttpRunner httprunrt 参数化没有效果

天空的城 · 2022年01月06日 · 最后由 QingShan 回复于 2022年01月17日 · 1133 次阅读

想不明白,请教大佬,第二条用例怎么取的值还是 test1

# NOTE: Generated By HttpRunner v3.1.6
# FROM: testsuites/admin/医生端管理/queryDoctorInfo_suites.yaml


import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))


import pytest
from httprunner import Parameters


from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

from testcases.admin.医生端管理.queryDoctorInfo_test import (
    TestCaseQuerydoctorinfo as Querydoctorinfo,
)


class TestCaseQuerydoctorinfoSuites(HttpRunner):
    @user1ize(
        "param", Parameters({"user": ["test1", "test2", "test3", "test4"]})
    )
    def test_start(self, param):
        super().test_start(param)
        print(param)

    config = Config("查询医生信息")

    teststeps = [
        Step(
            RunTestCase("查询医生信息")
            .with_variables(**{"user": "$user"})
            .call(Querydoctorinfo)
        ),
    ]


if __name__ == "__main__":
    TestCaseQuerydoctorinfoSuites().test_start()

```py




ASSED [ 50%]{'user': 'test2'}
2022-01-06 17:15:33.282 | INFO | httprunner.runner:test_start:451 - Start to run testcase: 查询医生信息, TestCase ID: ae983c31-8f6b-4c3f-96d5-fb09cf3dee41
2022-01-06 17:15:33.283 | INFO | httprunner.runner:run_step:292 - run step begin: 查询医生信息 >>>>>>
2022-01-06 17:15:33.284 | INFO | httprunner.runner:
run_step:292 - run step begin: get_token >>>>>>
2022-01-06 17:15:33.284 | INFO | httprunner.runner:__run_step:292 - run step begin: login >>>>>>
2022-01-06 17:15:33.510 | DEBUG | httprunner.client:request:186 - client IP: 192.168.1.136, Port: 53858
2022-01-06 17:15:33.512 | DEBUG | httprunner.client:request:194 - server IP: 120.79.213.204, Port: 443
2022-01-06 17:15:33.513 | DEBUG | httprunner.client:log_print:40 -

2022-01-06 17:15:33.518 | INFO | httprunner.runner:__run_step:292 - run step begin: 查询医生信息 >>>>>>
2022-01-06 17:15:33.584 | DEBUG | httprunner.client:request:186 - client IP: 192.168.1.136, Port: 53858
2022-01-06 17:15:33.585 | DEBUG | httprunner.client:request:194 - server IP: 120.79.213.204, Port: 443
2022-01-06 17:15:33.585 | DEBUG | httprunner.client:log_print:40 -
================== request details ==================
method : POST
url : https://api.roujjk.com/jeecg/doctor/queryDoctorInfo
headers : {
"User-Agent": "python-requests/2.27.0",
"Accept-Encoding": "gzip, deflate",
"Accept": "/",
"Connection": "keep-alive",
"X-Access-Token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NDE0NjQxMzIsInVzZXJuYW1lIjoiQWRtaW5Bc3Npc3RhbnQifQ.JMDu-C1qGDbUVOagmK01LhKcH-xWpIpu-thR4rDxs-w",
"HRUN-Request-ID": "HRUN-ae983c31-8f6b-4c3f-96d5-fb09cf3dee41-533519",
"Cookie": "acw_tc=0bc676ac16414605333621916ec21ab34582f87b295bc52c6482d188755414",
"Content-Length": "24",
"Content-Type": "application/json"
}
cookies : {
"acw_tc": "0bc676ac16414605333621916ec21ab34582f87b295bc52c6482d188755414"
}
body : {
"searchValue": "test1"
}

2022-01-06 17:15:33.586 | DEBUG | httprunner.client:log_print:40 -
================== response details ==================
status_code : 200
headers : {
"Date": "Thu, 06 Jan 2022 09:15:33 GMT",
"Content-Type": "application/json;charset=UTF-8",
"Transfer-Encoding": "chunked",
"Connection": "keep-alive",
"Server": "openresty/1.19.3.1",
"Access-Control-Allow-Methods": "GET,POST,OPTIONS,PUT,DELETE"
}
cookies : {}
encoding : UTF-8
content_type : application/json;charset=UTF-8
body : {
"success": true,
"message": "操作成功!",
"code": 200,
"result": {
"count": 0,
"dataSet": []
},
"data": null,
"timestamp": 1641460533598
}

2022-01-06 17:15:33.586 | INFO | httprunner.client:request:218 - status_code: 200, response_time(ms): 65.09 ms, response_length: 0 bytes
2022-01-06 17:15:33.587 | INFO | httprunner.response:validate:249 - assert status_code equal 200(int) ==> pass
2022-01-06 17:15:33.587 | INFO | httprunner.response:validate:249 - assert body.code equal 200(int) ==> pass
2022-01-06 17:15:33.587 | INFO | httprunner.response:validate:249 - assert body.message equal 操作成功!(str) ==> pass
2022-01-06 17:15:33.588 | INFO | httprunner.runner:__run_step:304 - run step end: 查询医生信息 <<<<<<

2022-01-06 17:15:33.588 | INFO | httprunner.runner:__run_step:304 - run step end: 查询医生信息 <<<<<<

2022-01-06 17:15:33.588 | INFO | httprunner.runner:test_start:460 - generate testcase log: /Users/huangbinbin/Desktop/roujik-test/http-roujjk/logs/ae983c31-8f6b-4c3f-96d5-fb09cf3dee41.run.log


共收到 17 条回复 时间 点赞
@user1ize("param", Parameters({"title-semesterName-attendTime-readPlanId-recruitEndTime-recruitStartTime-fileUrl-msg":"${add_semester()}"}))
#     def test_start(self, param):
#         super().test_start(param)
ChasingV 回复

看不懂,注释下面会报错,也没有 user1ize 这个装饰器

看不懂,user1ize 是啥装饰器啊,你自己写的?参数化那个不是@parameterize么?

我去催饭 回复

拷贝错了...

我去催饭 回复

注释下面会报错

官方参数化的写法

class TestCaseRequestWithParameters(HttpRunner):
    @user1ize(
        "param",
        Parameters(
            {
                "user_agent": ["iOS/10.1", "iOS/10.2"],
                "username-password": "${parameterize(request_methods/account.csv)}",
                "app_version": "${get_app_version()}",
            }
        ),
    )
    def test_start(self, param):
        super().test_start(param)

    config = (
        Config("request methods testcase: validate with parameters")
        .variables(**{"app_version": "f1"})
        .base_url("https://postman-echo.com")
        .verify(False)
    )

demo:
https://github.com/httprunner/httprunner/blob/master/examples/postman_echo/request_methods/request_with_parameters_test.py

赵晨成 回复

我是按照官方的例子写的

赵晨成 回复

看截图,第二条用例断言应该是失败,密码是错误的, 但它执行成功了

天空的城 回复

看日志呢?
是两次请求的参数一致吗?
我在本地试的是可以的!

赵晨成 回复

是有一个包问题,回退就解决了

赵晨成 回复

我现在考虑用哪个版本,你能给我一个建议吗

建议最新版本 V3.x
同时在使用框架,如果要深度使用,个人建议,在了解官方文档的后,能够看下源码~简单的了解下如何实现。
可以参考我的一个对 httprunner 框架的理解。

https://github.com/zhaochencheng/httprunner_pro
赵晨成 回复

在官方文档中,参数化是这么写的, 实际中写 testcases 不会生效,能否留个联系方式,这里沟通不方便


config:
  name: test suite demo
  base_url: "https://postman-echo.com"

testcases:
-
  name: test case 1
  parameters:
    username-password:
      - [ "user1", "111111" ]
      - [ "user2", "222222" ]
      - [ "user3", "333333" ]
  testcase: /path/to/testcase1

天空的城 回复

你可以看我个人主页,添加联系方式~

仅楼主可见
QingShan 回复

pydantic 回退到 1.8.2 就可以了

仅楼主可见
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册