测试人社区

求助:接口测试实战2,运行test_delete_tag,报错"errmsg": "Warning: wrong json format. invalid tagid, hint: ,没找到哪里出现了问题

class  Basepage1:
    def __init__(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,tag_id,group_id):
        r=requests.post("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag",params={"access_token":self.token},json={
            #tag_id是列表
            "tag_id": tag_id,
            #group_id是列表
            "group_id": group_id
        }
        )
        print(json.dumps(r.json(),indent=2))
        return r
# -*- 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参数化,
    @pytest.mark.parametrize("group_name,tag_name,need_group_id",[
        ["group_demo_039",[{"name": "tag_nam776"}],False],
                             #删除有效的单个标签
                             #删除有效的多个标签
                             #如果tag_id为空,则删除group_id,
                             ["group_demo_0202238",[{"name": "tag_name077","name": "tag_name27"}],False],
                             ["group_demo_0202238", [],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"]
        else:
            group_id=None
        self.tag.delete(group_id,tag)

运行后报错:
{
“errcode”: 40068,
“errmsg”: "Warning: wrong json format. invalid tagid, hint:

这个报错是你接口返回的错误,你发请求之前转成json格式 试试

还是不行

还是失败

检查下企业微信错误状态码说明的是啥错误,对症下药

此问题已经解决