unttest管理接口用例

Posted mr-zy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unttest管理接口用例相关的知识,希望对你有一定的参考价值。

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管理接口用例的主要内容,如果未能解决你的问题,请参考以下文章

单元测试框架unittest

Python Api接口自动化测试框架 代码写用例

python接口自动化测试-unittest-单个用例管理

[分享] 自己写的接口测试平台-----IAPTest

python接口自动化测试-unittest-批量用例管理

python 接口自动化测试使用unittest 批量用例管理