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)- 从配置文件中读取测试数据的更多相关文章

  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. rpc框架解释

    远程过程调用协议RPC(Remote Procedure Call Protocol) RPC是指远程过程调用,也就是说两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方 ...

  2. 吴裕雄--天生自然 PHP开发学习:MySQL 读取数据

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  3. Acunetix WVS安全测试软件使用教程(入门级)

    1.下载 Acunetix WVS 10.5软件,下载地址为:https://pan.baidu.com/s/1Koku0Lhya5PgphMVL7w19g 密码:v438 2.压缩包中有破解说明,按 ...

  4. js运算符相关要点

    取模运算的结果符号只与左边值的符号有关: var x = 7 % 3; // 结果为 1 var y = 7 % (-3); // 结果为 1 var z = (-7) % 3; // 结果为 -1

  5. python 3.6

    安装了最新版anaconda3-4.3 发现jupyter-notebook 少了一些东西.需要手工安装 https://github.com/Anaconda-Platform/nbpresent

  6. C/C++中开平方函数sqrt()的用法

    开平方使用sqrt()函数 使用方法: 包含于math.h头文件 sqrt(float * number),返回number的开平方数,返回值为浮点型 sqrt使用时大多需要要强制类型转化,因为sqr ...

  7. PAT甲级——1152.Google Recruitment (20分)

    1152 Google Recruitment (20分) In July 2004, Google posted on a giant billboard along Highway 101 in ...

  8. 爬虫笔记(十一)——认识cookie

    什么是cookie? 在爬虫的使用中,如果涉及登录等操作时,经常会使用到cookie.简单的来说,我们访问每一个互联网页面,都是通过HTTP协议进行的,而HTTP协议是一个无状态协议,所谓的无状态协议 ...

  9. 解决 Win7 远程桌面 已停止工作的问题

    Windows 7远程桌面登录时崩溃, 错误提示如下: 问题签名: 问题事件名称: APPCRASH 应用程序名: mstsc.exe 应用程序版本: 6.1.7601.18540 应用程序时间戳: ...

  10. BaseAdapter教程(2) BaseAdapter的notifyDataSetChanged动态刷新

    遇到了这麽一个需求,ListView滑到最底,然后会自动在底部加入新的Cell,实现动态刷新. 1. 首先,为ListView加上setOnScrollListener. lvHomePostItem ...