实现合同测试用例-合同分类-签约对象
用例数据依赖分析
- 注意:
- 虽然签约对象和合同类型在同一级目录,但是如果我们用例组织和放在同一级目录下,那么用例就会出现分叉,用例就不在一条线上,合同分类就复用不了签约对象的conftest,为了复用conftest,建议把合同分类放到签约对象下面
在原有的目录结构的基础上,新增目录签约对象,新建conftest文件
import pytest
@pytest.fixture(scope='session')
def init_accounts():
pass
接着实现测试库
business下面实现
#合同API
class ContractsAPI(BaseAPI):
pass
#签约对象API
class AccountsAPI(BaseAPI):
pass
#合同分类API
class ContractTypesAPI(BaseAPI):
pass
接着要实现配置文件,从接口文档拷贝
ContractsAPI:
path: /api/v4/contracts
add:
{
"no": "009",
"create_date": "2020-07-07T07:31:06.754Z",
"company_id": "QmBLZC5zKaEMu96QN",
"othercompany": "38n6uHtDsdfmQ72jT",
"project": "jgrJCeMttzD8pHZhy",
"contract_type": "7Gy5aL2sM3gqnsg77",
"name": "test001",
"bop": "付款合同",
"applicant": "5efae24570ff1b0012038be1",
"owner": "5efae24570ff1b0012038be1",
"virtual_contract": "合同",
"amount": 0,
"amount_type": "固定",
"receive_payment_type": "一次性",
"paid_amount": 0,
"unpaid_amount": 0,
"receipted_amount": 0,
"unclaimed_votes_amount": 0,
"received_amount": 0,
"unreceived_amount": 0,
"for_invoicing_amount": 0,
"unfor_invoicing_amount": 0,
"space": "jTaQzFyri4ojvYSQm"
}
edit:
{
"no": "009",
"create_date": "2020-07-07T07:31:06.754Z",
"company_id": "QmBLZC5zKaEMu96QN",
"othercompany": "38n6uHtDsdfmQ72jT",
"project": "jgrJCeMttzD8pHZhy",
"contract_type": "7Gy5aL2sM3gqnsg77",
"name": "test001",
"bop": "付款合同",
"applicant": "5efae24570ff1b0012038be1",
"owner": "5efae24570ff1b0012038be1",
"virtual_contract": "合同",
"amount": 0,
"amount_type": "固定",
"receive_payment_type": "一次性",
"paid_amount": 0,
"unpaid_amount": 0,
"receipted_amount": 0,
"unclaimed_votes_amount": 0,
"received_amount": 0,
"unreceived_amount": 0,
"for_invoicing_amount": 0,
"unfor_invoicing_amount": 0,
"space": "jTaQzFyri4ojvYSQm"
}
AccountsAPI:
path: /api/v4/accounts
add:
{
"name": name,
"category": 1,
"company_ids": ["QmBLZC5zKaEMu96QN"],
"status": 1,
"space": self.api.space
}
edit:
{
"name": "testobj001",
"phone": "400-888888",
"address": "南京市软件大道",
"registered_capital": 1000000,
"category": "1",
"credit_code": "370202199507078631",
"company_ids": [
"QmBLZC5zKaEMu96QN"
],
"status": "2",
"space": "jTaQzFyri4ojvYSQm"
}
ContractTypesAPI:
path: /api/v4/contract_types
add:
{
name: "房租合同",
code: "2020-001",
space: "jTaQzFyri4ojvYSQm"
}
edit:
{
'name': "房租合同1",
'code': "2020-001",
'space': "jTaQzFyri4ojvYSQm"
}
优化测试库
import pytest
from pylib.webapi.bussiness import AccountsAPI
#签约对象--vip
@pytest.fixture(scope='session')
def init_accounts(init_admin,empty_organization):
account_api=AccountsAPI(init_admin)
org =empty_organization #待关联的部门
account=account_api.add(name='VIP客户',company_ids=[org['_id']])
yield account
account_api.delete(account['_id'])
实现合同类型测试库
在签约对象下面创建合同类型目录,在目录下面创建conftest实现测试库
import pytest
from pylib.webapi.bussiness import ContractTypesAPI
@pytest.fixture(scope='session')
def init_contracttypes(init_admin):
ctapi=ContractTypesAPI(init_admin)
account_type=ctapi.add(name='收购合同',code='888888')
yield account_type
ctapi.delete(account_type['_id'])
实现合同用例
import pytest
from pylib.webapi.bussiness import ContractsAPI
def test_tc003001(init_admin,init_accounts,init_contracttypes,init_organiz):
contract_api=ContractsAPI(init_admin)
#step1
contract=contract_api.add(name='001号合同',othercompany=init_accounts['_id'],contract_type=init_contracttypes['_id'],company_id=init_organiz['_id'])
#step2
contracts=contract_api.list_all()
#step3
assert contract in contracts
发现,新增接口的入参都是关键字参数形式,可以定义一个字典,然后拆包成关键字参数
contract_api=ContractsAPI(init_admin)
kws={
'name' : '001号合同',
'othercompany' : init_accounts['_id'],
'contract_type' : init_contracttypes['_id'],
"company_id" :init_organiz['_id']
}
#step1
contract=contract_api.add(**kws)
接着做数据清除操作:
import pytest
from pylib.webapi.bussiness import ContractsAPI
def test_tc003001(after_tc003001,init_accounts,init_contracttypes,init_organiz):
global contract
contract_api=after_tc003001
kws={
'name' : '001号合同',
'othercompany' : init_accounts['_id'],
'contract_type' : init_contracttypes['_id'],
"company_id" :init_organiz['_id']
}
#step1
contract=contract_api.add(**kws)
#step2
contracts=contract_api.list_all()
#step3
assert contract in contracts
@pytest.fixture()
def after_tc003001(init_admin):
contract_api = ContractsAPI(init_admin)
yield contract_api
contract_api.delete(contract['_id'])
执行通过,但是通过抓包发现,创建日期使用模板时间,跟实际时间不一致
生成一个时间格式跟入参一致
控制台调试
import time
time.localtime()
time.struct_time(tm_year=2021, tm_mon=8, tm_mday=15, tm_hour=15, tm_min=39, tm_sec=32, tm_wday=6, tm_yday=227, tm_isdst=0)
from datetime import datetime
datetime.now()
datetime.datetime(2021, 8, 15, 15, 39, 52, 554317)
localtime时间精确度没有now高
dt=datetime.now()
dt.strftime('%Y-%m-%dT%H:%M:%S.%f')
'2021-08-15T15:43:07.888396'
dt.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3]+'Z'
'2021-08-15T15:43:07.888Z'
在utils增加一个pulgings.py文件,定义一个类处理数据转换
from datetime import datetime
class Conver_data:
@staticmethod
def current_time(time_formate='%Y-%m-%dT%H:%M:%S.%f'):
dt=datetime.now()
return dt.strftime(time_formate)[:-3]+'Z'
优化测试用例,增加入参
kws={
'name' : '001号合同',
'othercompany' : init_accounts['_id'],
'contract_type' : init_contracttypes['_id'],
"company_id" :init_organiz['_id'],
"create_date":Conver_data.current_time()
}
问题记录
- 1、从PDF接口文档拷贝配置文件到pycharm,出现NBSP
- 解决:复制[NBSP]这个奇怪的东西,(剪贴板显示的其实还是空格),按住CTRL+R,上面的是一个空格(事先复制的NBSP),下面不要填,替换所有的。相当于把NBSP删除。over。
https://blog.csdn.net/weixin_40912987/article/details/117661447
-
2、init_admin = <RequestsCookieJar[Cookie(version=0, name='X-Access-Token', value='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7In...cified=True, secure=False, expires=1636787352, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>
empty_organization = <pylib.webapi.bussiness.OrganizAPI object at 0x000001F406EA45B0>@pytest.fixture(scope='session')
def init_accounts(init_admin,empty_organization):
account_api=AccountsAPI(init_admin)
org =empty_organization #待关联的部门
account=account_api.add(name='VIP客户',company_ids=[org['_id']])
E TypeError: 'OrganizAPI' object is not subscriptable
tcD-管理员登录D-需求部D-签约对象conftest.py:14: TypeError
解决:
empty_organization返回的是api
def init_accounts(init_admin,init_organiz):
account_api=AccountsAPI(init_admin)