自动生成基于pyunit的接口测试用例示例

生成用例脚本如下:

#coding=utf-8
'''
pyunit
WingIDE5.1.10-1   python3.5.1
,
    parameters=["Testcase_cartInformation",
                   "/cart/inforinformation",
                   [
                        {"TestcaseName":"cart_information1","BusinessParas":{},"judgePara":"200",
                             "testcase":"CartInformation200"},
                        {"TestcaseName":"cart_information2","BusinessParas":{"aaa":"bbb"},"judgePara":"400",
                             "testcase":"CartInformation400"}
                   ]
              ]
    modelClassCreate(parameters)
'''
import   os , sys
from   string   import   Template
#
def   singleMethodCreate ( MethodList , interfaceNamePara ) :
     code = Template ( '''\n    def test${testcase}(self):
         u"""${testcaseName}"""
         start=datetime.datetime.now()
         results = requests.post(self.url('${interfaceName}'),${businessParas})
         result=Frame_Result.ResultProcess(results)
         print (result)
         end=datetime.datetime.now()
         print ("case time:"+str((end-start).microseconds/1000000.0))
    ''' )
     string   =   code . substitute ( testcase = MethodList [ "testcase" ] , testcaseName = MethodList [ "TestcaseName" ] ,
                                     interfaceName = interfaceNamePara , businessParas = MethodList [ "BusinessParas" ])
     return   string
#
def   methodCreate ( MethodParaList , interfaceNamePara ) :
     string = ""
     for   MethodPara   in   MethodParaList :
          string2 = singleMethodCreate ( MethodPara , interfaceNamePara )
          string = string + string2
     return   string
#
def   modelClassCreate ( parameters ) :   
     modelCode = methodCreate ( parameters [ 2 ] , parameters [ 1 ])
     code   =   Template ( '''#coding: utf-8
    
"""
WingIDE5.1.10-1   python3.5.1
"""
import unittest,requests,datetime,sys
from RunCommon import *
from RunConfig import *
class ${className}(unittest.TestCase):
    u"""${interfaceName}"""
    def setUp(self):
         self.domain = TestcaseConfig.BaseURL
         
    def tearDown(self):
         pass
         
    ${model}
    
    def url(self, path):
         return self.domain + path
''' )
     fileStr   =   code . substitute ( className = parameters [ 0 ] , interfaceName = parameters [ 1 ] , model = modelCode )
     f = open ( parameters [ 0 ] + ".py" , 'w' , encoding = 'utf-8' )
     f . write ( fileStr )

    f.close()





生成的用例文件内容如下:

#coding: utf-8
    
"""
WingIDE5.1.10-1   python3.5.1
"""
import   unittest , requests , datetime , sys
from   RunCommon   import   *
from   RunConfig   import   *
class   Testcase_cartInformation ( unittest . TestCase ) :
     u"""/cart/inforinformation"""
     def   setUp ( self ) :
          self . domain   =   TestcaseConfig . BaseURL
         
     def   tearDown ( self ) :
          pass
         
    
     def   testCartInformation200 ( self ) :
          u"""cart_information1"""
          start = datetime . datetime . now ( )
          results   =   requests . post ( self . url ( '/cart/inforinformation' ) , { })
          result = Frame_Result . ResultProcess ( results )
          print   ( result )
          end = datetime . datetime . now ( )
          print   ( "case time:" + str (( end - start ) . microseconds / 1000000.0 ))
    
     def   testCartInformation400 ( self ) :
          u"""cart_information2"""
          start = datetime . datetime . now ( )
          results   =   requests . post ( self . url ( '/cart/inforinformation' ) , { 'aaa' 'bbb' })
          result = Frame_Result . ResultProcess ( results )
          print   ( result )
          end = datetime . datetime . now ( )
          print   ( "case time:" + str (( end - start ) . microseconds / 1000000.0 ))
    
    
     def   url ( self path ) :
          return   self . domain   +   path

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值