pytest + yaml 框架 -52.支持 websocket 协议

前言

v1.4.2 版本支持 websocket 协议
备注:从v1.2.4 以后新版本不再公开,新功能内部 VIP 学员可以使用,公开版本仅解决bug, 不提供新功能了。

python 操作websocket 协议

环境准备

pip3 install websocket-client
pip3 install websockets

基本代码示例

from websocket import create_connection
import json


url = 'ws://localhost:8081/ws'
ws = create_connection(url, timeout=10)  # 创建链接
print(ws.getstatus())        # 状态码
ws.send('hello world')       # 发送文本消息
print(ws.recv())             # 接收返回内容

# 发送json消息
body = {
    "name": "yoyo",
    "email": "123@qq.com"
}

ws.send(json.dumps(body))
print(ws.getstatus())
print(ws.recv())
ws.shutdown()  # 关闭连接

运行结果

101
接受到的消息是: hello world
101
接受到的消息是: {"name": "yoyo", "email": "123@qq.com"}

yaml 中 webscoket 用例实现

ws关键字相当于create_connection(url, timeout=10) 创建连接
send 关键字相当于 ws.send('hello') 发送文本消息`

test_x1:
  ws:
    url: ws://localhost:8081/ws
    timeout: 10
  send: hello
  validate:
    - eq: [status, 101]
    - eq: [text, '接受到的消息是: hello']

运行结果

collected 1 item                                                                                                        

a_ws/test_ws.yml::test_x1
---------------------------------------------------- live log call -----------------------------------------------------
2023-07-17 19:44:52 [INFO]: 执行文件-> test_ws.yml
2023-07-17 19:44:52 [INFO]: base_url-> http://124.70.221.221:8201
2023-07-17 19:44:52 [INFO]: config variables-> {}
2023-07-17 19:44:52 [INFO]: 运行用例-> test_x1
2023-07-17 19:44:53 [INFO]: 创建 websocket 链接: ws://localhost:8081/ws
2023-07-17 19:44:53 [INFO]: websocket send: hello
2023-07-17 19:44:53 [INFO]: websocket recv: 接受到的消息是: hello
2023-07-17 19:44:53 [INFO]: validate 校验内容-> [{'eq': ['status', 101]}, {'eq': ['text', '接受到的消息是: hello']}]
2023-07-17 19:44:53 [INFO]: validate 校验结果-> eq: [101, 101]
2023-07-17 19:44:53 [INFO]: validate 校验结果-> eq: [接受到的消息是: hello, 接受到的消息是: hello]
PASSED 

websocket 一次连接可以发送多个请求,所以可以在一个用例中有多个send请求

test_x2:
-
  ws:
    url: ws://localhost:8081/ws
    timeout: 10
-
  send: hello
  validate:
    - eq: [status, 101]
    - eq: [text, '接受到的消息是: hello']
-
  send:
    name: yoyo
    email: w22@qq.com
  validate:
    - eq: [status, 101]
    - eq: [text, '接受到的消息是: {"name": "yoyo", "email": "w22@qq.com"}']

多个连接地址测试

一个yaml 文件中可以有多个连接

test_x1:
-
  ws:
    url: ws://localhost:8081/ws
    timeout: 10
-
  send: hello
  validate:
    - eq: [status, 101]
    - eq: [text, '接受到的消息是: hello']
    - eq: [body, '接受到的消息是: hello']

test_x2:
  ws:
    url: ws://localhost:8081/json
    timeout: 10
  send:
    name: xx
    email: w22@qq.com
  validate:
    - eq: [status, 101]
    - eq: [$.name, xx]

多个url地址公共环境地址 base_url 也可以写的config中

config:
  base_url: ws://localhost:8081
test_x1:
-
  ws:
    url: /ws
    timeout: 10
-
  send: hello
  validate:
    - eq: [status, 101]
    - eq: [text, '接受到的消息是: hello']
    - eq: [body, '接受到的消息是: hello']

test_x2:
  ws:
    url: /json
    timeout: 10
  send:
    name: xx
    email: w22@qq.com
  validate:
    - eq: [status, 101]
    - eq: [$.name, xx]

执行结果

collected 2 items                                                                                                       

a_ws/test_ws1.yml::test_x1
-------------------------------live log call ---------------------------------------
2023-07-17 19:50:07 [INFO]: 执行文件-> test_ws1.yml
2023-07-17 19:50:07 [INFO]: base_url-> ws://localhost:8081
2023-07-17 19:50:07 [INFO]: config variables-> {}

2023-07-17 19:50:08 [INFO]: 创建 websocket 链接: ws://localhost:8081/ws
2023-07-17 19:50:08 [INFO]: websocket send: hello
2023-07-17 19:50:08 [INFO]: websocket recv: 接受到的消息是: hello
2023-07-17 19:50:08 [INFO]: validate 校验内容-> [{'eq': ['status', 101]}, {'eq': ['text', '接受到的消息是: hello']}, {'eq
': ['body', '接受到的消息是: hello']}]
2023-07-17 19:50:08 [INFO]: validate 校验结果-> eq: [101, 101]
2023-07-17 19:50:08 [INFO]: validate 校验结果-> eq: [接受到的消息是: hello, 接受到的消息是: hello]
2023-07-17 19:50:08 [INFO]: validate 校验结果-> eq: [接受到的消息是: hello, 接受到的消息是: hello]
PASSED                                                                                      [ 50%]

----------------- live log call -------------------------------------
2023-07-17 19:50:08 [INFO]: 执行文件-> test_ws1.yml
2023-07-17 19:50:08 [INFO]: base_url-> ws://localhost:8081
2023-07-17 19:50:08 [INFO]: config variables-> {}
2023-07-17 19:50:08 [INFO]: 运行用例-> test_x2
2023-07-17 19:50:09 [INFO]: 创建 websocket 链接: ws://localhost:8081/json
2023-07-17 19:50:09 [INFO]: websocket send: {'name': 'xx', 'email': 'w22@qq.com'}
2023-07-17 19:50:09 [INFO]: websocket recv: {"name": "xx", "email": "w22@qq.com", "code": 0, "time": 1689594609.5537014}
2023-07-17 19:50:09 [INFO]: validate 校验内容-> [{'eq': ['status', 101]}, {'eq': ['$.name', 'xx']}]
2023-07-17 19:50:09 [INFO]: validate 校验结果-> eq: [101, 101]
2023-07-17 19:50:09 [INFO]: validate 校验结果-> eq: [xx, xx]
PASSED                                                                                     [100%]

================== 2 passed in 2.47s ============================ 

网易云完整视频课程https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
报名咨询wx:283340479 (已报名的同学学习过程中有问题,都可以协助解决)

posted @ 2023-07-17 19:51  上海-悠悠  阅读(307)  评论(0编辑  收藏  举报