case.config

  1. # 1. http_request.py
  2.  
  3. import requests
  4.  
  5. class HttpRequest:
  6. def http_request(self, url, method, data=None, cookie=None):
  7. try:
  8. if method.upper() == "GET":
  9. res = requests.get(url, data, cookies=cookie)
  10. elif method.upper() == "POST":
  11. res = requests.post(url, data, cookies=cookie)
  12. else:
  13. print("请输入正确的参数")
  14. except Exception as e:
  15. print("请求报错了:{}".format(e))
  16. raise e
  17. return res
  1. # 2.get_data7.py
  2.  
  3. from openpyxl import load_workbook
  4. from day_20191202.config_data_depart.read_config import ReadConfig
  5.  
  6. class DoExcel:
  7. def __init__(self, file, sheet):
  8. self.file = file
  9. self.sheet = sheet
  10.  
  11. def get_data(self, mode="all"):
  12. mode = ReadConfig().get_config("case.config", "MODE", "mode")
  13. wb = load_workbook(self.file)
  14. sheet = wb[self.sheet]
  15. case_data = []
  16. for i in range(2, sheet.max_row+1):
  17. sub_data = {}
  18. sub_data["case_id"] = sheet.cell(i, 1).value
  19. sub_data["url"] = sheet.cell(i, 2).value
  20. sub_data["method"] = sheet.cell(i, 3).value
  21. sub_data["data"] = eval(sheet.cell(i, 4).value)
  22. sub_data["expected"] = sheet.cell(i, 5).value
  23. case_data.append(sub_data)
  24. if mode == "all":
  25. final_data = case_data
  26. else: # [1, 2, 5]
  27. final_data = []
  28. for test_data in case_data: # 遍历每一条测试数据,如果测试数据的id在传入的列表中,就把这条数据加到final_data
  29. if test_data["case_id"] in eval(mode):
  30. final_data.append(test_data)
  31.  
  32. return final_data
  33.  
  34. if __name__ == "__main__":
  35. case_data = DoExcel("data_7.xlsx", "sh2").get_data()
  36. print(case_data)
  1. # 3. read_config.py
  2.  
  3. import configparser
  4.  
  5. class ReadConfig:
  6. def get_config(self, file, section, option):
  7. cf = configparser.ConfigParser()
  8. cf.read(file, encoding="utf-8")
  9. return cf.get(section, option)
  10.  
  11. if __name__ == "__main__":
  12. r = ReadConfig().get_config("case.config", "MODE", "mode")
  13. print(r)
  1. # 4. test_login.py
  2.  
  3. import unittest
  4. from API_AUTO.tools.http_request import HttpRequest
  5. from ddt import ddt, data, unpack
  6. from day_20191202.config_data_depart.get_data7 import DoExcel
  7. from day_20191202.config_data_depart.read_config import ReadConfig
  8.  
  9. # 自定义取某几条数据执行用例
  10. # mode = ReadConfig().get_config("case.config", "MODE", "mode")
  11. test_data = DoExcel("data_7.xlsx", "sh2").get_data()
  12. # print(test_data)
  13.  
  14. @ddt
  15. class TestLogin(unittest.TestCase):
  16. def setUp(self):
  17. print("start testing...")
  18.  
  19. def tearDown(self):
  20. print("case done.")
  21.  
  22. @data(*test_data)
  23. @unpack
  24. # 注意新的表格数据多了id,要用参数来接收case_id
  25. def test_api(self, case_id, url, method, data, expected):
  26. # print("url:", url)
  27. # print("method", method)
  28. # print("data_c", data)
  29. res = HttpRequest().http_request(url, method, data)
  30. r = res.json()["info"]
  31. try:
  32. self.assertEqual(r, expected)
  33. except AssertionError as e:
  34. print("there is an error in the case {}".format(e))
  35. raise e
  36.  
  37. if __name__ == '__main__':
  38. TestLogin().test_api()
  1. # 5. run.py
  2.  
  3. import unittest
  4. from day_20191202.config_data_depart.class_test_ddt import TestLogin
  5. import HTMLTestRunner
  6.  
  7. suite = unittest.TestSuite()
  8. loader = unittest.TestLoader()
  9. suite.addTest(loader.loadTestsFromTestCase(TestLogin))
  10.  
  11. with open("login7.html", "wb") as file:
  12. runner = HTMLTestRunner.HTMLTestRunner(stream=file,
  13. verbosity=2,
  14. title="登录7测试报告",
  15. description="管住心情,就是胜利")
  16. runner.run(suite)
  17. # runner = unittest.TextTestRunner(verbosity=2)
  18. # runner.run(suite)

unittest(13)- 从配置文件中读取测试数据的更多相关文章

  1. Feign从配置文件中读取url

    Feign的url和name都是可配置的,就是从配置文件中读取的属性值,然后用占位符引用就可以了: ${rpc.url} @FeignClient(name = "me", url ...

  2. spring boot: 从配置文件中读取数据的常用方法(spring boot 2.3.4)

    一,从配置文件中读取数据有哪些方法? 通常有3种用法: 1,直接使用value注解引用得到配置项的值 2,  封装到Component类中再调用 3,  用Environment类从代码中直接访问 生 ...

  3. 【Python学习笔记七】从配置文件中读取参数

    将一些需要更改或者固定的内容存放在配置文件中,通过读取配置文件来获取参数,这样修改以及使用起来比较方便 1.首先是配置文件的写法如下一个environment.ini文件: 里面“[]”存放的是sec ...

  4. spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。

    需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@  而不是  ...

  5. 监听tomcat服务器启动/关闭并从配置文件中读取参数进行初始化

    监听tomcat服务器启动/关闭很简单(2步): 1. 建立一个类实现ServletContextListener接口,重写其中的方法(contextDestroyed和contextInitiali ...

  6. C# 从配置文件中读取/写入信息

    读取: var currMemberID = System.Configuration.ConfigurationManager.AppSettings["tolunaMemberID&qu ...

  7. 从配置文件中读取数据获取Connection

    配置文件 db.driver=com.mysql.jdbc.Driver db.url=jdbc\:mysql\://localhost\:3306/mybase db.user=root db.ps ...

  8. 在ASP.NET 5中读取配置文件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 在ASP.NET 5中摒弃了之前配置文件的基础结构,引入了一个全新配置文件系统.今天推荐的文 ...

  9. java 如何从配置文件(.properties)中读取内容

    1.如何创建.properties文件 很简单,建立一个txt文件,并把后缀改成.properties即可 2.将.properties文件拷入src的根目录下 3..properties文件内容格式 ...

随机推荐

  1. softmax推导以及多分类的应用

  2. 1.where子句的优化

    不需要在牺牲可读性的情况下重写sql,因为mysql会自动进行类似的优化. 1.去掉无用的括号 ((a AND b) AND c OR (((a AND b) AND (c AND d)))) -&g ...

  3. Scrapy连接到各类数据库(SQLite,Mysql,Mongodb,Redis)

    如何使用scrapy连接到(SQLite,Mysql,Mongodb,Redis)数据库,并把爬取的数据存储到相应的数据库中. 一.SQLite 1.修改pipelines.py文件加入如下代码 # ...

  4. WIFI无线协议802.11a/b/g/n/ac的演变以及区别

    摘自:https://blog.csdn.net/Brouce__Lee/article/details/80956945 毫无疑问,WiFi的出现普及带给我们巨大的上网便利,所以了解一下WiFi对应 ...

  5. Java之线程通信的方法

    /** * 线程通信的例子:使用两个线程打印 1-100.线程1, 线程2 交替打印 * * 涉及到的三个方法: * wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器. * no ...

  6. 9. Dockerfile 实际操作 (把 python app 打包成 image 并运行)

    1. 创建并进入 flask-hello-world mkdir flask-hello-world && cd flask-hello-world 2. 编写 python 文件 a ...

  7. 金蝶cloud成本核算流程

  8. screen模式下鼠标无法滚动【问题】

    忍了很久, 终于查到原因了. 回滚模式: CTRL+A (释放), [ 切换模式: CTRL+ C 参考: https://serverfault.com/questions/206303/how-t ...

  9. 吴裕雄--天生自然TensorFlow高层封装:解决ImportError: cannot import name 'tf_utils'

    将原来版本的keras卸载了,再安装2.1.5版本的keras就可以了.

  10. unity学习 5.x打包

    using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor; publi ...