以企業(yè)微信API進行實戰(zhàn)
- 企業(yè)微信API官網(wǎng):https://work.weixin.qq.com/api/doc
Swagger測試流程
- API管理工具:https://swagger.io/
- swagger editor:可以編輯api
- swagger codegen:后端用于打樁
- swagger ui:查看api
- 接口規(guī)范:前端開發(fā)医咨、后端開發(fā)枫匾、產(chǎn)品、測試
- 前端開發(fā):使用generate server生成一個stub
- 后端開發(fā):使用generate client生成一個調(diào)用的client sdk
- 測試:使用swagger ui瀏覽拟淮、了解api干茉,自動生成基礎(chǔ)測試用例并完善豐富更多用例
測試企業(yè)微信標簽管理接口
-
獲取corpid:
-
獲取corpsecret:
獲取token
r = requests.get(
"https://qyapi.weixin.qq.com/cgi-bin/gettoken",
params={"corpid": corpid, "corpsecret": corpsecret}
)
token = r.json()["access_token"]
- 創(chuàng)建標簽初步代碼框架:
import json
import requests as requests
corpid = "" # 企業(yè)id
corpsecret = ""
def test_tag_get():
"""獲取token"""
r = requests.get(
"https://qyapi.weixin.qq.com/cgi-bin/gettoken",
params={"corpid": corpid, "corpsecret": corpsecret}
)
token = r.json()["access_token"]
"""創(chuàng)建標簽"""
url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag"
r = requests.post(
url,
params={"access_token":token},
json={
"group_id": "GROUP_ID",
"group_name": "GROUP_NAME",
"order": 1,
"tag": [{
"name": "TAG_NAME_1",
"order": 1
},
{
"name": "TAG_NAME_2",
"order": 2
}
],
"agentid" : 1000014
}
)
print(r.json())
print(json.dumps(r.json(), intent=2)) # intent=2 會讓結(jié)果間隔兩個空格
assert r.status_code == 200
assert r.json()['errcode'] == 0
- 現(xiàn)存問題:
- 代碼冗余
- 與底層框架耦合太多
- 封裝層次不足,不利于管理
API Object
- po層:業(yè)務(wù)管理很泊、業(yè)務(wù)功能的封裝
- testcase:與底層無關(guān)角虫,只是業(yè)務(wù)功能的調(diào)用
- tag.py
#!/user/bin/env python
# -*- coding: utf-8 -*-
import json
import requests
corpid = "" # 企業(yè)id
corpsecret = ""
class Tag:
def __init__(self):
self.token = ""
def get_token(self):
"""獲取token"""
r = requests.get(
"https://qyapi.weixin.qq.com/cgi-bin/gettoken",
params={"corpid": corpid, "corpsecret": corpsecret}
)
self.token = r.json()["access_token"]
def list(self):
"""獲取tag列表"""
r = requests.get(
"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list",
params={"access_token=": self.token},
json={
"tag_id":[]
}
)
print(json.dumps(r.json(), intent=2)) # intent=2 會讓結(jié)果間隔兩個空格
return r
def add(self, group_name, tags):
url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag"
r = requests.post(
url,
params={"access_token": self.token},
json={
"group_id": "GROUP_ID",
"group_name": group_name,
"order": 1,
"tag": tags,
"agentid": 1000014
}
)
# print(r.json())
print(json.dumps(r.json(), intent=2)) # intent=2 會讓結(jié)果間隔兩個空格
return r
- test_tag.py
#!/user/bin/env python
# -*- coding: utf-8 -*-
import pytest
from Exercises.service.tag import Tag
# todo: 代碼冗余
# todo: 與底層架構(gòu)耦合太多
# todo: 封裝層次不足,不利于管理
class TestTag:
def setup_class(self):
# todo: 數(shù)據(jù)清理過程委造,把測試數(shù)據(jù)清空或還原
self.tag = Tag()
self.tag.get_token()
def test_tag_list(self):
"""獲取標簽列表"""
r = self.tag.list()
assert r.status_code == 200
assert r.json()["errcode"] == 0
@pytest.mark.parametrize("group_name, tag_names", [
["group1", [{"name": "tag1"}]],
["group2", [{"name": "tag2"}, {"name": "tag3"}]]
])
def test_tag_get(self, group_name, tag_names):
# todo: 完善功能測試
"""創(chuàng)建標簽"""
r = self.tag.add(group_name=group_name, tags=tag_names)
assert r.status_code == 200
assert r.json()['errcode'] == 0
"""校驗列表"""
r = self.tag.list()
assert r.status_code == 200
assert r.json()["errcode"] == 0
group = [group for group in r.json()["tag_group"] if group["group_name"] == group_name][0]
tags = [{"name": tag['name']} for tag in r.json()["tag"]]
print(group)
print(tags)
assert group['group_name'] == group_name
assert tags == tag_names
def test_tag_fail(self):
pass
Tips:在Pycharm中將重復(fù)的數(shù)據(jù)創(chuàng)建變量的方法