1.加入unittest框架

  1. #coding=utf-8
  2.  
  3. import requests
  4. import unittest
  5.  
  6. class TestApi(unittest.TestCase):
  7.  
  8. def setUp(self):
  9. self.apiurl = "http://www.xxxxx.com/customer/login.html"
  10. 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"}
  11. self.timeout = 1
  12.  
  13. def testlogin01(self):
  14. body = {"loginName":17779828888,"loginPwd":"zy123456"}
  15. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  16. ":
  17. pass
  18. else:
  19. raise ValueError
  20.  
  21. def testlogin02(self):
  22. body = {"}
  23. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  24. if response.json()["errorMsg"] == u"用户或者密码错误":
  25. pass
  26. else:
  27. raise ValueError
  28.  
  29. def testlogin03(self):
  30. body = {"}
  31. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  32. if response.json()["errorMsg"] == u"用户或者密码错误":
  33. pass
  34. else:
  35. raise ValueError
  36.  
  37. def testlogin04(self):
  38. body = {"}
  39. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  40. if response.json()["errorMsg"] == u"用户名不能为空":
  41. pass
  42. else:
  43. raise ValueError
  44.  
  45. def testlogin05(self):
  46. body = {"loginName":17779828888,"loginPwd":""}
  47. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  48. if response.json()["errorMsg"] == u"用户密码不能为空":
  49. pass
  50. else:
  51. raise ValueError
  52.  
  53. if __name__ == '__main__':
  54. unttest.main()

2.生成测试报告

  1. #coding=utf-8
  2.  
  3. import requests
  4. import unittest
  5. import HTMLTestRunner
  6.  
  7. class TestApi(unittest.TestCase):
  8.  
  9. def setUp(self):
  10. self.apiurl = "http://www.xxxxx.com/customer/login.html"
  11. 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"}
  12. self.timeout = 1
  13.  
  14. def testlogin01(self):
  15. body = {"loginName":17779828888,"loginPwd":"zy123456"}
  16. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  17. ":
  18. pass
  19. else:
  20. raise ValueError
  21.  
  22. def testlogin02(self):
  23. body = {"}
  24. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  25. if response.json()["errorMsg"] == u"用户或者密码错误":
  26. pass
  27. else:
  28. raise ValueError
  29.  
  30. def testlogin03(self):
  31. body = {"}
  32. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  33. if response.json()["errorMsg"] == u"用户或者密码错误":
  34. pass
  35. else:
  36. raise ValueError
  37.  
  38. def testlogin04(self):
  39. body = {"}
  40. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  41. if response.json()["errorMsg"] == u"用户名不能为空":
  42. pass
  43. else:
  44. raise ValueError
  45.  
  46. def testlogin05(self):
  47. body = {"loginName":17779828888,"loginPwd":""}
  48. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  49. if response.json()["errorMsg"] == u"用户密码不能为空":
  50. pass
  51. else:
  52. raise ValueError
  53.  
  54. if __name__ == '__main__':
  55. suit = unittest.TestSuite()
  56. testcases = [TestApi("testlogin01"),TestApi("testlogin02"),TestApi("testlogin03"),
  57. TestApi("testlogin04"),TestApi("testlogin05")]
  58. suit.addTests(testcases)
  59. dir = "D:\\testapi.html"
  60. path = open(dir,"wb")
  61. runner = HTMLTestRunner.HTMLTestRunner(stream=path,title="TestReport",description="TestDesc")
  62. runner.run(suit)
  63. path.close()

3.断言

  • if ... else ...  如上代码
  • try ... except ...
  1. #coding=utf-8
  2.  
  3. import requests
  4. import unittest
  5.  
  6. class TestApi(unittest.TestCase):
  7.  
  8. def setUp(self):
  9. self.apiurl = "http://www.xxxx.com/customer/login.html"
  10. 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"}
  11. self.timeout = 1
  12.  
  13. def testlogin01(self):
  14. body = {"loginName":17779828888,"loginPwd":"zy295240???"}
  15. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  16. try:
  17. result = response.json()["values"]["loginName"]
  18. ":
  19. pass
  20. else:
  21. raise ValueError
  22. except:
  23. print ("testlogin01 error!")
  24. else:
  25. print ("testlogin01 ok!")
  26.  
  27. if __name__ == '__main__':
  28. unittest.main()
  • unttest 中 assert断言方式
  1. #coding=utf-8
  2.  
  3. import requests
  4. import unittestclass TestApi(unittest.TestCase):
  5.  
  6. def setUp(self):
  7. self.apiurl = "http://www.xxxx.com/customer/login.html"
  8. 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; "}
  9. self.timeout = 1
  10.  
  11. def testlogin01(self):
  12. body = {"loginName":17779828888,"loginPwd":"zy123456"}
  13. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  14. phone = response.json()["values"]["loginName"]
  15. self.assertEqual(phone,",msg="testlogin01 error!")
  16.  
  17. def testlogin02(self):
  18. body = {"loginName":17779828881,"loginPwd":"zy123456"}
  19. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  20. errorMsg = response.json()["errorMsg"]
  21. self.assertNotEqual(errorMsg,u"成功",msg="testlogin02 error!")
  22.  
  23. def testlogin03(self):
  24. body = {"}
  25. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  26. errorMsg = response.json()["errorMsg"]
  27. self.assertIn(u"密码错误",errorMsg,msg="testlogin03 error!")
  28.  
  29. def testlogin04(self):
  30. body = {"}
  31. response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
  32. errorMsg = response.json()["errorMsg"]
  33. self.assertNotIn(u"密码错误",errorMsg,msg="testlogin04 error!")
  34.  
  35. if __name__ == '__main__':
  36. 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. [K8s]无yaml文件重启Pod

    在没有pod 的yaml文件时,强制重启某个pod kubectl get pod PODNAME -n NAMESPACE -o yaml | kubectl replace --force -f ...

  2. Jav面向对象

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

  3. echo的色彩处理

    在Shell脚本中,可以使用echo的-e选项使显示内容呈现出不同的颜色. 格式1:echo -e "\033[背景颜色代码;文字颜色代码m 输出的字符串 \033[0m" 格式2 ...

  4. 【计算机视觉】OpenCV篇(10) - 模式识别中的模板匹配

    什么是模式识别? 它指的是,对表征事物或现象的各种形式的信息进行处理和分析,从而达到对事物或现象进行描述.辨认.分类和解释的目的. 我们之所以可以很快辨别猫是猫.O不是0,就是因为在我们大脑中已经给猫 ...

  5. [ kvm ] 三种基础网络模型创建及分析

    1. 前言 最近在模拟生产环境在做测试,本来准备用 vmware 直接来实现的,本着学以致用的道理,选择直接在linux 环境使用 kvm 来模拟测试,遇到的第一个问题就是,网络环境的模拟.这里对比v ...

  6. ORA-01126: 数据库必须已装载到此实例并且不在任何实例中打开

    原因:修改归档模式的操作只能在 mount 状态下进行,不能处于 open 状态. SQL> alter database archivelog;alter database archivelo ...

  7. 工具分享:excel2json,将Excel表格转换为JSON

    此次分享的是github上的一个开源小工具:excel2json,用于把Excel表转换成json对象,并保存到一个文本文件. 项目地址:https://github.com/neil3d/excel ...

  8. js生成条形码——JsBarcode

    原文地址:https://www.cnblogs.com/huangenai/p/6347607.html 介绍一下在GitHub生成条形码的js插件→JsBarcode 条码支持的有: CODE12 ...

  9. python语法入门之变量

    目录 一.变量 1.1 什么是变量 1.2 怎么使用变量 1.3 变量名的命名规范 1.4 变量名的命名风格 1.5 变量的三大特征 2.常量 一.变量 1.1 什么是变量 # 变量就是可以变化的量, ...

  10. Unity 新手引导

    根据Shader动态生成遮罩 源码地址 圆形遮罩镂空处理脚本: using System; using System.Collections.Generic; using UnityEngine; u ...