接口测试-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()

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

posted @ 2019-12-09 17:15  张珊33  阅读(335)  评论(0编辑  收藏  举报