求助:接口直播2,删除标签方法中,运行后代码报错

运行代码1中的,test_delete_tag方法,参数: [“gruop_delete_demo3”, [{“name”: “tag_delete_demo44”}],True],这个参数是报错,跟随老师视频已经在def delete方法中,添加了相应的设置。麻烦老师看下,代码在下方

{
  "errcode": 0,
  "errmsg": "ok. Warning: wrong json format. "
}

代码1:

# -*- coding: utf-8 -*-
# @Time  :2021/2/2 下午12:58
# @Author : caohe
# @File :test_tag1.py
import pytest


#再次封装
from interface_shizhan2.tag import Basepage1


class  Testtag():
    def setup_class(self):
        #数据清理的过程,把测试数据清空或者还原

        self.tag = Basepage1()
        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_name",[["group_demo_0202",[{"name": "tag_name0202"}]],
                                                    ["group_demo_02023",[{"name": "tag_name04502"}]]])
    def test_tag_add(self,group_name,tag_name):
        #数据不可重复,需要数据清理的过程
        # group_name = "group_demo_0202"
        # tag_name = [{"name": "tag_name0202"}]
        r=self.tag.add_tag(group_name, tag_name)
        assert r.status_code==200
        #assert r.json()["errcode"]==0
        r =self.tag.list()
        assert  r.status_code==200
        group=[group for group in r.json()["tag_group"] if group["group_name"] == group_name][0]
        tags=[{"name":tag["name"]} for  tag in group["tag"]]
        print(group)
        print(tags)
        assert group["group_name"] == group_name
        assert tags == tag_name
    #使用pytest参数化,
    #增加False,True 是为了 增加一个开关,因为删除是 要么 group—id,要么tag-id
    @pytest.mark.parametrize("group_name,tag_name,need_group_id",[
        # 删除有效的单个标签
        ["gruop_delete_demo1",[{"name": "tag_delete_demo1"}],False],

        # 删除有效的多个标签

        ["gruop_delete_demo2",[{"name": "tag_delete_demo2","name": "tag_delete_demo3"}],False],
        ["gruop_delete_demo3", [{"name": "tag_delete_demo44"}],True],
        ])
    def test_delete_tag(self,group_name,tag_name,need_group_id):
        #先去创建一个标签
        #group_name="group_demo_0202232"
        #self.tag.add_tag(group_name,[{"name": "tag_name02027776"},{"name": "tag_name0202777"},{"name": "tag_name02027"}])
        self.tag.add_tag(group_name,tag_name)

        r=self.tag.list()
        #取[group_name],取tag
        group=[group for group in r.json()["tag_group"] if group["group_name"]== group_name][0]
        #拿到所有group_id
        #group=[group["group_id"] for group in r.json()["tag_group"] if group["group_name"]== group_name][0]
        #tag=[tag for tag in group["tag"]]
        #拿到所有的tag_id
        tag=[tag["id"] for tag in group["tag"]]
        #删除单个标签
        #解决"errmsg": "Warning: wrong json format. invalid tagid, hint:
        #如果是
        if need_group_id :
           group_id = group["group_id"]
           tag=None
        else:
            group_id=None
        self.tag.delete(group_id,tag)
        print("***********************")
        r=self.tag.list()
        groups = [group for group in r.json()["tag_group"] if group["group_name"] == group_name]
        assert  groups==[]

代码2

class  Basepage1:
    def __init__(self):
        self.token=""
    #获取token
    def get_token(self):
        corpid = "wwa686deb64cca6454"
        corpsecret = "Z6N4wKGTNlHSywYyNblIQttAphcOl-m-ybEsAdmVXCQ"

        r = requests.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken",
                             params={"corpid": corpid, "corpsecret": corpsecret})
        self.token=r.json()["access_token"]


    #list是返回的列表,为后续的断言做验证,相当于获取表边页面
    def  list(self):
        r=requests.post("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(),indent=2))
        return r

    def add_tag(self,group_name,tags):
        r=requests.post("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag",
                        params={"access_token":self.token},
                        json={
                            "group_name": group_name,
                            "tag":tags
                                }
                        )
        print(json.dumps(r.json(),indent=2))
        return r
    def delete(self,group_id,tag_id):
        '''
        1.解决group_id 和tag_id删除时,只能保留一个
        {
            "errcode": 0,
            "errmsg": "ok. Warning: wrong json format. "
        }
        '''
        if tag_id is None:
            json_data={
                "group_id": group_id
            }
        else:
            json_data={
                "tag_id": tag_id,
                # group_id是列表
                "group_id": group_id
            }

        r=requests.post("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag",params={"access_token":self.token},
                        json=json_data
        #                 {
        #     #tag_id是列表
        #     "tag_id": tag_id,
        #     #group_id是列表
        #     "group_id": group_id
        # }
                        # ,proxies=proxies,
        # verify=False
        )
        print("*****************************************")
        print(json.dumps(r.json(),indent=2))
        return r

这个是官方的bug,已经删除成功了,但是返回返回了Warning信息,忽略这个Warning把,主要errmsg是ok就可以啦

关闭