This article mainly introduces you to the relevant information on how to use Python to implement data-driven interface automated testing. The article introduces it in great detail through sample code. It has certain reference learning value for everyone to learn or use python. Friends who need it can come here Let’s take a look together
Foreword
During the interface testing process, we often use CSV reading operations. This article mainly explains how to write and read CSV in Python3. Not much to say below, let’s take a look at the detailed introduction.
1. Demand
A certain API, GET method, three parameters: token, mobile, and email
- token is required
- mobile,email 1 item is required
- mobile is the mobile phone number, email is the email format
2. Plan
For the above API, when doing interface testing, the number of test cases required will often be as many as 10+. At this time, it may be more appropriate to use a data-driven approach to write common content into the configuration file.
Here we consider pre-saving the API, parameters, and expected results in a formatted CSV, using the csv component to read the URL, parameters, and expected results from the CSV. The Requests component initiates a request and compares the response results with the expected results. Finally, write the comparison results to the result CSV.
The process is as shown below?
3. Implementation
1. Before loading the code, install the following components:
- csv read and write CSV files
- json
- requests initiates a request and obtains the response result
- unittest test case scheduling
Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected. If you don’t want to experience the feeling of not being able to find resources when self-study, having no one to answer your questions, and persisting for a few days before giving up. You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc. Share their experience, and also share many live lectures and technical salons You can learn for free! Focus on it! Open source! ! ! QQ group number: 110685036
2. data.csv (this example selects some use cases)
3. reader_CSV function code example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 27 28 29 30 31 32 |
|
4. request_URL function example (including GET request and POST request methods)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two |
|
5. Example of assert_Result function
1 2 3 4 5 6 7 8 9 |
|
6. Write_CSV function example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
7. Test_interface1 function example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
8. result_1523956055.csv (please ignore the test results in this example)
Summary
Python encapsulates many methods, and the development speed is relatively fast for testing. If the interface automation test adopts the data-driven method of CSV management, using csv + requests is one of the tools that cannot be missed in test development.
Okay, that’s all the content of this article!!!
Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!
Software testing interview document
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeWeb crawlerSelenium388365 people are learning the system