requests+unittest实例

1.将requests封装成unittest

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

import unittest
import res
class MyUnit(unittest.TestCase):
    def test_get(self):
        res1 = obj.get(
            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/login',
            data1={"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_postjson(self):
        res3 = obj.post_json(
            full_url='http://localhost:8888/login',
            data1={"username": "zhangshan", "password": "123456"},
            headers_dict={'content_type': 'application/json'}
        )
        checkstatus = res3['checkstatus']
        self.assertEqual(checkstatus, 'on', msg='与预期不符应为"on"')
        print('符合预期')
obj = res.Httprequests()  # 包名.类名 类名() 实例化
if __name__ == '__main__':
    unittest.main()

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

01-31 04:21