• 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)
  • 相关阅读:
    JavaScript字面量
    JavaScript一元运算符、二元运算符和三元运算符
    JavaScript变量声明与变量声明提前
    JavaScript作用域链
    JavaScript变量污染
    语法糖
    JavaScript包装对象
    JavaScript全局变量与局部变量
    JavaScript标识符
    JS函数命名规范
  • 原文地址:https://www.cnblogs.com/lisa2016/p/12531835.html
Copyright © 2020-2023  润新知