码迷,mamicode.com
首页 > 其他好文 > 详细

unttest管理接口用例

时间:2019-11-15 15:47:47      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:ade   pre   erro   testcases   div   else   sel   测试报告   out   

1.加入unttest框架

#coding=utf-8

import requests
import unittest

class TestApi(unittest.TestCase):

    def setUp(self):
        self.apiurl = "http://www.xxxxx.com/customer/login.html"
        self.header = {"Cookie": "mediav=%7B%22eid%22%3A%22470884%22%2C%22ep%22%3A%22%22%2C%22vid%22%3A%22%40-vC_Waqh_%3AU%234K75o%5B!%22%2C%22ctn%22%3A%22%22%7D"}
        self.timeout = 1

    def testlogin01(self):
        body = {"loginName":17779828888,"loginPwd":"zy123456"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["values"]["loginName"] == "17779828888":
            pass
        else:
            raise ValueError

    def testlogin02(self):
        body = {"loginName":17779828881,"loginPwd":"123456"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["errorMsg"] == u"用户或者密码错误":
            pass
        else:
            raise ValueError

    def testlogin03(self):
        body = {"loginName":17779828888,"loginPwd":"123443"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["errorMsg"] == u"用户或者密码错误":
            pass
        else:
            raise ValueError

    def testlogin04(self):
        body = {"loginName":"","loginPwd":"123443"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["errorMsg"] == u"用户名不能为空":
            pass
        else:
            raise ValueError

    def testlogin05(self):
        body = {"loginName":17779828888,"loginPwd":""}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["errorMsg"] == u"用户密码不能为空":
            pass
        else:
            raise ValueError

if __name__ == __main__:
    unttest.main()

  

2.生成测试报告

#coding=utf-8

import requests
import unittest

class TestApi(unittest.TestCase):

    def setUp(self):
        self.apiurl = "http://www.xxxxx.com/customer/login.html"
        self.header = {"Cookie": "mediav=%7B%22eid%22%3A%22470884%22%2C%22ep%22%3A%22%22%2C%22vid%22%3A%22%40-vC_Waqh_%3AU%234K75o%5B!%22%2C%22ctn%22%3A%22%22%7D"}
        self.timeout = 1

    def testlogin01(self):
        body = {"loginName":17779828888,"loginPwd":"zy123456"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["values"]["loginName"] == "17779828888":
            pass
        else:
            raise ValueError

    def testlogin02(self):
        body = {"loginName":17779828881,"loginPwd":"123456"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["errorMsg"] == u"用户或者密码错误":
            pass
        else:
            raise ValueError

    def testlogin03(self):
        body = {"loginName":17779828888,"loginPwd":"123443"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["errorMsg"] == u"用户或者密码错误":
            pass
        else:
            raise ValueError

    def testlogin04(self):
        body = {"loginName":"","loginPwd":"123443"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["errorMsg"] == u"用户名不能为空":
            pass
        else:
            raise ValueError

    def testlogin05(self):
        body = {"loginName":17779828888,"loginPwd":""}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        if response.json()["errorMsg"] == u"用户密码不能为空":
            pass
        else:
            raise ValueError

if __name__ == __main__:
    suit = unittest.TestSuite()
    testcases = [TestApi("testlogin01"),TestApi("testlogin02"),TestApi("testlogin03"),
                 TestApi("testlogin04"),TestApi("testlogin05")]
    suit.addTests(testcases)
    dir = "D:\\testapi.html"
    path = open(dir,"wb")
    runner = HTMLTestRunner.HTMLTestRunner(stream=path,title="TestReport",description="TestDesc")
    runner.run(suit)
    path.close()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

unttest管理接口用例

标签:ade   pre   erro   testcases   div   else   sel   测试报告   out   

原文地址:https://www.cnblogs.com/Mr-ZY/p/11867074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!