Python3 how to write and read CSV

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.

Demand

An 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. Plan

For 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

Python3 how to write and read CSV

3. Realize

(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)

Python3 how to write and read CSV

(3) Reader_CSV function code example

Python3 how to write and read CSV

(4) example of request_URL function (including 2 methods for GET request and POST request)

Python3 how to write and read CSV

(5) Example of assert_Result function

Python3 how to write and read CSV

(6) write_CSV function example

Python3 how to write and read CSV

(7) test_interface1 function example

Python3 how to write and read CSV

Python3 how to write and read CSV

8result_1523956055.csv (The test results in this example please ignore)

Python3 how to write and read CSV

4. Summary

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.

PC Motherboards

Shenzhen Innovative Cloud Computer Co., Ltd. , https://www.xcypc.com