测试案例实现代码库与测试用例V1.0
创建分部
#创建分部
def add(name):
path = '/api/v4/organizations'
payload={
"name": name,#分部名称
"parent": "iFme7FHg8wQgzTrCC",#上级部门ID,默认使用总公司id
"sort_no": 100,
"hidden": False,
"space": "NhnKCEchFReJbgqZM" #空间id,通过登录返回的cookies来获取
}
列出分部all
def list_all():
path='/api/v4/organizations'
resp =requests.get(f'{host}{path}')
return resp.json()
删除单个
def delete(organization_id):
path=f'/api/v4/organizations/{organization_id}'
resp = requests.delete(f'{host}{path}')
return resp.json()
删除所有分部
def delete_all():
#先列出,再循环依次删除
orgs =list_all()
for org in orgs:
delete(org['_id'])
用户鉴权---电影院买票看电影例子
common.py模块下
def login(email,pwd):
path='/accounts/password/authenticate'
payload={"user":
{"email":email},
"password":pwd,
"code":"",
"locale":"zh-cn"}
res =requests.post(f'{host}{path}',json=payload)
return res.cookies
测试下方法:
<RequestsCookieJar[<Cookie X-Access-Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InRva2VuIjoiMzlkZmM3YjFmZTcyNzc3NzQ0ZDVjZGNhYmY0ZjljNjVkZTg4ZjA4MjMzZDk2MmY0MzgxMDVjOGQ4OWFiOWUyNDM0MmFmNjVhOGMyOWVlOTVmYzQ2MWMiLCJpc0ltcGVyc29uYXRlZCI6ZmFsc2UsInVzZXJJZCI6IjVmZDg3NDM0YjQyODAzMDAxMmYyNjFiZiJ9LCJpYXQiOjE2Mjc2NDk3NDUsImV4cCI6MTYzNTQyNTc0NX0.rrCHEwDSOyPQftgluCMriNkcauOrPkzyBHbZ8HFNfB8 for 120.27.146.185/>, <Cookie X-Auth-Token=39dfc7b1fe72777744d5cdcabf4f9c65de88f08233d962f438105c8d89ab9e24342af65a8c29ee95fc461c for 120.27.146.185/>, <Cookie X-Space-Id=tY4wsv85gTFhk6B5N for 120.27.146.185/>, <Cookie X-Space-Token=tY4wsv85gTFhk6B5N,39dfc7b1fe72777744d5cdcabf4f9c65de88f08233d962f438105c8d89ab9e24342af65a8c29ee95fc461c for 120.27.146.185/>, <Cookie X-User-Id=5fd87434b428030012f261bf for 120.27.146.185/>]>
接着把返回的cookies全部加到上面的接口中
resp = requests.post(f'{host}{path}',json=payload,cookies=cookies)
测试库
from pylib.webapi.organizapi import *
import pytest
from conf.env import g_pwd,g_email
from pylib.webapi.common import login
@pytest.fixture()
def before_tc000001():
cookies=login(g_email,g_pwd)
delete_all(cookies)
return cookies
def test_tc000001(before_tc000001):
cookies=before_tc000001
#step1
org=add('研发部',cookies)
#step2
orgs=list_all(cookies)
assert org in orgs#检查列表中包含刚刚创建的部门信息
问题记录
1、TypeError: string indices must be integers
执行报错:
def delete_all(cookies):
#先列出,再循环依次删除
orgs =list_all(cookies)
for org in orgs:
delete(org['_id'],cookies)
E TypeError: string indices must be integers
解决:list_all,要返回列表,
return resp.json()['value']
2、assert org in orgs#检查列表中包含刚刚创建的部门信息
执行又报另外的错误
def test_tc000001(before_tc000001):
cookies=before_tc000001
#step1
org=add('研发部',cookies)
#step2
orgs=list_all(cookies)
assert org in orgs#检查列表中包含刚刚创建的部门信息
E assert [{'@odata.editLink': "http://127.0.0.1:5040/api/odata/v4/tY4wsv85gTFhk6B5N/organizations('KLQ4hH65L4kjmxdHF')", '@odat...://127.0.0.1:5040/api/odata/v4/tY4wsv85gTFhk6B5N/organizations('KLQ4hH65L4kjmxdHF')", '_id': 'KLQ4hH65L4kjmxdHF', ...}] in [{'@odata.editLink': "http://127.0.0.1:5040/api/odata/v4/tY4wsv85gTFhk6B5N/organizations('iFme7FHg8wQgzTrCC')", '@odat...://127.0.0.1:5040/api/odata/v4/tY4wsv85gTFhk6B5N/organizations('KLQ4hH65L4kjmxdHF')", '_id': 'KLQ4hH65L4kjmxdHF', ...}]
原因分析:
返回的是列表,列表不能遍历列表,需要是列表里面的值
解决;
add的方法修改返回值,返回列表中的元素
return resp.json()['value'][0]
3、抓包有红色报错
案例执行通过,但是抓包发现有红色的报错
原因:
删除全部,总公司无法删除
解决:
过滤总公司
先列出,再循环依次删除
orgs =list_all(cookies)[1:]