Python Writing interface Automation framework

Source: Internet
Author: User

The code is as follows:

  1 #!/usr/bin/env Python 2 #-*-coding:utf_8-*-3 # Get test Case file Excel 4 5 import xlrd 6 import JSON 7 8 9 class Createexcel:10 def __init__ (self): one pass @classmethod def open_excel (CLS): PA th = "Testcase.xls", workbook = Xlrd.open_workbook (path), table = Workbook.sheets () [0] Retu         RN Table 19 20 # Get Sheet @classmethod def get_nrows (CLS, table): nrows = table.nrows 25 Return nrows 26 27 # Get line number @classmethod def get_id (CLS, table, nrows): TestID = [ ] + for I in range (1, nrows): Testid.append (Table.cell (i, 0). Value) return TestID 35 3 6 @classmethod get_name def (CLS, table, nrows): TestName = [] 1 for I in range (, nrows): 4 0 testname.append (Table.cell (i, 1). Value)-Return testname 42 43 # Get the use case name @classme Thod-def Get_data(CLS, table, nrows): TestData = [] for I in range (1, nrows): try:50                 data = Json.loads (Table.cell (i, 2). Value) Wuyi testdata.append (data) except valueerror:53 Testdata.append (None) testdata return 55 56 # Get Data Interface Parameters @classmethod Get_url (CLS, table, nrows): Testurl = [] testurl.append for I in range (1, nrows): + + (tab Le.cell (i, 3). Value) Testurl return 64 65 # Get interface Test URL @classmethod (CLS, t Able, nrows): TestMethod = [] for I in range (1, nrows): Testmethod.append (Table.cell ( I, 4). Value) Get_pattern return TestMethod 73 74 # Get interface test Method-@classmethod-def (CLS, TA BLE, nrows): Testpattern = [] for I in range (1, nrows): Testpattern.append (Table.cell        (I, 5). Value) 81 Return Testpattern 82 83 # Get interface Expected response result 84  
  1 #!/usr/bin/env Python 2 #-*-coding:utf_8-*-3 # Test Core Components 4 5 import requests 6 import re 7 from DateTime impo RT datetime 8 from Createexcel import createexcel 9 from xml.dom import minidom import sys one class createtes         T:14 Reload (SYS) sys.setdefaultencoding ("Utf-8") 16 17 # Error preventing string writing to file. Def __init__ (self): 20 Pass @classmethod def test_api (cls, method, URL, data): Global results try: If method = = "POST": results = requests.post (URL, data) if method = = "Get ": results = requests.get (URL, data) return results except Exception.__bases_ _: Print "Server access failed" 33 34 # Interface Function @classmethod PNS test_on (CLS): print "use case Line Start "Max @classmethod Test_close (CLS): print" Use case execution End "@classmethod def test    _result (CLS, PA): 46     Global Report try:48 pattern = Re.compile (PA)-match = Pattern.search (testresul             Ts.text) if match.group () = = pa:51 report = "Test through" except attributeerror:53 Report = "Test Failed"-Report 55 56 # Regular Expression detect test_http (CLS, Code): print "Request return status code:", code @classmethod + def test_time (CLS): Nowtime = DateTime     . Today () time = Nowtime.strftime ("%y-%m-%d%h:%m:%s") 67 68 # Get current time conversion string 69 70 @classmethod def test_report (CLS): Nowtime = Datetime.today () Reportime = Nowtime.strftime (     "%y%m%d%h%m%s") ReportName = Reportime + ". Xml", return ReportName 76 77 # Get test Report file name 78 79         @classmethod def test_main (CLS): Bayi Global TestResults-createexcel.open_excel () 83 nrows = Createexcel.Get_nrows (table), XML = Minidom.         Document () Xml.appendchild (xml.createcomment ("Test Report")) Caselist = Xml.createelement ("caselist") 87  Xml.appendchild (Caselist), 0, nrows-1): TestID = createexcel.get_id (table, nrows) [i] testname = createexcel.get_name (table, nrows) [i] testdata = Createexcel.get_data ( Table, nrows) [i] Testurl = createexcel.get_url (table, nrows) [i], TestMethod = Createexcel.get _method (table, nrows) [i] 94 Testpattern = Createexcel.get_pattern (table, nrows) [i] 95 96 # Perform Test 9             7 createtest.test_on () 98 testresults = Createtest.test_api (TestMethod, Testurl, testdata) 99 Testcode = str (testresults.status_code) try:101 createtest.test_http (testresults.st Atus_code) 102 except attributeerror:103 pass104 createtest.test_Close () 105 # execution End 106 # Generate XML file 107 case = Xml.createelement ("case") 108 case . SetAttribute ("id", TestID) 109 # Input use case ID110 111 name = xml.createelement ("name")              E.appendchild (Xml.createtextnode (testname)) 113 # Input Use case name method = Xml.createelement ("method") 115 Method.appendchild (Xml.createtextnode (TestMethod)) 116 # Input Interface type 117 code = Xml.createel Ement ("code") 118 Code.appendchild ((Xml.createtextnode (Testcode))) 119 # Input use case return status code + RE Sult = xml.createelement ("result") 121 Result.appendchild (Xml.createtextnode (Createtest.test_result (Testpattern )) 122 # Input use case test result 123 time = Xml.createelement ("Time") 124 Time.appendchild (Xml.createte Xtnode (Createtest.test_time ())) 125 # Input use case Execution Time 126 127 case.appendchild (name) Case.ap    Pendchild (method) 129         Case.appendchild (code) case.appendchild (Result) 131 Case.appendchild (time) 132 133          Caselist.appendchild (case) 134 # XML file generation end 135 filename = file (Createtest.test_report (), "w+") 136 # Generate a test report file named at the current time 137 xml.writexml (filename) 138 filename.close () 139 # Close File 141 142 if __name__ = = ' __main__ ': 143 Createtest.test_main ()

Here is the test Portal:

1 #!/usr/bin/env Python 2 #-*-coding:utf_8-*-3 # **************************************************************** 4 # interface.py 5 # Author     : Chenlei 6 # Version    : 2.0 7 # Date       : 2016-4-15 8 # ******************************** 9 Import time11 from createtest import CreateTest12 start = Time.clock () createte St.test_main () end = Time.clock () print "Interface Automation script run time:%.03f seconds"% (End-start)

The XML file that automatically generates the current time after running is as follows:

Category: Python

Python Writing interface Automation framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.