• python mock之接口请求及测试用例


    1.下载mock

    2.login.json

    编写内容:

    [
      {
        "request" :
          {
            "method" : "post",
            "uri" : "/login",
            "json":
            {
            "username":"admin",
            "password":"admin",
            "roleID":22
            }
    
        },
        "response" :
          {
            "json" :
            {
            "username":"xiaolizi",
            "userID":22,
            "token":"asdgfhh322456asfgrsfss"
            }        
          }
      }
    ]

    3.命令行运行

    java -jar moco-runner-0.12.0-standalone.jar http -p 12306 -c login.json

     4.用postman 发送post请求

     5.编写测试用例

    上代码

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    import unittest
    import requests
    """
    2.mock login接口,测试用例
    """
    class MockLoginTest(unittest.TestCase):
        def setUp(self):
            self.url='http://localhost:12306'
    
        def tearDown(self):
            pass
    
        def getUrl(self,path):
            return self.url+path
    
        def getToken(self):
            """get token"""
            data={
                "username":"admin",
                "password":"admin",
                "roleID":22
            }
            r=requests.post(self.getUrl('/login'),json=data)
            #print("token----------",r.json()['token'])
            return r.json()['token']
    
        def test_login(self):
            """验证登录的接口"""
            data={
                "username":"admin",
                "password":"admin",
                "roleID":22
            }
    
            r=requests.post(self.getUrl("/login"),json=data)
            #print("username---------",r.json()['username'])
            self.assertEqual(r.status_code,200)
            self.assertEqual(r.json()['username'],'xiaolizi')
    
    if __name__=='__main__':
        unittest.main(verbosity=2)

    执行结果:

     接口请求,用python写

    上代码

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    import requests
    import json
    """
    1.mock login请求接口
    """
    url="http://localhost:12306/login"
    
    data= {"username":"admin","password":"admin","roleID":22}
    #json转换成字符串
    data=json.dumps(data)
    headers = {'content-type': 'application/json'}
    
    response=requests.request("POST",url,headers=headers,data=data)
    #response=requests.post(url,headers=headers,json=data)
    
    print(response.status_code)
    print(response.json())
    print(response.url)
    print(response.text)        # 打印解码后的返回数据
    print(response.content)
  • 相关阅读:
    第一章——第二节 启动模式
    Android 展示键盘时候布局被修改的问题
    JAVA混型和潜在类型机制
    第一章——Activity的生命周期
    android 程序中禁止屏幕旋转和重启Activity
    项目知识—九
    项目知识——八
    项目知识——七
    Drawable复习—第六章
    项目知识(六)
  • 原文地址:https://www.cnblogs.com/lisa2016/p/12531835.html
Copyright © 2020-2023  润新知