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# 动态执行JS

    有时候需要,在程序中灵活的嵌入自定义的计算逻辑,使用C#加载JS脚本形式可以实现: // 添加引用 using Microsoft.JScript; string jsStr = "var ...

  2. Jav面向对象

    /* * 面向对象: * 1.关注现实存在的事物的各方面信息,从对象的角度出发,根据事物的特征进行程序设计 * 2.对象:用来描述客观事物的一个实体 * 3.类:具有相同属性和方法的一组对象的集合 * ...

  3. phpspreadsheet 中文文档(八)读写文件+读取文件

    2019年10月11日14:09:40 配置设定 将PhpSpreadsheet文件包含在脚本中之后,但是在实例化Spreadsheet对象或加载工作簿文件之前,可以设置许多配置选项,这些配置选项将影 ...

  4. Nginx - 安装并启动Nginx

    1 - 安装Nginx 官网步骤:http://nginx.org/en/linux_packages.html#RHEL-CentOS [Anliven@h202 ~]$ sudo vim /etc ...

  5. 转 java 8 lamba stream

    一直在写中间件相关的代码,提供SDK给业务方使用,但很多业务方还一直停留在1.7版本,迟迟不升级,为了兼容性,不敢在代码中使用Java8的一些新特性,比如Stream之类的,虽然不能用,但还是要学一下 ...

  6. Vue父子组件相互传值及调用方法的方案

    Vue父子组件相互传值及调用方法的方案 一.调用方法: 1.父组件调用子组件方法: 2.子组件调用父组件方法: 参考:https://www.cnblogs.com/jin-zhe/p/9523782 ...

  7. STL源码之traits编程技法

    摘要 主要讨论如何获取迭代器相应型别.使用迭代器时,很可能用到其型别,若需要声明某个迭代器所指对象的型别的变量,该如何解决.方法如下: function template的参数推导机制 例如: tem ...

  8. linux CC攻击解决方法

    linux CC攻击1 由于不断的请求接口 导致带宽不足 然后不断的运行mysql语句 造成cpu饱和 这个时候服务器重负不堪 导致运行代码暖慢 导致入侵 一般采取的方法http://newmirac ...

  9. Java操作fastDFS

    一.加入Maven依赖 <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs- ...

  10. pom中parent和dependency区别以及dependencyManagement区别

    真的很详细 很感动 1.在同一个pom文件下,如果<dependencies>和<dependencyManagement>中都对该jar做了依赖,以<dependenc ...