Python interface test automation instructions and code examples: including GET, post, put, delete and other methods

Source: Internet
Author: User
Tags string format

First, the interface description document

Environment Preparation:

Install Firefox

Install plugin: httprequester

https://addons.mozilla.org/en-US/firefox/addon/httprequester/

Interface Return code:

The interface returns the code description:

' 00 ': Success

' 01 ': User already exists

' 02 ': argument is not valid

' 03 ': Parameter error (1, user information error 2, parameter error, data not present in database)

' 999 ': unknown error, see background log

Request Interface Example:

MD5 Computing Website:

http://md5jiami.51240.com/

1. Registration: Post method

Request Address:/HTTP 127.0.0.1:8080/register/

Request content: {"username": "Wulaoshi", "Password": "a12345678", "email": "[email protected]"}

Return content:

{"Code": "XX", "userid": 5}

2. Login: Post method

Request Address: http://127.0.0.1:8080/login/

Request content: {"username": "Wulaoshi", "Password": "e9bc0e13a8a16cbb07b175d92a113126"}

Return content: {"token": "94beb86afbf126a345b0cdf30e5e5202", "Code": "XX", "userid": 5, "login_time": "2016-10-13 22:50:24"}

3. Added Post: Post method

Request Address: http://127.0.0.1:8080/create/

Request content: {"userid": 5, "token": "94beb86afbf126a345b0cdf30e5e5202", "title": "Python", "content": "Python port Test"}

Return content: {"Data": [{"Content": "Python port Test", "title": "Python"}], "code": "XX", "userid": 5}

4. Modify the blog post: Using the Put method

Request Address: http://127.0.0.1:8080/update/

Request content: {"userid": 5, "token": "94beb86afbf126a345b0cdf30e5e5202", "ArticleID": One, "title": "Gloryroad", "Content": "Test Test!!!!!!!!!!!!! "}

Return content:

{"ArticleID": One, "Update_time": "2016-10-13 23:11:36", "Code": "XX", "userid": 5}

5, Query User blog: Post method

Request Address: http://127.0.0.1:8080/getBlogsOfUser/

Request content: {"userid": 5, "token": "94beb86afbf126a345b0cdf30e5e5202"}

Return content:

{"Data": [{"update_time": null, "title": "Python", "content": "Python port Test", "ArticleID": One, "owner": 5, "posted_on" : "2016-10-13 22:56:49"}], "code": "XX", "userid": 5}/

6. Check the contents of the blog: Get method

Request Address: HTTP://127.0.0.1:8080/GETBLOGCONTENT/11

Request content: Empty

Return content: {"code": "XX", "Data": [{"Update_time": "2016-10-13 23:11:36", "title": "Gloryroad", "Content": "Test test!!!!!!!!!! !!! "," ArticleID ": One," owner ": 5," posted_on ":" 2016-10-13 22:56:49 "}]}/

7, Bulk query Blog content:

Request Address: http://127.0.0.1:8080/getBlogsContent/articleIds=11,12

Request content: Empty

Return content: {"code": "XX", "Data": [{"Update_time": "2016-10-13 23:11:36", "title": "Gloryroad", "Content": "Test test!!!!!!!!!! !!! "," ArticleID ": One," owner ": 5," posted_on ":" 2016-10-13 22:56:49 "}, {" Update_time ": null," title ":" Mysql0 "," content " : "MySQL learn 0", "ArticleID": +, "owner": 1, "posted_on": "2016-10-13 23:04:35"}]}/

8. Delete Blog Posts:

Request Address: http://127.0.0.1:8080/delete/

Request content: {"userid": 5, "token": "94beb86afbf126a345b0cdf30e5e5202", "ArticleID": [11]}

Return content {"ArticleID": [One], "code": "XX", "userid": 5}

Python2.7 code instance one, login module

1. User Registration

Register

Post/register

Parameters:

Parameter Rule description:

Username

1. Composition of letters and numbers

2, length 2~20 bit

3. Letters are not case sensitive

Password

1, length 8~20 bit

2. Must contain letters and numbers

Email: Standard Email rules

JSON string format parameters, example: {"username": "Lily", "Password": "Lily", "email": "[email protected]"}

Response:

{"Code": "XX", "userid": 2}

Example

Import requests

Import JSON

print "Register------"

data = Json.dumps ({' username ': ' Lily ', ' Password ': ' WCX123WAC ', ' email ': ' [email protected] ') #

r = Requests.post (' http://localhost:8080/register/', data= data)

Print R.status_code

Print R.text

Print type (R.json ())

Print str (R.json ())

2. User Login

Login

Post/login

Parameters:

JSON string format parameters, example:

{"username": "Lily", "Password": "0550876efca0820e30e7398c177fd30b"}

Response:

{"token": "A1C0738A6CF054606B055A419C3390F3", "Code": "XX", "userid": 3, "Login_time": "2016-09-06 12:02:14"}

Example

Import requests

Import JSON

Import Hashlib

M5 = Hashlib.md5 ()

M5.update (' Wcx123wacs ')

PWD = M5.hexdigest ()

Print pwd

Print "Login------"

data = Json.dumps ({' username ': ' Lily ', ' Password ': pwd}) #

r = Requests.post (' http://localhost:8080/login/', data = data)

Print R.status_code

Print R.text

Print type (R.json ())

Print str (R.json ())

Second, the post-editing module

1. Added blog post

Create

Post/create

Parameters:

JSON string format parameters, example:

{"UserID": 3, "token": "A1c0738a6cf054606b055a419c3390f3", "title": "Python", "content": "Python port Test"}

Response:

{"Data": [{"Content": "Python port Test", "title": "Python"}], "code": "XX", "userid": 3}

Example

Import requests

Import JSON

Import Hashlib

M5 = Hashlib.md5 ()

M5.update (' Lily ')

PWD = M5.hexdigest ()

Print pwd

Print "Create post------"

data = Json.dumps ({' UserID ': 1, token: "A1c0738a6cf054606b055a419c3390f3" s, ' title ': "MySQL", ' content ': ' MySQL Learn '})

r = Requests.post (' http://localhost:8080/create/', data = data)

Print R.text

Print type (R.json ())

Print str (R.json ())

2. Revise the blog post

Update

Post/update

Parameters:

JSON string format parameters, example:

{"UserID": 3, "token": "A1c0738a6cf054606b055a419c3390f3", "ArticleID": One, "title": "Python", "Content": "Test Test"}

Response:

{"Data": [{"Content": "Python port Test", "title": "Python"}], "code": "XX", "userid": 3}

Example

Import requests

Import JSON

Print "Query posts of user------"

data = Json.dumps ({' UserID ': 3, ' token ': ' a1c0738a6cf054606b055a419c3390f3 ', ' offset ': 2, ' Lines ': 3})

r = Requests.post (' http://localhost:8080/getBlogsOfUser/', data = data)

Print R.status_code

Print R.text

Print type (R.json ())

Print str (R.json ())

3. Check the user's blog post

Getblogsofuser

Post/getblogsofuser

Parameters:

JSON string format parameters, example:

{"UserID": 3, "token": "A1C0738A6CF054606B055A419C3390F3"}

{"UserID": 3, "token": "A1C0738A6CF054606B055A419C3390F3", "offset": 2, "Lines": 3}

Response:

{"Data": [{"update_time": null, "title": "Oracle", "Content": "Oracle Test", "ArticleID": 5, "owner": 2, "posted_on": "201 6-09-02 14:24:58 "}, {" Update_time ": null," title ":" C "," Content ":" C Test "," ArticleID ": 4," owner ": 2," posted_on ":" 201 6-09-02 14:24:49 "}]," code ":" XX "," userid ": 2}

Example

Import requests

Import JSON

Print "Query posts of user------"

data = Json.dumps ({' UserID ': 3, ' token ': ' a1c0738a6cf054606b055a419c3390f3 ', ' offset ': 2, ' Lines ': 3})

r = Requests.post (' http://localhost:8080/getBlogsOfUser/', data = data)

Print R.status_code

Print R.text

Print type (R.json ())

Print str (R.json ())

4. Check the contents of the blog

Getblogcontent

Post/getblogcontent

Parameters:

Request Address, Example:

Http://localhost:8080/getBlogContent/1

Response:

{"Code": "XX", "Data": [{"update_time": null, "title": "Python", "content": "Python port Test", "ArticleID": 1, "owner": 2 , "posted_on": "2016-09-02 14:24:24"}]}

Example

Import requests

Print "Query post------"

ArticleID = 2

r = Requests.get (' http://localhost:8080/getBlogContent/' + str (ArticleID))

Print R.status_code

Print R.text

Print type (R.json ())

Print str (R.json ())

5, Bulk Query Blog

Getblogscontent

Post/getblogscontent

Parameters:

Request Address, Example:

http://localhost:8080/getBlogsContent/articleIds=1,2,3

Response:

{"Code": "XX", "Data": [{"update_time": null, "title": "Python", "content": "Python port Test", "ArticleID": 1, "owner": 2 , "posted_on": "2016-09-02 14:24:24"}, {"Update_time": null, "title": "Java", "content": "Java port test", "ArticleID": 2, "Owner": 2, "posted_on": "2016-09-02 14:24:32"}, {"Update_time": null, "title": "C + +", "Content": "C + + Test", "ArticleID" : 3, "owner": 2, "posted_on": "2016-09-02 14:24:42"}]}

Example

Import requests

Print "Query posts by blogId------"

r = Requests.get (' http://localhost:8080/getBlogsContent/' + str ("articleids=1,2,3"))

Print R.status_code

Print R.text

Print type (R.json ())

Print str (R.json ())

6. Delete Blog Posts

Delete

Post/delete

Parameters:

JSON string format parameters, example:

{"UserID": 1, "token": "868d26e05666c5aaeb76d361faa7448c", "ArticleID": [[i]}

Response:

{"ArticleID": [1, 2, 3], "code": "XX", "userid": 1}

Example

Import requests

Import JSON

Import Hashlib

M5 = Hashlib.md5 ()

M5.update (' Lily ')

PWD = M5.hexdigest ()

Print pwd

Print "Delete post------"

data = Json.dumps ({"userid": 1, "token": "868d26e05666c5aaeb76d361faa7448c", "ArticleID": [3,4,5]}) #

r = Requests.delete (' http://localhost:8080/delete/', data = data)

Print R.status_code

Print R.text

Print type (R.json ())

Print str (R.json ())

Description

If these interfaces are requested in some interface request plug-ins (such as Httprequester), there is no space between key-value pairs when the parameter is passed, and the string must be enclosed in double quotation marks, which differs from the Python application request.

Python interface test automation instructions and code examples: including GET, post, put, delete and other methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.