美文网首页学习小殿
Postman接口从入门到精通

Postman接口从入门到精通

作者: 司小幽 | 来源:发表于2021-02-23 15:39 被阅读0次

    工具资源

    资源

    Postman接口从入门到精通

    工具

    Cmder
    cmder如何切换到指定目录?
    Node.js安装详细步骤教程(Windows版)

    实战演练

    测试对象:v2ex的api
    文档:https://www.v2ex.com/p/7v9TEc53
    api地址:https://www.v2ex.com/api/topics/hot.json

    断言

    #GET http://itest.info/course
    tests["状态码必须是200"] = responseCode.code === 200;
    pm.test("返回正确的状态码",function(){
        pm.expect(pm.response).to.have.status(200);
        pm.expect(pm.response).to.be.success;
    });
    
    #GET https://www.v2ex.com/api/topics/hot.json
    var res = JSON.parse(responseBody);
    console.log(res.length);
    tests["必须返回10条数据"] = res.length === 10
    
    
    #GET http://www.itest.info/courses
    var cheerio = require('cheerio');
    var l = cheerio.load(responseBody);
    
    pm.test("必须包含4门课程",function(){
        pm.response.to.be.success;
        pm.expect(l('servive-block-in').length === 4);
    });
    
    #GET https://www.v2ex.com/api/nodes/show.json?name={{node_name}}
    #从环境变量中读取变量
    tests["Status code is 200"] = responseCode.code === 200;
    var jsonData = JSON.parse(responseBody);
    var node_name = postman.getEnvironmentVariable("node_name");
    tests["返回值的name必须跟参数中的name相等"] = jsonData.name === node_name;
    console.log(node_name);
    
    #GET https://www.v2ex.com/api/nodes/show.json?name={{node_name}}
    #从数据中读取变量
    tests["Status code is 200"] = responseCode.code === 200;
    var jsonData = JSON.parse(responseBody);
    var node_name = postman.getEnvironmentVariable("node_name");
    tests["返回值的name必须跟参数中的name相等"] = jsonData.name === data.node_name;
    console.log(node_name);
    
    #从命令行运行Postman
    newman run v2ex.postman_collection.json -d text_data.json -r html,cli,json,junit
    

    Postman导出python代码

    数据驱动

    import requests
    import unittest
    
    
    class V2exAPITestCase(unittest.TestCase):
        def test_mode_api(self):
            url = "https://www.v2ex.com/api/nodes/show.json"
            querystring = {"name":"php"}
            for node_name in ['php','python','qna']:
                response = requests.request("GET", url, params={'name':node_name}).json()
                self.assertEqual(response['name'],node_name)
    
    
    if __name__=='__main__':
        unittest.main()
    

    非数据驱动

    import requests
    import unittest
    
    
    
    class V2exAPITestCase(unittest.TestCase):
        def test_mode_api(self):
            url = "https://www.v2ex.com/api/nodes/show.json"
    
            querystring = {"name":"php"}
            response = requests.request("GET", url, params=querystring).json()
            self.assertEqual(response['name'],querystring['name'])
    
    
    if __name__=='__main__':
        unittest.main()
    ···

    相关文章

      网友评论

        本文标题:Postman接口从入门到精通

        本文链接:https://www.haomeiwen.com/subject/pbwrfltx.html