【作业-接口自动化测试1】企业微信接口自动化测试实战-作业1

作业

  • 通讯录管理-标签管理接口,至少实现前四个

https://work.weixin.qq.com/api/doc/90000/90135/90209

作业:通讯录管理-标签管理接口,至少实现前四个

-https://work.weixin.qq.com/api/doc/90000/90135/90209

import requests

class TestDepartment:
    def setup(self):
        corpid = "ww441338b28c377647"
        corpsecret = "7-Dbm-lumcWei3dpn-1tof-JmxC1sApIIKAnhkhYVi0"
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = {
            "corpid":"ww441338b28c377647",
            "corpsecret" : "7-Dbm-lumcWei3dpn-1tof-JmxC1sApIIKAnhkhYVi0"
        }
        r = requests.get(url=url, params=params)
        self.token = r.json()['access_token']

    # def teardown(self):
    #     #  关闭应用回收资源
    #     self.driver.quit()

1、创建标签

    # 创建标签
    def test_create_label(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create"
        params = {
            "access_token": self.token
        }
        json = {
            "tagname": "惊蛰1",
            "tagid": 12
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0


image

2、更新标签名字

    # 更新标签名字
    def test_update_label(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update"
        params = {
            "access_token": self.token
        }
        json = {
            "tagid": 12,
            "tagname": "惊蛰2"
            }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0


image

3、删除标签

    # 删除标签
    def test_delete_label(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete"
        params = {
            "access_token": self.token,
            "tagid": "12"
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0


image

4、获取标签成员

    # 获取标签成员
    def  test_getlist_department(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get"
        params = {
            "access_token": self.token,
            "tagid": 12
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0


5、增加标签成员

    # 增加标签成员
    def test_add_labelmembers(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"
        params = {
            "access_token": self.token
        }
        json = {
            "tagid": 12,
            "userlist": ["a1","a2"],
             "partylist" : [1]
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0


问题:企业成员ID和企业企业ID已给值,标签正常添加了,但是有’invalidparty’: []返回
image

6、删除标签成员

    # 删除标签成员
    def test_delete_labelmembers(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers"
        params = {
            "access_token": self.token
        }
        json = {
            "tagid": 12,
            "userlist": ["a1", "a2"],
            "partylist": [1]
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0



#7、获取标签列表

    # 获取标签列表
    def test_getlist_labelmembers(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list"
        params = {
            "access_token": self.token
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0

作业-20200611

  • 通讯录管理-标签管理接口,至少实现前四个

代码

import pytest
import requests

class TestLabelManager:

    def setup(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = {
            "corpid": "wwaa5e12b39afbafe3",
            "corpsecret": "2_YAsegpEFFziREvf3GB5Vr2bIgAP7cBIWr35DN_b-s"
        }
        r = requests.get(url=url, params=params)
        self.token = r.json()["access_token"]
        print(r.json()["access_token"])
    @pytest.mark.parametrize("tagname,tagid",[("Testing",1),("Tetsing2",2),("Tetsing3",3)])
    def test_create_label(self,tagname,tagid):
        """
        *创建标签
        """
        url="https://qyapi.weixin.qq.com/cgi-bin/tag/create"
        params={
            "access_token": self.token,
        }
        json={
            "tagname": tagname,
            "tagid": int(tagid),
        }
        r=requests.post(url=url,params=params,json=json)
        print(r.json())
        assert r.json()['errcode']==0

    def test_update_label_name(self):
        """
        *更新标签名字
        """
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update"
        params = {
            "access_token": self.token,
        }
        json={
            "tagname": "Testing1",
            "tagid": 1
        }
        r = requests.post(url=url, params=params,json=json)
        print(r.json())
        assert r.json()['errcode'] == 0
    def test_delete_label(self):
        """
        *删除标签
        """
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete"
        params = {
            "access_token": self.token,
            "tagid": 3,
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()['errcode'] == 0

    def test_get_label_member(self):
        """
        *获取标签成员
        """
        url="https://qyapi.weixin.qq.com/cgi-bin/tag/get"
        params={
            "access_token" : self.token,
            "tagid": 1
        }
        r=requests.get(url=url,params=params)
        print(r.json())
        assert r.json()['errcode'] == 0

    def test_add_label_member(self):
        """
        *增加标签成员
        """
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"
        params = {
            "access_token": self.token,
        }
        json={
            "tagid": 1,
            "userlist": ["cjg1","cjg2","cjg3"],
        }
        r = requests.post(url=url, params=params,json=json)
        print(r.json())
        assert r.json()['errcode'] == 0

    def test_delete_label_member(self):
        """
        *删除标签成员
        """
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers"
        params = {
            "access_token": self.token,
        }
        json={
            "tagid": 1,
            "userlist": ["cjg3"]
        }
        r = requests.post(url=url, params=params,json=json)
        print(r.json())
        assert r.json()['errcode'] == 0
    def test_getlist_label(self):
        """
        *获取标签列表
        """
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list"
        params = {
            "access_token": self.token,
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()['errcode'] == 0

执行结果

============================= test session starts =============================
platform win32 -- Python 3.8.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- G:\python\SET_hogwarts\venv\Scripts\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.3', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '5.4.2', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.13', 'assume': '2.2.1', 'dependency': '0.5.1', 'forked': '1.1.3', 'html': '2.1.1', 'metadata': '1.9.0', 'ordering': '0.6', 'rerunfailures': '9.0', 'xdist': '1.32.0'}, 'JAVA_HOME': 'G:\\Java\\jdk1.8.0_101'}
rootdir: G:\python\SET_hogwarts\python_base, inifile: pytest.ini
plugins: allure-pytest-2.8.13, assume-2.2.1, dependency-0.5.1, forked-1.1.3, html-2.1.1, metadata-1.9.0, ordering-0.6, rerunfailures-9.0, xdist-1.32.0
collecting ... [<Function test_create_label[Testing-1]>, <Function test_create_label[Tetsing2-2]>, <Function test_create_label[Tetsing3-3]>, <Function test_update_label_name>, <Function test_delete_label>, <Function test_get_label_member>, <Function test_add_label_member>, <Function test_delete_label_member>, <Function test_getlist_label>]
<class 'list'>
collected 9 items

test_label_manager.py::TestLabelManager::test_create_label[Testing-1] 
test_label_manager.py::TestLabelManager::test_create_label[Tetsing2-2] 
test_label_manager.py::TestLabelManager::test_create_label[Tetsing3-3] 
test_label_manager.py::TestLabelManager::test_update_label_name 
test_label_manager.py::TestLabelManager::test_delete_label 
test_label_manager.py::TestLabelManager::test_get_label_member 
test_label_manager.py::TestLabelManager::test_add_label_member 
test_label_manager.py::TestLabelManager::test_delete_label_member 
test_label_manager.py::TestLabelManager::test_getlist_label 

============================== 9 passed in 5.61s ==============================

Process finished with exit code 0
XvDAxDhcZjMYLj6yzgG4nwwXeH7Wu97u8FH0nxN-Z_9WHVtRV-yrayHDtj-JiRbKa6pGu1tVI8x4LGDtvXvawixc7q423ndOnZyoSpO9kTJA5C9wew0Wz5108f1IcMjtDbRbdCzqk0iCIzrsFYtbrMd_Mg_cO5a0itpKppMCbV3ad970zABnj3iuDK5UFUjL5jIjjqTVr989-pcJS57G-g
PASSED [ 11%]{'errcode': 0, 'errmsg': 'created', 'tagid': 1}
XvDAxDhcZjMYLj6yzgG4nwwXeH7Wu97u8FH0nxN-Z_9WHVtRV-yrayHDtj-JiRbKa6pGu1tVI8x4LGDtvXvawixc7q423ndOnZyoSpO9kTJA5C9wew0Wz5108f1IcMjtDbRbdCzqk0iCIzrsFYtbrMd_Mg_cO5a0itpKppMCbV3ad970zABnj3iuDK5UFUjL5jIjjqTVr989-pcJS57G-g
PASSED [ 22%]{'errcode': 0, 'errmsg': 'created', 'tagid': 2}
XvDAxDhcZjMYLj6yzgG4nwwXeH7Wu97u8FH0nxN-Z_9WHVtRV-yrayHDtj-JiRbKa6pGu1tVI8x4LGDtvXvawixc7q423ndOnZyoSpO9kTJA5C9wew0Wz5108f1IcMjtDbRbdCzqk0iCIzrsFYtbrMd_Mg_cO5a0itpKppMCbV3ad970zABnj3iuDK5UFUjL5jIjjqTVr989-pcJS57G-g
PASSED [ 33%]{'errcode': 0, 'errmsg': 'created', 'tagid': 3}
XvDAxDhcZjMYLj6yzgG4nwwXeH7Wu97u8FH0nxN-Z_9WHVtRV-yrayHDtj-JiRbKa6pGu1tVI8x4LGDtvXvawixc7q423ndOnZyoSpO9kTJA5C9wew0Wz5108f1IcMjtDbRbdCzqk0iCIzrsFYtbrMd_Mg_cO5a0itpKppMCbV3ad970zABnj3iuDK5UFUjL5jIjjqTVr989-pcJS57G-g
PASSED   [ 44%]{'errcode': 0, 'errmsg': 'updated'}
XvDAxDhcZjMYLj6yzgG4nwwXeH7Wu97u8FH0nxN-Z_9WHVtRV-yrayHDtj-JiRbKa6pGu1tVI8x4LGDtvXvawixc7q423ndOnZyoSpO9kTJA5C9wew0Wz5108f1IcMjtDbRbdCzqk0iCIzrsFYtbrMd_Mg_cO5a0itpKppMCbV3ad970zABnj3iuDK5UFUjL5jIjjqTVr989-pcJS57G-g
PASSED        [ 55%]{'errcode': 0, 'errmsg': 'deleted'}
XvDAxDhcZjMYLj6yzgG4nwwXeH7Wu97u8FH0nxN-Z_9WHVtRV-yrayHDtj-JiRbKa6pGu1tVI8x4LGDtvXvawixc7q423ndOnZyoSpO9kTJA5C9wew0Wz5108f1IcMjtDbRbdCzqk0iCIzrsFYtbrMd_Mg_cO5a0itpKppMCbV3ad970zABnj3iuDK5UFUjL5jIjjqTVr989-pcJS57G-g
PASSED    [ 66%]{'errcode': 0, 'errmsg': 'ok', 'userlist': [], 'partylist': [], 'tagname': 'Testing1'}
XvDAxDhcZjMYLj6yzgG4nwwXeH7Wu97u8FH0nxN-Z_9WHVtRV-yrayHDtj-JiRbKa6pGu1tVI8x4LGDtvXvawixc7q423ndOnZyoSpO9kTJA5C9wew0Wz5108f1IcMjtDbRbdCzqk0iCIzrsFYtbrMd_Mg_cO5a0itpKppMCbV3ad970zABnj3iuDK5UFUjL5jIjjqTVr989-pcJS57G-g
PASSED    [ 77%]{'errcode': 0, 'errmsg': 'ok', 'invalidparty': []}
XvDAxDhcZjMYLj6yzgG4nwwXeH7Wu97u8FH0nxN-Z_9WHVtRV-yrayHDtj-JiRbKa6pGu1tVI8x4LGDtvXvawixc7q423ndOnZyoSpO9kTJA5C9wew0Wz5108f1IcMjtDbRbdCzqk0iCIzrsFYtbrMd_Mg_cO5a0itpKppMCbV3ad970zABnj3iuDK5UFUjL5jIjjqTVr989-pcJS57G-g
PASSED [ 88%]{'errcode': 0, 'errmsg': 'deleted', 'invalidparty': []}
XvDAxDhcZjMYLj6yzgG4nwwXeH7Wu97u8FH0nxN-Z_9WHVtRV-yrayHDtj-JiRbKa6pGu1tVI8x4LGDtvXvawixc7q423ndOnZyoSpO9kTJA5C9wew0Wz5108f1IcMjtDbRbdCzqk0iCIzrsFYtbrMd_Mg_cO5a0itpKppMCbV3ad970zABnj3iuDK5UFUjL5jIjjqTVr989-pcJS57G-g
PASSED       [100%]{'errcode': 0, 'errmsg': 'ok', 'taglist': [{'tagid': 1, 'tagname': 'Testing1'}, {'tagid': 2, 'tagname': 'Tetsing2'}]}

作业:通讯录管理-标签管理

一、获取access_token

class TestDemo():
    def setup(self):        #定义setup方法,以便执行用例时可以获取token值
        corpid = "wwf909985f20e7bb66"
        corpsecret = "ZxzplF-X8jacr_C5Hda0WDkf2QXxQE3xijHexPaZXWw"
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        param = {
            "corpid": corpid,
            "corpsecret": corpsecret
        }
        r = requests.get(url=url, params=param)
        self.token = r.json()["access_token"]

    def teardown(self):
        pass

二、创建标签

#创建标签
    def test_creat_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create"
        params = {
            "access_token":self.token
        }
        json = {
            "tagname": "人力部",
            "tagid": 1     #非负整型,指定此参数时新增的标签会生成对应的标签id,不指定时则以目前最大的id自增
        }
        r = requests.post(url=url,params=params,json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

疑问:post属性(url, data=None, json=None, **kwargs),为什么这里只有用params才能执行成功呢?有点迷惑

image

三、获取标签列表

#获取标签列表
        def test_getlist_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list"
        params = {
            "access_token": self.token
        }
        r = requests.get(url=url, params=params)
        print(r.json())

疑问:老师为什么用get和post都能执行成功呢?

四、更新标签名

#更新标签名
    def test_update_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update"
        params = {
            "access_token": self.token
        }
        json = {
            "tagid": 1,
            "tagname": "人力行政部"
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

image

五、添加标签成员

 # 增加标签成员
    def test_insertlistnum_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"
        params = {
            "access_token": self.token
        }
        json = {
            "tagid": 1,
            "userlist": "YinYingYi"
        }
        r = requests.post(url=url, params=params,json=json)
        print(r.json())
        assert r.json()["errcode"] == 0


疑问:为什么有warnning和invalidparty

六、获取标签成员

#获取标签成员
    def test_getlistnum_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get"
        params = {
            "access_token": self.token,
            "tagid": 1
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0

七、删除标签

#删除标签
    def test_delete_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete"
        params = {
            "access_token": self.token,
            "tagid": 1
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0

image

作业 通讯录标签管理

import requests
class TestTag:
    def setup(self):
            url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
            corpid = "ww45f0b3e1f0fb5929"
            corpsecret = "7wpuu89ZzNGxuwoKVnsoC7o3YN4q38hxdbS0IGFAMU4"
            params = {
                "corpid": corpid,
                "corpsecret": corpsecret
            }

            r = requests.get(url=url, params=params)
            print(r.json()['access_token'])
            self.token = r.json()['access_token']
    #创建标签
    def test_creat_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create"
        tag_name={
           "tagname": "狮子头",
           "tagid": 13
        }
        params ={"access_token":self.token}
        r1 = requests.post( url=url,params=params ,json=tag_name)
        print(r1.json())
        assert r1.json()['errcode'] == 0
    #更新标签名称
    def test_updata_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update"
        params = {"access_token": self.token}
        updata_name={
            "tagid": 13,
            "tagname": "技术流"
        }
        r2 = requests.post(url=url,params=params,json=updata_name)

        print(r2.json())
        assert r2.json()['errcode'] == 0
    #删除标签
    def test_delete_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete"
        params = {"access_token": self.token,
                  "tagid": 11
                  }
        r3= requests.get(url=url,params=params)
        print(r3.json())
        assert r3.json()['errcode'] == 0

    #获取标签成员
    def test_gettag_member(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get"
        params = {"access_token": self.token,
                  "tagid": 12
                  }
        r4 = requests.get(url=url,params=params)
        print("--------")
        print(r4.json()['partylist'])
        print("---*****++++---")
        assert r4.json()['errcode'] == 0

    #增加标签成员
    def test_addtag_member(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"
        params = {"access_token": self.token
                  }
        member = {
            "tagid": 13,
            "userlist": ["665593","AWen"]
        }
        r5 = requests.post(url=url, params=params,json=member)
        assert r5.json()['errcode'] == 0

    #删除标签成员
    def test_deltag_member(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers"
        params = {"access_token": self.token
                  }
        member = {
            "tagid": 13,
            "userlist": ["665593", "AWen"]
        }
        r6 = requests.post(url=url, params=params,json=member)
        assert r6.json()['errcode'] == 0

#获取标签列表
    def test_gettag_memberlist(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list"
        params = {"access_token": self.token
                  }
        r7 = requests.get(url=url,params=params)
        print(r7.json())
        assert r7.json()['errcode'] == 0

代码

import requests


class TestLabel:
    # 获取token
    def setup(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = {
            "corpid":"ww170e13c876c74789",
            "corpsecret":"N5-mHIhmaAYP1-qcMaccISC0Ktw5L8YY93GDjUy7pjs"
        }
        r = requests.get(url=url, params=params)
        # print(r.json())
        self.token = r.json()["access_token"]
        # print(self.token)

    # 创建标签
    def test_create_label(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create"
        params = {
            "access_token":self.token
        }
        json = {
            "tagname": "Ui3",
            "tagid": 3
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 更新标签名字
    def test_update_label(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update"
        params = {"access_token":self.token}
        json = {
            "tagid": 3,
            "tagname": "Ui3_edit"
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 删除标签
    def test_delete_label(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete"
        params = {
            "access_token":self.token,
            "tagid":3
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 获取标签成员
    def test_get_labelmember(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get"
        params = {
            "access_token":self.token,
            "tagid":1
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 增加标签成员
    def test_create_labelmember(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"
        params = {"access_token":self.token}
        json = {
            "tagid": 1,
            "userlist": ["lisi", "lisi1"],
            "partylist": [3,4]
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 删除标签成员
    def test_delete_labelmember(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers"
        params = {"access_token":self.token}
        json = {
            "tagid": 1,
            "userlist": ["lisi", "lisi1"],
            "partylist": [3,4]
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 获取标签列表
    def test_get_labellist(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list"
        params = {"access_token":self.token}
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()['errcode'] == 0

运行截图

作业:标签管理接口

import requests
import pytest

class TestTag:
    # 在执行测试用例之前被调用,获得token
    def setup(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = {
            "corpid": "ww24dc354fa0a345cf",
            "corpsecret": "T3fYAsORyoOPMNBjJvXq_ELcXF_7inR5gNnfa-zbM-4"
        }
        r = requests.get(url=url, params=params)
        self.token = r.json()["access_token"]

    # 创建标签
    def test_create_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create"
        params = {
            "access_token": self.token
        }
        json = {
            "tagname": "MMK4",
            "tagid": 4
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 更新标签名字
    def test_update_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update"
        params = {
            "access_token": self.token
        }
        json = {
            "tagid": 3,
            "tagname": "MMK3_UPDATE"
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 删除标签
    def test_delete_tag(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete"
        params = {
            "access_token": self.token,
            "tagid": 4
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 获取标签成员
    def test_get_member_list(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get"
        params = {
            "access_token": self.token,
            "tagid": 2
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0

    # 增加标签成员
    def test_add_tag_member(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"
        params = {
            "access_token": self.token
        }
        json = {
            "tagid": 1,
            "userlist": ["user003", "user004"],
            "partylist": [88]
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0
        # 若部分userid、partylist非法,则返回4个key,errcode也是0
        keys = r.json().keys()
        assert len(keys) != 4

    # 删除标签成员
    def test_delete_tag_member(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers"
        params = {
            "access_token": self.token
        }
        json = {
            "tagid": 1,
            "userlist": ["user003", "user004"],
            "partylist": [88]
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0
        # 若部分userid、partylist非法,则返回4个key,errcode也是0
        keys = r.json().keys()
        assert len(keys) != 4

    # 获取标签列表
    def test_get_tag_list(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list"
        params = {
            "access_token": self.token
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0

    if __name__ == '__main__':
        pytest.main()

执行结果:

问题:
在 增加标签成员 和 删除标签成员 里面,如果部分userid、partylist非法,会返回4个key值,errcode也是0,多了一个key值“invalidlist”,如何直接判断 r.json()[“invalidlist”] 是不存在的呢?
我现在是在判断errcode是0之后,再判断返回的key值的个数

assert r.json()["errcode"] == 0
# 若部分userid、partylist非法,则返回4个key,errcode也是0
keys = r.json().keys()
assert len(keys) != 4

作业:通讯录管理-标签管理接口,至少实现前四个

import requests
class TestTag:
    #获取token
    def setup(self):
        param = {
            "corpid": "ww92ca933432777375",
            "corpsecret": "Owsd7rgwlZJfwz-ROVVzSBWwfP2AP8El8lAP7PRqaeQ"
        }
        url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
        r = requests.get(url=url, params=param)
        self.token=r.json()['access_token']
       # 创建标签
    def test_creat_tag(self):
        url='https://qyapi.weixin.qq.com/cgi-bin/tag/create'
        params={
            'access_token':self.token
        }
        json={
            "tagname": "UII",
            "tagid": 11
        }
        r= requests.post(url=url,params=params,json=json)
        print(r.json())
        assert r.json()['errcode']==0
        #更新标签
    def test_update_tag(self):
        url = 'https://qyapi.weixin.qq.com/cgi-bin/tag/update'
        params = {
            'access_token': self.token
        }
        json = {
            "tagid": 12,
            "tagname": "UI design"
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()['errcode'] == 0
        #删除标签
    def test_delete_tag(self):
        url = 'https://qyapi.weixin.qq.com/cgi-bin/tag/delete'
        params = {
            'access_token': self.token,
            'tagid':12
        }
        r = requests.get(url=url, params=params)
        print(r)
        assert r.json()['errcode'] == 0
        #获取标签成员
    def test_getuser_tag(self):
        url = 'https://qyapi.weixin.qq.com/cgi-bin/tag/get'
        params = {
            'access_token': self.token,
            'tagid': 11
        }
        r = requests.get(url=url, params=params)
        print(r)
        assert r.json()['errcode'] == 0
作业:通讯录管理-标签管理接口,至少实现前四个
"""
作业:企业微信通讯录管理-标签管理接口,至少实现前四个
"""

import pytest
import requests

class TestTag:

    def setup(self):
        corpid = "ww2e51e84799d1f6f7"
        corpsecret = "xMZ8h3PCTOGFp9TtjvWWfqtX--uwTdCPVcSmzRqRMn8"
        param = {
            "corpid": corpid,
            "corpsecret": corpsecret
        }
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        r = requests.get(url=url, params=param)
        # print(type(r.json())) 查看r.token是一个字典类型
        self.token = r.json()["access_token"]

    #@pytest.mark.skip
    def test_create_tag(self):
        url="https://qyapi.weixin.qq.com/cgi-bin/tag/create"
        params={
            "access_token":self.token
        }
        json={
           "tagname": "PT",
           # "tagid": 12 # 标签id,非负整型,指定此参数时新增的标签会生成对应的标签id,不指定时则以目前最大的id自增。
        }
        r=requests.post(url=url,params=params,json=json)
        print(r.json())
        assert r.json()["errcode"] == 0
    #@pytest.mark.skip
    def test_update_tagname(self):
        url="https://qyapi.weixin.qq.com/cgi-bin/tag/update"
        params = {
            "access_token": self.token
        }
        json={
                "tagid": 5,
                "tagname": "UI design"
        }
        r= requests.post(url=url,params=params,json=json)
        print(r.json())
        assert r.json()["errmsg"] == "updated"

    #@pytest.mark.skip
    def test_delete_tag(self):
        url="https://qyapi.weixin.qq.com/cgi-bin/tag/delete"
        params = {
            "access_token": self.token,
            "tagid": 6
        }
        r=requests.get(url=url,params=params)
        print(r.json())
        assert r.json()["errmsg"] == "deleted"

    #@pytest.mark.skip
    def test_get_tagmember(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get"
        params = {
            "access_token": self.token,
            "tagid": 3
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0

    #@pytest.mark.skip
    def test_add_tagmember(self):
        url="https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"
        params = {
            "access_token": self.token
        }
        # userlist、partylist不能同时为空,userlist单次请求个数不超过1000,partylist单次请求个数不超过100
        json={
                "tagid": 3,
                "userlist": ["员工新","员工老"],
                "partylist": [1,2]
        }
        r=requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

    #@pytest.mark.skip
    def test_delete_tagmember(self):
        url="https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers"
        params = {
            "access_token": self.token
        }
        json = {
            "tagid": 3,
            "userlist": ["员工新", "员工老"],
            "partylist": [1, 2]
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errcode"] == 0

    def test_get_taglist(self):
        url="https://qyapi.weixin.qq.com/cgi-bin/tag/list"
        params = {
            "access_token": self.token
        }
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0
执行结果:

作业:

代码如下:

import requests

class TestTags:
    def setup(self):
        corpid ="wwa00084331d6037a9"
        corpsecret ="Oo7HNzj6u60jPosjy7Lj-gVpetM4C9Pl-COpcaqnkzY"
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = {
            "corpid" : corpid,
            "corpsecret" : corpsecret
        }
        r = requests.get(url = url,params=params)
        self.token = r.json()['access_token']

    def test_create_tag(self): #创建标签
        url ='https://qyapi.weixin.qq.com/cgi-bin/tag/create'
        params ={"access_token": self.token}
        json = {
           "tagname": "标签2",
           "tagid": 22
        }
        r =requests.post(url =url,params=params,json =json)
        print(r.json())
        assert r.json()['errcode'] == 0

    def test_update_tag(self): #更新标签名字
        url ='https://qyapi.weixin.qq.com/cgi-bin/tag/update'
        params ={"access_token": self.token }
        json = {
            "tagid": 22,
            "tagname": "更新的标签22"
        }
        r =requests.post(url =url,params=params,json =json)
        print(r.json())
        assert r.json()['errcode'] == 0

    def test_delete_tag(self): #删除标签
        url ='https://qyapi.weixin.qq.com/cgi-bin/tag/delete'
        params = {"access_token": self.token,
                  "tagid":22}
        r = requests.get(url =url,params=params)
        print(r.json())
        assert r.json()['errcode'] == 0

    def test_getlist(self): #获取标签成员
        url ='https://qyapi.weixin.qq.com/cgi-bin/tag/get'
        params = {"access_token": self.token,
                  "tagid":13}
        r = requests.get(url =url,params=params)
        print(r.json())
        assert r.json()['errcode'] == 0

    def test_addmembers(self): #增肌标签成员
        url ='https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers'
        params = {"access_token": self.token}
        json ={
                "tagid": 13,
                "userlist": ["李四106", "李四107"],
                "partylist": [2]
            }
        r = requests.post(url =url,params=params,json =json)
        print(r.json())
        assert r.json()["errcode"] == 0

    def test_deletemembers(self): #删除标签成员
        url = 'https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers'
        params = {"access_token": self.token}
        json = {
            "tagid": 13,
            "userlist": ["POLO", "KELLY"],
            "partylist": [2]
        }
        r = requests.post(url=url, params=params, json=json)
        print(r.json())
        assert r.json()["errmsg"] == 'deleted'

    def test_get_taglist(self): #获取标签列表
        url = 'https://qyapi.weixin.qq.com/cgi-bin/tag/list'
        params = {"access_token": self.token}
        r = requests.get(url=url, params=params)
        print(r.json())
        assert r.json()["errcode"] == 0
        

运行截图:

关闭