Python learning notes (interface automation framework) and python learning notes
Previously, we used the unittest test framework provided by python.
This time, I designed one by myself and then added a little more functionality to it.
(Ps: of course, this framework is really very simple .. Very simple... Very simple ...)
Excel File Format:
1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 4 import xlrd 5 import json 6 7 8 class CreateExcel: 9 def _ init _ (self): 10 pass11 12 @ classmethod13 def open_excel (cls): 14 path = "testcase.xls" 15 workbook = xlrd. open_workbook (path) 16 table = workbook. sheets () [0] 17 return table18 # Get sheet19 20 @ classmethod21 def get_nrows (cls, table): 22 nrows = table. nrows23 return nrows24 # Get row 25 26 @ classmethod27 def get_name (cls, table, nrows): 28 testname = [] 29 for I in range (1, nrows): 30 testname. append (table. cell (I, 0 ). value) 31 return testname32 # obtain the name33 34 @ classmethod35 def get_data (cls, table, nrows): 36 testdata = [] 37 for I in range (1, nrows ): 38 data = json. loads (table. cell (I, 1 ). value) 39 testdata. append (data) 40 return testdata41 # obtain the data interface parameter 42 43 @ classmethod44 def get_url (cls, table, nrows): 45 testurl = [] 46 for I in range (1, nrows): 47 testurl. append (table. cell (I, 2 ). value) 48 return testurl49 # obtain the Interface Test url50 51 @ classmethod52 def get_method (cls, table, nrows): 53 testmethod = [] 54 for I in range (1, nrows ): 55 testmethod. append (table. cell (I, 3 ). value) 56 return testmethod57 # obtain the Interface Test method58 59 @ classmethod60 def get_pattern (cls, table, nrows): 61 testpattern = [] 62 for I in range (1, nrows ): 63 testpattern. append (table. cell (I, 4 ). value) 64 return testpattern65 # obtain the interface's expected response result 66 67 @ classmethod68 def get_report (cls, table, nrows): 69 testreport = [] 70 for I in range (1, nrows): 71 testreport. append (table. cell (I, 5 ). value) 72 return testreport73 # Get the expected running result of the Use Case 74 75 76 if _ name _ = "_ main _": 77 CreateExcel ()
The above code is used to process the excel document.
The following code is a test platform.
1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 4 5 import requests 6 import re 7 from createexcel import CreateExcel 8 9 9 10 class CreateTest: 11 def _ init _ (self): 12 pass13 14 @ classmethod15 def test_api (cls, method, url, data): 16 global results17 if method = "post ": 18 results = requests. post (url, data) 19 if method = "get": 20 results = requests. get (url, data) 21 return results22 23 @ classmethod24 def test_on (cls): 25 print "Use Case execution start" 26 27 @ classmethod28 def test_close (cls ): 29 print "End of case execution" 30 31 @ classmethod32 def test_result (cls, ra, rb): 33 if ra = rb: 34 print "Test Result: test passed" 35 else: 36 print "Test Result: test failed" 37 38 @ classmethod39 def test_http (cls, code): 40 if code = print "test request: request via "42 43 @ classmethod44 def test_main (cls): 45 global report46 table = CreateExcel. open_excel () 47 nrows = CreateExcel. get_nrows (table) 48 for I in range (0, nrows-1): 49 testname = CreateExcel. get_name (table, nrows) [I] 50 testdata = CreateExcel. get_data (table, nrows) [I] 51 testurl = CreateExcel. get_url (table, nrows) [I] 52 testmethod = CreateExcel. get_method (table, nrows) [I] 53 testpattern = CreateExcel. get_pattern (table, nrows) [I] 54 testreport = CreateExcel. get_report (table, nrows) [I] 55 CreateTest. test_on () 56 print "Test Case:", testname57 try: 58 testresults = CreateTest. test_api (testmethod, testurl, testdata) 59 CreateTest. test_http (testresults. status_code) 60 pattern = re. compile (testpattern) 61 match = pattern. search (testresults. url) 62 if match. group () = testpattern: 63 report = "pass" 64 CreateTest. test_result (testreport, report) 65 TB t AttributeError: 66 report = "no" 67 CreateTest. test_result (testreport, report) 68 failed t Exception. _ base __: 69 print "test request: request failed" 70 report = None71 CreateTest. test_result (testreport, report) 72 CreateTest. test_close () 73 74 75 if _ name _ = '_ main _': 76 CreateTest ()