python接口测试
Posted d666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python接口测试相关的知识,希望对你有一定的参考价值。
引入unittest框架
get请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#coding: utf-8 import unittest import requests import json class TestGet(unittest.TestCase): def setUp( self ): self .test_url = ‘http://localhost:8080/jenkins/api/json?tree=jobs[name]‘ def test_get( self ): self .r = requests.get( self .test_url) ‘‘‘ result = self.r.text json_result = json.loads(result) #反序列化过程 ‘‘‘ json_result = self .r.json() #反序列化等同于上面注释代码 print json_result self .assertEquals(json_result[ ‘jobs‘ ][ 0 ][ ‘name‘ ], ‘check_python_version‘ ) if __name__ = = "__mian__" : unittest.mian() |
post请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#coding: utf-8 import unittest import requests import json class TestPost(unittest.TestCase): def setUp( self ): self .test_url = ‘http://localhost:8080/jenkins/job/check_python_version/build‘ def test_post( self ): self .r = requests.post( self .test_url, data = {}, auth = ( ‘admin‘ , ‘123456‘ )) print self .r.status_code self .assertEqual( self .r.status_code, 200 ) if __name__ = = "__mian__" : unittest.mian() |
以上是关于python接口测试的主要内容,如果未能解决你的问题,请参考以下文章