测试人社区

接口测试训练营活动

接口测试框架

requests接口测试工具
https://requests.readthedocs.io/en/master/

pytest测试框架,目前python界里最好用的测试框架
https://docs.pytest.org/en/stable/

pycharm python编程最佳IDE

import json

import pytest
import requests


@pytest.mark.parametrize('keyword', [
    'appium',
    'selenium',
    'hogwarts'
])
def test_search(keyword):
    r = requests.get(
        'https://ceshiren.com/search',
        params={'q': keyword},
        headers={'accept': 'application/json'}
    )
    print(json.dumps(r.json(), indent=2, ensure_ascii=False))

    assert r.status_code == 200
    assert keyword in str(r.json()['posts'][0]['blurb']).lower()

企业微信接口测试实战

企业微信接口文档
https://work.weixin.qq.com/api/doc/90000/90135/90664
客户便签管理
https://work.weixin.qq.com/api/doc/90000/90135/92117

封装

测试用例

import json
from time import sleep

import pytest
import requests

from wework_tag import WeWorkTag

class TestWework:
    # token=None
    # r=None
    def setup_class(self):
        self.tag = WeWorkTag()
        self.tag.get_token()

    def test_tags_list(self):
        r = self.tag.list()
        assert r.status_code == 200

    @pytest.mark.parametrize('name', [
        'ceshiren.com',
        'seveniruby',
        'hogwarts'
    ])
    def test_tags_list_1(self, name):
        # todo: case反模式示例问题
        # 如何解决数据依赖问题
        r = self.tag.list()
        tag = r.json()['tag_group'][0]['tag'][0]
        tag_id = tag['id']
        r = self.tag.update_tag(tag_id, name)
        assert r.status_code == 200

po模式的封装api object

import json

import requests


class WeWorkTag:
    def get_token(self):
        r = requests.get(
            'https://qyapi.weixin.qq.com/cgi-bin/gettoken',
            params={
                'corpid': 'wwd6da61649bd66fea',
                'corpsecret': 'heLiPlmyblHRiKAgGWZky7MMvyld3d3QMUl5ra7NBZU'
            }
        )
        self.token = r.json()['access_token']
        self.pretty(r)

    def list(self):
        r = requests.post(
            'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list',
            params={'access_token': self.token}
        )
        self.pretty(r)
        # 加入加解密逻辑
        return r

    def update_tag(self, tag_id, name):
        r = requests.post(
            'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag',
            params={
                'access_token': self.token,
            },
            json={

                'id': tag_id,
                'name': name
            }

        )
        self.pretty(r)
        return r

    def pretty(self, r):
        print(json.dumps(r.json(), indent=2, ensure_ascii=False))

扫码进群

接口测试训练营

1 Like