1.加入unittest框架

#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)
        ":
            pass
        else:
            raise ValueError

    def testlogin02(self):
        body = {"}
        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 = {"}
        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 = {"}
        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
import HTMLTestRunner

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)
        ":
            pass
        else:
            raise ValueError

    def testlogin02(self):
        body = {"}
        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 = {"}
        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 = {"}
        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()

3.断言

  • if ... else ...  如上代码
  • try ... except ...
#coding=utf-8

import requests
import unittest

class TestApi(unittest.TestCase):

    def setUp(self):
        self.apiurl = "http://www.xxxx.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":"zy295240???"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        try:
            result = response.json()["values"]["loginName"]
            ":
                pass
            else:
                raise ValueError
        except:
            print ("testlogin01 error!")
        else:
            print ("testlogin01 ok!")

if __name__ == '__main__':
    unittest.main()
  • unttest 中 assert断言方式
#coding=utf-8

import requests
import unittestclass TestApi(unittest.TestCase):

    def setUp(self):
        self.apiurl = "http://www.xxxx.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)
        phone = response.json()["values"]["loginName"]
        self.assertEqual(phone,",msg="testlogin01 error!")

    def testlogin02(self):
        body = {"loginName":17779828881,"loginPwd":"zy123456"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        errorMsg =  response.json()["errorMsg"]
        self.assertNotEqual(errorMsg,u"成功",msg="testlogin02 error!")

    def testlogin03(self):
        body = {"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        errorMsg =  response.json()["errorMsg"]
        self.assertIn(u"密码错误",errorMsg,msg="testlogin03 error!")

    def testlogin04(self):
        body = {"}
        response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
        errorMsg =  response.json()["errorMsg"]
        self.assertNotIn(u"密码错误",errorMsg,msg="testlogin04 error!")

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

unittest管理接口用例的更多相关文章

  1. unittest管理接口用例(数据分离-读取excel)

    1.简单读取 #coding=utf-8 #调用封装好的excel读取公共方法 from python_API.common.ReadExcel import ReadExcel import req ...

  2. unittest 管理接口用例(数据分离-读取excel)

    1.公共模块 ---> login.xls """ common (package) ---> ReadFile.py """ ...

  3. requests,unittest——多接口用例,以及需要先登录再发报的用例

    之前写过最简单的接口测试用例,本次利用unittest进行用例管理,并出测试报告,前两个用例是两个不同网页的get用例,第三个是需要登录才能访问的网页A,并在其基础上访问一个需要在A页面点开链接才能访 ...

  4. python web自动化测试框架搭建(功能&接口)——接口用例实现

    测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...

  5. python3+requests+unittest:接口自动化测试(一)

    转载请表明出处:https://www.cnblogs.com/shapeL/p/9179484.html 简单介绍框架的实现逻辑,参考代码的git地址: https://github.com/zha ...

  6. python脚本实现接口自动化轻松搞定上千条接口用例

    接口自动化目前是测试圈主流的一个话题,我也在网上搜索了很多关于自动化的关键词,大多数博主分享的python做接口自动化都是以开源的框架,比如:pytest.unittest+ddt(数据驱动) 最常见 ...

  7. python学习笔记(28)-unittest单元测试-执行用例

    执行用例 #写一个测试类 import unittest import HTMLTestRunnerNew #写好的模块可以直接调用 #import HTMLTest #测试报告模板 from cla ...

  8. C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试

    在上篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及对应的对象模型,本篇继续微信小店的主题,介绍其中API接口的封装和测试使用.微信小店的相 ...

  9. Lenovo System x3650 设置管理接口地址

    1.开启服务器. 2.显示<F1> Setup提示后,按 F1.(此提示在屏幕上仅显示几秒钟.必须迅速按 F1.) 如果同时设置了开机密码和管理员密码,则必须输入管理员密码才能访问完整的 ...

随机推荐

  1. C# selenium 高级

    https://www.cnblogs.com/morang/p/7441091.html https://www.cnblogs.com/tobecrazy/p/4817946.html https ...

  2. 没有可用的软件包 xxx,但是它被其它的软件包引用了

    在linux下apt安装软件,弹出这个错. 解决,更新下资源: sudo apt-get update

  3. 【Gitlab】宝塔gitlab 修改管理员账号密码

    步骤: a. 切换目录:cd /opt/gitlab/bin b.执行 :sudo gitlab-rails console production 命令 开始初始化密码 c.在 irb(main):0 ...

  4. python非官方扩展库

    https://www.lfd.uci.edu/~gohlke/pythonlibs/ 安装方法: 1.下载自己需要的库文件 例如:Twisted-19.2.1-cp37-cp37m-win32.wh ...

  5. ThreadLocal源代码2

    private static int nextIndex(int i, int len) { return ((i + 1 < len) ? i + 1 : 0); } private stat ...

  6. Django-05-视图函数

    http请求中产生两个核心对象: http请求:HttpRequest对象 http响应:HttpResponse对象 所在位置:django.http 之前我们用到的参数request就是HttpR ...

  7. xorm-删除和软删除实例

    删除数据Delete方法,参数为struct的指针并且成为查询条件.注意:当删除时,如果user中包含有bool,float64或者float32类型,有可能会使删除失败 package main i ...

  8. python 递归-汉诺塔

    # 汉诺塔 a = "A" b = "B" c = "C" def hano(a, b, c, n): if n == 1: print(& ...

  9. vim安装 YCM 过程记录

    YCM(YouComplateMe) 属于Vim中大神级的插件,提供了类似于巨硬爸爸的VS中的代码补全,但是其安装方式也是比较复杂,因此特意写下一篇记录,记录下我自己如何安装这一插件的过程: 检查自己 ...

  10. 继承与构造函数(base关键字)

    1.背景 我:虽然通过继承减少了代码冗余,但是,每一个子类的构造函数还是需要给所有属性赋值的,很麻烦的. 师:这个好办,用base就行啦. 我:贝司?还吉他呢! 师:别急,首先我们先介绍下实例化子类对 ...