美文网首页
使用requests编写接口测试用例

使用requests编写接口测试用例

作者: Chaweys | 来源:发表于2021-01-25 07:37 被阅读0次

总结断言的分类:
#msg:判断不成立时需要反馈的字符串

self.assertEqual(first, second, msg=None)      判断两个参数是否相等:first == second,不是则fail

self.assertNotEqual(first, second, msg=None)   判断两个参数不相等:first != second,是则fail

self.assertIn(member, container, msg=None)     判断是字符串是否包含:member in container,不是则fail

self.assertNotIn(member, container, msg=None)  判断是字符串是否不包含:member not in container,是则fail

self.assertTrue(expr, msg=None)                判断是否为真:expr is True,不是则fail

self.assertFalse(expr, msg=None)               判断是否为假:expr is False,是则fail

self.assertIs(arg1,arg2,msg=None)              判断arg1,arg2是否为同一个对象,不是则fail

self.assertIsNot(arg1,arg2,msg=None)           判断arg1,arg2是否为不同对象,是则fail

self.assertIsNone(obj, msg=None)               判断是否为None:obj is None,不是则fail

self.assertIsNotNone(obj, msg=None)            判断是否不为None:obj is not None,是则fail

self.assertIsInstance(obj,cls,msg=None)        判断obj是否是cls的实例,不是则fail

self.assertIsNotInstance(obj,cls,msg=None)     判断obj是否不是cls的实例,是则fail


#coding=utf-8
from until.RequestUtils import *
import unittest

class TestIndexcatagry(unittest.TestCase):

    #测试首页分类列表用例
    def testIndexCatagryList(self):
        url="https://api.xdclass.net"

        req=RequestUtil()    #继承的请求工具类
        response=req.request(url+"/pub/api/v1/web/all_category","get")
        self.assertEqual(response["code"],0,"请求首页列表异常")
        self.assertTrue(len(response["data"])>0,"视频列表为空")


    #测试视频卡片列表用例
    def testIndexVideoCard(self):
        url = "https://api.xdclass.net"

        req = RequestUtil()   #继承的请求工具类
        response = req.request(url + "/pub/api/v1/web/index_card", "get")
        self.assertEqual(response["code"],0,"请求视频卡片接口异常")
        card_list=response["data"]
        for card in card_list:
            self.assertTrue(len(card["title"]) < 0,"视频标题为空,id为:"+str(card["id"]))


    #测试登录接口
    def testIndexLogin(self):
        url = "https://api.xdclass.net"

        req = RequestUtil()    #继承的请求工具类
        data={"phone":"13045890834","pwd":"hdc@328216"}
        headers={"Content-Type":"application/x-www-form-urlencoded"}
        response = req.request(url+"/pub/api/v1/web/web_login","post",params=data,headers=headers)
        self.assertEqual(response["code"],0,"登录异常")



if __name__ == '__main__':
    unittest.main(verbosity=2)


"""
testIndexCatagryList (__main__.TestIndexcatagry) ... ok
testIndexLogin (__main__.TestIndexcatagry) ... ok
testIndexVideoCard (__main__.TestIndexcatagry) ... FAIL

======================================================================
FAIL: testIndexVideoCard (__main__.TestIndexcatagry)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:/HDCZU_Test/接口自动化/case/TestIndex.py", line 26, in testIndexVideoCard
    self.assertTrue(len(card["title"]) < 0,"视频标题为空,id为:"+str(card["id"]))
AssertionError: False is not true : 视频标题为空,id为:1

----------------------------------------------------------------------
Ran 3 tests in 0.187s

FAILED (failures=1)

Process finished with exit code 1
"""

相关文章

网友评论

      本文标题:使用requests编写接口测试用例

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