Python learning notes (excel + requests), pythonrequests
After a simple excel operation
You can start writing test cases in excel.
Read use case execution case
Write execl in advance
The following code is used:
1 #! /Usr/bin/env python 2 #-*-coding: utf_8-*-3 4 import xlrd 5 import requests 6 import re 7 import json 8 9 workbook = xlrd. open_workbook (r'e: \ work \ python case \ learn \ testcase.xls ') 10 # Open the excel file 11 table = workbook according to the path. sheets () [0] 12 # obtain the first sheet13 nrows = table. nrows14 # obtain the number of rows 15 TestData = [] 16 # array 17 for I in range (1, nrows): 18 TestData. append (table. cell (I, 1 ). value) 19 # Add cell data 20 print TestData21 22 for j in range (0, nrows-1) at the end of the loop array: 23 data = json. loads (TestData [j]) 24 # string escape json25 TestCase = data26 # The cyclic array value is assigned to the test case parameter 27 print TestCase28 # The test case parameter 29 results = requests. post ('HTTP: // localhost: 8081/swcw/back/sysLogin. action ', data = TestCase) 30 # post request 31 pattern = re. compile (r 'tomain') 32 # regular expression ps: I am too simple to use o (Regular Expression) o alas 33 match = pattern. search (results. url) 34 try: 35 if results. status_code = 200:36 if match. group () = 'tomain': 37 print 'test result: test passed '38 else: 39 print 'Test Result: request failed '40 TB AttributeError: 41 print 'test result: Test failed'
Simple code is written.
View the running result:
You can see the direction of this API automated testing framework.