In the process of interface testing, many times the reading operation of CSV is used. This article mainly explains how Python3 writes and reads CSV.
DemandAn API, GET method, token, mobile, email three parameters
Token is required
Mobile,email Required 1 item
Mobile is the phone number and email is the email format
2. PlanFor the API above, the number of test cases required when doing interface testing will be as high as 10+. This may be more appropriate to use a data-driven approach to write common content to the configuration file.
Considering that the API, parameters, and expected results are pre-stored in a formatted CSV, the csv component reads the URL, parameters, and expected results from the CSV. The Requests component initiates the request and compares the response result with the expected result. Finally write the result of the comparison to the result CSV.
The process is as shown below
(1) Before installing the code, first install the following components:
Csv read and write CSV file
Json
Requests initiates a request for a response
Unittest test case scheduling
(2) data.csv (select some use cases in this example)
(3) Reader_CSV function code example
(4) example of request_URL function (including 2 methods for GET request and POST request)
(5) Example of assert_Result function
(6) write_CSV function example
(7) test_interface1 function example
8result_1523956055.csv (The test results in this example please ignore)
Python encapsulates a lot of methods. For testing, the development speed is relatively fast. If the interface automation test adopts the data driven method of CSV management, using csv+requests is one of the weapon that test development should not miss.
Shenzhen Innovative Cloud Computer Co., Ltd. , https://www.xcypc.com