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

猜你喜欢

转载自www.cnblogs.com/D666/p/9165052.html