Python learning notes (API automation framework V2.0) and python learning notes

Source: Internet
Author: User

Python learning notes (API automation framework V2.0) and python learning notes

This is based on the previous framework version.

Use python to obtain test case data in an excel file

Use the requets test interface and the regular expression to verify the response content

Generate xml file test report

Version updates:

1. sorted out the CreateTest. test_main () process logic.

2. optimized the testcase.xls File Format

3. added the XML file generation Test report

The Code is as follows:

1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 # obtain the test case file excel 4 5 import xlrd 6 import json 7 8 9 class CreateExcel: 10 def _ init _ (self): 11 pass 12 13 @ classmethod 14 def open_excel (cls): 15 path = "testcase.xls" 16 workbook = xlrd. open_workbook (path) 17 table = workbook. sheets () [0] 18 return table 19 20 # Get sheet 21 22 @ classmethod 23 def get_nrows (cls, table): 24 nrows = table. nrows 25 return nrows 26 27 # Get row 28 29 @ classmethod 30 def get_id (cls, table, nrows): 31 testid = [] 32 for I in range (1, nrows ): 33 testid. append (table. cell (I, 0 ). value) 34 return testid 35 36 @ classmethod 37 def get_name (cls, table, nrows): 38 testname = [] 39 for I in range (1, nrows): 40 testname. append (table. cell (I, 1 ). value) 41 return testname 42 43 # obtain the use case name 44 45 @ classmethod 46 def get_data (cls, table, nrows): 47 testdata = [] 48 for I in range (1, nrows): 49 try: 50 data = json. loads (table. cell (I, 2 ). value) 51 testdata. append (data) 52 failed t ValueError: 53 testdata. append (None) 54 return testdata 55 56 # obtain the data interface parameter 57 58 @ classmethod 59 def get_url (cls, table, nrows ): 60 testurl = [] 61 for I in range (1, nrows): 62 testurl. append (table. cell (I, 3 ). value) 63 return testurl 64 65 # obtain the Interface Test url 66 67 @ classmethod 68 def get_method (cls, table, nrows): 69 testmethod = [] 70 for I in range (1, nrows): 71 testmethod. append (table. cell (I, 4 ). value) 72 return testmethod 73 74 # obtain the Interface Test method 75 76 @ classmethod 77 def get_pattern (cls, table, nrows): 78 testpattern = [] 79 for I in range (1, nrows): 80 testpattern. append (table. cell (I, 5 ). value) 81 return testpattern 82 83 # Get the interface's expected response Result 84
1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 # test the Core Component 4 5 import requests 6 import re 7 from datetime import datetime 8 from createexcel import CreateExcel 9 from xml. dom import minidom 10 import sys 11 12 13 class CreateTest: 14 reload (sys) 15 sys. setdefaultencoding ("UTF-8") 16 17 # avoid errors in writing strings to files 18 19 def _ init _ (self): 20 pass 21 22 @ classmethod 23 def test_api (cls, method, url, data): 24 global results 25 try: 26 if method = "post": 27 results = requests. post (url, data) 28 if method = "get": 29 results = requests. get (url, data) 30 return results 31 failed t Exception. _ bases __: 32 print "server access failed" 33 34 # interface function 35 36 @ classmethod 37 def test_on (cls ): 38 print "Start of Use Case execution" 39 40 @ classmethod 41 def test_close (cls): 42 print "End of Use Case execution" 43 44 @ classmethod 45 def test_result (cls, pa ): 46 global report 47 try: 48 pattern = re. compile (pa) 49 match = pattern. search (testresults. text) 50 if match. group () = pa: 51 report = "tested" 52 failed t AttributeError: 53 report = "test failed" 54 return report 55 56 # Regular Expression detection 57 58 @ classmethod 59 def test_http (cls, code): 60 print "request return Status code :", code 61 62 @ classmethod 63 def test_time (cls): 64 nowtime = datetime. today () 65 time = nowtime. strftime ("% Y-% m-% d % H: % M: % S ") 66 return time 67 68 # obtain the current time conversion string 69 70 @ classmethod 71 def test_report (cls): 72 nowtime = datetime. today () 73 reportime = nowtime. strftime ("% Y % m % d % H % M % S") 74 reportname = reportime + ". xml "75 return reportname 76 77 # obtain the name of the test report file 78 79 @ classmethod 80 def test_main (cls): 81 global testresults 82 table = CreateExcel. open_excel () 83 nrows = CreateExcel. get_nrows (table) 84 xml = minidom. document () 85 xml. appendChild (xml. createComment ("Test Report") 86 caselist = xml. createElement ("caselist") 87 xml. appendChild (caselist) 88 for I in range (0, nrows-1): 89 testid = CreateExcel. get_id (table, nrows) [I] 90 testname = CreateExcel. get_name (table, nrows) [I] 91 testdata = CreateExcel. get_data (table, nrows) [I] 92 testurl = CreateExcel. get_url (table, nrows) [I] 93 testmethod = CreateExcel. get_method (table, nrows) [I] 94 testpattern = CreateExcel. get_pattern (table, nrows) [I] 95 96 # run the test 97 CreateTest. test_on () 98 testresults = CreateTest. test_api (testmethod, testurl, testdata) 99 testcode = str (testresults. status_code) 100 try: 101 CreateTest. test_http (testresults. status_code) 102 failed t 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 case ID110 111 name = xml. createElement ("name") 112 name. appendChild (xml. createTextNode (testname) 113 # input case name 114 method = xml. createElement ("method") 115 method. appendChild (xml. createTextNode (testmethod) 116 # Input Interface Type 117 code = xml. createElement ("code") 118 code. appendChild (xml. createTextNode (testcode) 119 # enter the case to return the status code 120 result = xml. createElement ("result") 121 result. appendChild (xml. createTextNode (CreateTest. test_result (testpattern) 122 # input test case Test Result 123 time = xml. createElement ("time") 124 time. appendChild (xml. createTextNode (CreateTest. test_time () 125 # input case execution time 126 127 case. appendChild (name) 128 case. appendChild (method) 129 case. appendChild (code) 130 case. appendChild (result) 131 case. appendChild (time) 132 133 caselist. appendChild (case) 134 # End of xml file generation 135 filename = file (CreateTest. test_report (), "w +") 136 # generate a test report file named after the current time 137 xml. writexml (filename) 138 filename. close () 139 # close file 140 141 142 if _ name _ = '_ main _': 143 CreateTest. test_main ()

 

Below is the test entry:

1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 #********************************** * ****************************** 4 # interface. py 5 # Author: ChenLei 6 # Version: 2.0 7 # Date: 8 #************************************* * ************************** 9 10 import time11 from createtest import CreateTest12 13 start = time. clock () 14 CreateTest. test_main () 15 end = time. clock () 16 17 print "interface automation script Run Time: %. 03f seconds "% (end-start)

The xml file generated automatically at the current time after running is as follows:

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.