unittest(9)- 使用ddt给测试用例传参
# 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_data4.py
from openpyxl import load_workbook
class DoExcel:
def __init__(self, file, sheet):
self.file = file
self.sheet = sheet
def get_data(self):
wb = load_workbook(self.file)
sheet = wb[self.sheet]
case_data = []
for i in range(1, sheet.max_row+1):
sub_data = {}
sub_data["url"] = sheet.cell(i, 1).value
sub_data["method"] = sheet.cell(i, 2).value
sub_data["data"] = eval(sheet.cell(i, 3).value)
sub_data["expected"] = sheet.cell(i, 4).value
case_data.append(sub_data)
return case_data
if __name__ == "__main__":
case_data = DoExcel("data_7.xlsx", "sh2").get_data()
print(case_data)
# 3. test_login.py
import unittest
from API_AUTO.tools.http_request import HttpRequest
from ddt import ddt, data, unpack
from day_20191202.learn_ddt.get_data4 import DoExcel
case_data = DoExcel("data_7.xlsx", "sh2").get_data()
@ddt
class TestLogin(unittest.TestCase):
@data(*case_data)
@unpack
def test_api(self, 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()
# 4. run.py
import unittest
from day_20191202.learn_ddt.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="登录4测试报告",
description="管住心情,就是胜利")
runner.run(suite)
# runner = unittest.TextTestRunner(verbosity=2)
# runner.run(suite)
unittest(9)- 使用ddt给测试用例传参的更多相关文章
- pytest 12 函数传参和fixture传参数request
前沿: 有的case,需要依赖于某些特定的case才可以执行,比如,登陆获取到的cookie,每次都需要带着他,为了确保是同一个用户,必须带着和登陆获取到的同一个cookies. 大部分的用例都会先登 ...
- pytest十一:函数传参和 firture 传参数 request
为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数.比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登录函数就行.但是登录 ...
- pytest_函数传参和firture传参数request
前言为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数. 比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行. ...
- 函数传参和firture传参数request
前言 为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数.比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行.但 ...
- python -- unittest测试用例函数无法传参的处理方法(ddt)
1.超继承 重写测试用例类的init方法,如下所示. import requests import unittest class XiaoheiCases(unittest.TestCase): de ...
- unittest改写传参方法
Python主要讲究简洁简单使用,所以它不像junit一样支持参数化测试,需要改装一下也可以传参.直接上代码实例 import unittest class ParametrizedTestCase( ...
- 上有传参下传json的接口调用
1.优化Myrequest import requests from conf.setting import log class MyRequest(): @staticmethod def post ...
- DophineSheduler上下游任务之间动态传参案例及易错点总结
作者简介 淡丹 数仓开发工程师 5年数仓开发经验,目前主要负责百得利MOBY新车业务 二手车业务及售后服务业务系统数仓建设 业务需求 在ETL任务之间调度时,我们有的时候会需要将上游的 ...
- Oracle 用Drapper进行like模糊传参查询需要在参数值前后带%符合
Oracle 用Drapper进行like模糊传参查询需要在参数值前后带%符合 string sqlstr="select * from tblname where name like ...
随机推荐
- 使用git submodule
git submodule 引用 $ git help submodule $ git submodule add https://github.com/aditya-grover/node2vec. ...
- 微服务项目开发学成在线_Vue.js与Webpack
Vue.js 1.Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.自底向上逐层应用:作为渐进式框架要实现的目标就是方便项目增量开发. 渐进式框架:Progress ...
- SaltStack中状态间关系unless、onlyif、require、require_in、watch、watch_in
1.unless 检查的命令,仅当unless选项指向的命令返回值为false时才执行name定义的命令 cmd.run: {% "] %} - name: 'nohup sh /alida ...
- java常见的 http 请求库比较
java常见的http请求库有httpclient,RestTemplate,OKhttp,更高层次封装的 feign.retrofit 1.HttpClient HttpClient:代码复杂,还得 ...
- 关于Vue.js的认识(第一部分)[转载]
一.关于v-bind 1.初识v-bind (1).加冒号的是 vue 的 v-bind 语法糖(指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用),绑定vue的一个 ...
- Window Redis安装
1.下载redis 下载地址:https://github.com/MicrosoftArchive/redis/releases 2. 安装redis 把下载的Redis-x64-3.2.100 ...
- 最大连续子序列(DP)
Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...
- 金蝶cloud成本核算流程
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第五天】
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- servlet 上传文件
java protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException ...