zoukankan      html  css  js  c++  java
  • 接口测试-requests+unittest实例

    requests+unittest实例

    1.将requests封装成unittest

    继续requests实例,再创建一个py文件 'unittest_requests'

    import unittest
    import json
    import res
    class MyUnit(unittest.TestCase):
        def setUp(self):
            print('开始')
        def test_get(self):
            res = obj.get(
                full_url='http://localhost:8888/book_list',
                headers_dict={},
            )  # 封装的方法
            error_code = res['error_code']
            self.assertEqual(error_code, 0, msg='与预期不符应为"0"')
            print('符合预期')
    
        def test_get_queries(self):
            res1 = obj.get_queries(
                full_url='http://localhost:8888/book_info',
                params_dict={'bookname': '软件测试', 'checkstatus': 'on'},
                headers_dict={},
            )  # 封装的方法
            error_code = res1['error_code']
            self.assertEqual(error_code, 0, msg='与预期不符应为"0"')
            print('符合预期')
    
        def test_post_forms(self):
            res2 = obj.post_forms(
                full_url='http://localhost:8888/login01',
                data={"username": "zhangshan", "password": "123456"},
                headers_dict={'content_type': 'application/x-www-form-urlencoded'},
            )  # 封装的方法
            checkstatus = res2['checkstatus']
            self.assertEqual(checkstatus, 'on', msg='与预期不符应为"on"')
            print('符合预期')
    
        def test_post_json(self):
            res3 = obj.post_json(
                full_url='http://localhost:8888/login02',
                data=json.dumps({"username": "zhangshan", "password": "123456"}),
                headers_dict={'content_type': 'application/json'}
            )
            checkstatus = res3['checkstatus']
            self.assertEqual(checkstatus, 'on', msg='与预期不符应为"on"')
            print('符合预期')
        def tearDown(self):
            print('结束')
    obj = res.Httprequests()  # 包名.类名 类名() 实例化
    
    if __name__ == '__main__':
        unittest.main()
    

    直接右键运行
    结果:四条用例执行成功

  • 相关阅读:
    ASP.NET 2.0 X64的奇怪问题
    【分享】从网上爬的WPF学习资料
    大家一起学习less 5:字符串插值
    大家一起学习less 3:命名空间
    我的模块加载系统 v18
    大家一起学习less 2:自带函数
    “计算机之子”的MVVM框架源码学习笔记
    我的MVVM框架 v0.1发布
    大家一起学习less 6:一些有用的混合函数
    less源码学习
  • 原文地址:https://www.cnblogs.com/zhangshan33/p/12012207.html
Copyright © 2011-2022 走看看