zoukankan      html  css  js  c++  java
  • python学习笔记(excel+unittest)

    准备先利用之前整理的python自带的unittest框架

    整合excel 实现接口自动化测试功能

    先看看excel表格设置:

    下来是对excel获取的代码:

     1 #!/usr/bin/env python
     2 # -*- coding: utf_8 -*-
     3 
     4 import xlrd
     5 import json
     6 
     7 class Create_excel:
     8     def open_excel(self,path):
     9         workbook = xlrd.open_workbook(path)
    10         table = workbook.sheets()[0]
    11         return table
    12     #获取sheet
    13 
    14     def get_nrows(self,table):
    15         nrows = table.nrows
    16         return nrows
    17     #获取行号
    18 
    19     def testname(self,table,nrows):
    20         TestName = []
    21         for i in range(1,nrows):
    22             TestName.append(table.cell(i,0).value)
    23         return TestName
    24     #获取用例name
    25 
    26     def testdata(self,table,nrows):
    27         TestData = []
    28         for i in range(1,nrows):
    29             data = json.loads(table.cell(i,1).value)
    30             TestData.append(data)
    31         return TestData
    32     #获取data接口参数
    33 
    34     def testurl(self,table,nrows):
    35         TestUrl = []
    36         for i in range(1,nrows):
    37             TestUrl.append(table.cell(i,2).value)
    38         return TestUrl
    39     #获取接口测试url
    40 
    41     def testpattern(self,table,nrows):
    42         TestPattern = []
    43         for i in range(1,nrows):
    44             TestPattern.append(table.cell(i,3).value)
    45         return TestPattern
    46     #获取接口期望响应结果
    47 
    48     def testreport(self,table,nrows):
    49         TestReport = []
    50         for i in range(1,nrows):
    51             TestReport.append(table.cell(i,4).value)
    52         return TestReport
    53     #获取用例期望的运行结果
    54 
    55 if __name__ == "__main__":
    56     Create_excel()

    之后是unittest框架

     1 #!/usr/bin/env python
     2 # -*- coding: utf_8 -*-
     3 
     4 import requests
     5 import re
     6 import unittest
     7 from myexcel import Create_excel
     8 
     9 class Testapi():
    10     def testapi(self,url,data):
    11         results = requests.post(url,data)
    12         return results
    13 
    14 class Testcase(unittest.TestCase):
    15     def setUp(self):
    16         print "接口测试开始"
    17 
    18     def tearDown(self):
    19         print "接口测试结束"
    20 
    21     def test_post(self):
    22         global report
    23         api = Testapi()
    24         excel = Create_excel()
    25         testpath = "testcase.xls"
    26         testtable = excel.open_excel(testpath)
    27         testnrows = excel.get_nrows(testtable)
    28         for i in range(0,testnrows-1):
    29             testname = excel.testname(testtable,testnrows)[i]
    30             testdata = excel.testdata(testtable,testnrows)[i]
    31             testurl = excel.testurl(testtable,testnrows)[i]
    32             testpattern = excel.testpattern(testtable,testnrows)[i]
    33             testreport = excel.testreport(testtable,testnrows)[i]
    34             testresults = api.testapi(testurl,testdata)
    35             pattern = re.compile(testpattern)
    36             match = pattern.search(testresults.url)
    37             try:
    38                 if testresults.status_code == 200:
    39                     if match.group() == testpattern:
    40                         report = "pass"
    41                 else:
    42                     print "测试请求失败"
    43             except AttributeError:
    44                 report = "no"
    45             if report == testreport:
    46                 print "用例名称:",testname,"测试结果:测试通过"
    47             else:
    48                 print "用例名称:",testname,"测试结果:测试失败"
    49 
    50 if __name__ == "__main__":
    51     unittest.main()

    利用循环执行所有用例

    现在只要在excel里添加接口测试用例

    运行脚本 即可

  • 相关阅读:
    TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系
    人工智能、机器学习、深度学习、神经网络概念说明
    神经网络
    人工智能学习资料汇总
    tf.nn.conv2d。卷积函数
    卷积神经网络(CNN)
    Softmax函数模型介绍
    使用virtualenv进行python环境隔离
    升级mac自带的python
    MAC资料汇总
  • 原文地址:https://www.cnblogs.com/cllovewxq/p/5377223.html
Copyright © 2011-2022 走看看