unittest(13)- 从配置文件中读取测试数据
case.config
- # 1. http_request.py
- import requests
- class HttpRequest:
- def http_request(self, url, method, data=None, cookie=None):
- try:
- if method.upper() == "GET":
- res = requests.get(url, data, cookies=cookie)
- elif method.upper() == "POST":
- res = requests.post(url, data, cookies=cookie)
- else:
- print("请输入正确的参数")
- except Exception as e:
- print("请求报错了:{}".format(e))
- raise e
- return res
- # 2.get_data7.py
- from openpyxl import load_workbook
- from day_20191202.config_data_depart.read_config import ReadConfig
- class DoExcel:
- def __init__(self, file, sheet):
- self.file = file
- self.sheet = sheet
- def get_data(self, mode="all"):
- mode = ReadConfig().get_config("case.config", "MODE", "mode")
- wb = load_workbook(self.file)
- sheet = wb[self.sheet]
- case_data = []
- for i in range(2, sheet.max_row+1):
- sub_data = {}
- sub_data["case_id"] = sheet.cell(i, 1).value
- sub_data["url"] = sheet.cell(i, 2).value
- sub_data["method"] = sheet.cell(i, 3).value
- sub_data["data"] = eval(sheet.cell(i, 4).value)
- sub_data["expected"] = sheet.cell(i, 5).value
- case_data.append(sub_data)
- if mode == "all":
- final_data = case_data
- else: # [1, 2, 5]
- final_data = []
- for test_data in case_data: # 遍历每一条测试数据,如果测试数据的id在传入的列表中,就把这条数据加到final_data
- if test_data["case_id"] in eval(mode):
- final_data.append(test_data)
- return final_data
- if __name__ == "__main__":
- case_data = DoExcel("data_7.xlsx", "sh2").get_data()
- print(case_data)
- # 3. read_config.py
- import configparser
- class ReadConfig:
- def get_config(self, file, section, option):
- cf = configparser.ConfigParser()
- cf.read(file, encoding="utf-8")
- return cf.get(section, option)
- if __name__ == "__main__":
- r = ReadConfig().get_config("case.config", "MODE", "mode")
- print(r)
- # 4. test_login.py
- import unittest
- from API_AUTO.tools.http_request import HttpRequest
- from ddt import ddt, data, unpack
- from day_20191202.config_data_depart.get_data7 import DoExcel
- from day_20191202.config_data_depart.read_config import ReadConfig
- # 自定义取某几条数据执行用例
- # mode = ReadConfig().get_config("case.config", "MODE", "mode")
- test_data = DoExcel("data_7.xlsx", "sh2").get_data()
- # print(test_data)
- @ddt
- class TestLogin(unittest.TestCase):
- def setUp(self):
- print("start testing...")
- def tearDown(self):
- print("case done.")
- @data(*test_data)
- @unpack
- # 注意新的表格数据多了id,要用参数来接收case_id
- def test_api(self, case_id, url, method, data, expected):
- # print("url:", url)
- # print("method", method)
- # print("data_c", data)
- res = HttpRequest().http_request(url, method, data)
- r = res.json()["info"]
- try:
- self.assertEqual(r, expected)
- except AssertionError as e:
- print("there is an error in the case {}".format(e))
- raise e
- if __name__ == '__main__':
- TestLogin().test_api()
- # 5. run.py
- import unittest
- from day_20191202.config_data_depart.class_test_ddt import TestLogin
- import HTMLTestRunner
- suite = unittest.TestSuite()
- loader = unittest.TestLoader()
- suite.addTest(loader.loadTestsFromTestCase(TestLogin))
- with open("login7.html", "wb") as file:
- runner = HTMLTestRunner.HTMLTestRunner(stream=file,
- verbosity=2,
- title="登录7测试报告",
- description="管住心情,就是胜利")
- runner.run(suite)
- # runner = unittest.TextTestRunner(verbosity=2)
- # runner.run(suite)
unittest(13)- 从配置文件中读取测试数据的更多相关文章
- Feign从配置文件中读取url
Feign的url和name都是可配置的,就是从配置文件中读取的属性值,然后用占位符引用就可以了: ${rpc.url} @FeignClient(name = "me", url ...
- spring boot: 从配置文件中读取数据的常用方法(spring boot 2.3.4)
一,从配置文件中读取数据有哪些方法? 通常有3种用法: 1,直接使用value注解引用得到配置项的值 2, 封装到Component类中再调用 3, 用Environment类从代码中直接访问 生 ...
- 【Python学习笔记七】从配置文件中读取参数
将一些需要更改或者固定的内容存放在配置文件中,通过读取配置文件来获取参数,这样修改以及使用起来比较方便 1.首先是配置文件的写法如下一个environment.ini文件: 里面“[]”存放的是sec ...
- spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。
需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@ 而不是 ...
- 监听tomcat服务器启动/关闭并从配置文件中读取参数进行初始化
监听tomcat服务器启动/关闭很简单(2步): 1. 建立一个类实现ServletContextListener接口,重写其中的方法(contextDestroyed和contextInitiali ...
- C# 从配置文件中读取/写入信息
读取: var currMemberID = System.Configuration.ConfigurationManager.AppSettings["tolunaMemberID&qu ...
- 从配置文件中读取数据获取Connection
配置文件 db.driver=com.mysql.jdbc.Driver db.url=jdbc\:mysql\://localhost\:3306/mybase db.user=root db.ps ...
- 在ASP.NET 5中读取配置文件
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 在ASP.NET 5中摒弃了之前配置文件的基础结构,引入了一个全新配置文件系统.今天推荐的文 ...
- java 如何从配置文件(.properties)中读取内容
1.如何创建.properties文件 很简单,建立一个txt文件,并把后缀改成.properties即可 2.将.properties文件拷入src的根目录下 3..properties文件内容格式 ...
随机推荐
- softmax推导以及多分类的应用
- 1.where子句的优化
不需要在牺牲可读性的情况下重写sql,因为mysql会自动进行类似的优化. 1.去掉无用的括号 ((a AND b) AND c OR (((a AND b) AND (c AND d)))) -&g ...
- Scrapy连接到各类数据库(SQLite,Mysql,Mongodb,Redis)
如何使用scrapy连接到(SQLite,Mysql,Mongodb,Redis)数据库,并把爬取的数据存储到相应的数据库中. 一.SQLite 1.修改pipelines.py文件加入如下代码 # ...
- WIFI无线协议802.11a/b/g/n/ac的演变以及区别
摘自:https://blog.csdn.net/Brouce__Lee/article/details/80956945 毫无疑问,WiFi的出现普及带给我们巨大的上网便利,所以了解一下WiFi对应 ...
- Java之线程通信的方法
/** * 线程通信的例子:使用两个线程打印 1-100.线程1, 线程2 交替打印 * * 涉及到的三个方法: * wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器. * no ...
- 9. Dockerfile 实际操作 (把 python app 打包成 image 并运行)
1. 创建并进入 flask-hello-world mkdir flask-hello-world && cd flask-hello-world 2. 编写 python 文件 a ...
- 金蝶cloud成本核算流程
- screen模式下鼠标无法滚动【问题】
忍了很久, 终于查到原因了. 回滚模式: CTRL+A (释放), [ 切换模式: CTRL+ C 参考: https://serverfault.com/questions/206303/how-t ...
- 吴裕雄--天生自然TensorFlow高层封装:解决ImportError: cannot import name 'tf_utils'
将原来版本的keras卸载了,再安装2.1.5版本的keras就可以了.
- unity学习 5.x打包
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor; publi ...