一、创建yaml文件
1. 安装yaml模块
 pip install pyyaml
2. 新建yaml文件
 右键任意文件夹-->New-->File,输入文件名并以.yaml或.yml结尾

二、yaml文件格式

1. 列表
 列表中的所有元素缩进相同,且均已“- ”(一个横杠和一个空格)开头

- 苹果
- 橙子
- 香蕉

代码读取yaml文件的数据

import yaml

f = open(r'C:\Users\yitai\Desktop\python相关\综评_json\test1.yaml',encoding='utf-8')
res = yaml.load(f)
print(type(res))
print(res)

最后打印结果为:
 
2. 字典
 字典中同级别的元素缩进相同,由简单的键 : 值的形式组成(必须得是英文冒号,且冒号后面要跟一个空格)

name : 张三
age : 25
phone : 18700000000

同上代码读取yaml文件的数据,最后打印结果为:

3. 复合结构

-
cookieType : 1
dataType: 0
url : user/login
method : post
detail : 登录
data :
username: 张三
password : 123456
check :
- 操作成功

最后打印结果为:

三、测试代码

import unittest,requests
import ddt
from urllib import parse
from conf.setting import BASE_URL,COOKIE
@ddt.ddt
class My_case(unittest.TestCase):
base_url = BASE_URL
@ddt.file_data(r'C:\Users\****\Desktop\****\case_data\test1.yaml')#ddt帮你读文件,获取文件内容,循环调用函数
def test_request(self,**kwargs):
detail = kwargs.get('detail','没写用例描述')
self._testMethodDoc = detail #动态的用例描述
url = kwargs.get('url')#url
url = parse.urljoin(self.base_url,url)#拼接好url
method = kwargs.get('method','get')#请求方式
data = kwargs.get('data',{}) #请求参数
header = kwargs.get('header',{})#请求头
cookieType = kwargs.get('cookieType')
check = kwargs.get('check')
dataType = kwargs.get('dataType')
method = method.lower() #便于处理 if cookieType:
cookie = COOKIE
else:
cookie={}
try:
if method=='post':
if dataType:
res = requests.post(url,json=data,cookies=cookie,headers=header).text
else:
res = requests.post(url,data=data,cookies=cookie,headers=header).text
else:
res = requests.get(url,params=data,cookies=cookie,headers=header).text
except Exception as e:
print('接口请求出错')
res = e
# 列表
for c in check:
self.assertIn(c,res,msg='预计结果不符,预期结果:'+c +','+ '实际结果:' +res)
if __name__ == '__main__':
unittest.main()
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(My_case))
report_html = BeautifulReport.BeautifulReport(suite)
# report_html.report(filename='test', description='用例描述')
fmt = '{date}_TestReport.html'.format(date=time.strftime('%Y%m%d%H%M%S'))
#生成报告的文件名格式20180329190544_TestReport.html
report_html.report(filename=fmt, description='用例描述')

四、yaml文件中参数化
下面以创建体育考试为例

import datetime
def get_cur_time(dic: dict): #取系统当前日期
if 'cur_time' in dic.values():
for k in dic:
if dic.get(k) == 'cur_time':
dic[k] = '%s' % datetime.date.today()
return dic
import unittest,requests
@ddt.ddt
class Physical(unittest.TestCase):
base_url = BASE_URL
@ddt.file_data(r'C:\Users\yitai\Desktop\python相关\综评_json\case_data\setxqjy.yaml')#ddt帮你读文件,获取文件内容,循环调用函数
def test_request(self,**kwargs):
detail = kwargs.get('detail','没写用例描述')
self._testMethodDoc = detail #动态的用例描述
url = kwargs.get('url')#url
url = parse.urljoin(self.base_url,url)#拼接好url
method = kwargs.get('method','get')#请求方式
data = kwargs.get('data',{}) #请求参数
data = get_cur_time(data) #将yaml文件中的cur_time字段替换为系统当前时间
header = kwargs.get('header',{})#请求头
cookieType = kwargs.get('cookieType')
check = kwargs.get('check')
dataType = kwargs.get('dataType')
method = method.lower() #便于处理 if cookieType:
cookie = COOKIE
else:
cookie={}
try:
if method=='post':
if dataType:
res = requests.post(url,json=data,cookies=cookie,headers=header).text
else:
res = requests.post(url,data=data,cookies=cookie,headers=header).text
else:
res = requests.get(url,params=data,cookies=cookie,headers=header).text
except Exception as e:
print('接口请求出错')
res = e
# 列表
for c in check:
self.assertIn(c,res,msg='预计结果不符,预期结果:'+c +','+ '实际结果:' +res) if __name__ == '__main__':
unittest.main()
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Physical))
report_html = BeautifulReport.BeautifulReport(suite)
# report_html.report(filename='test', description='用例描述')
fmt = '{date}_TestReport.html'.format(date=time.strftime('%Y%m%d%H%M%S'))
#生成报告的文件名格式20180329190544_TestReport.html
report_html.report(filename=fmt, description='用例描述')

python----数据驱动@ddt.file_data结合yaml文件的使用的更多相关文章

  1. Python @ddt.file_data() 为.yml 文件实例

    一,创建login.yml 文件(以登录接口为例) 1,创建 login.yml 文件,内容如下图: 打印login.yml 文件,代码及显示效果如下: 代码: import yaml,jsonf = ...

  2. Python数据驱动ddt

    import ddtimport unittest """ddt模块包含了一个类的装饰器ddt和两个方法的装饰器: data:包含多个你想要传给测试用例的参数: file ...

  3. Python数据驱动DDT的应用

    在开始之前,我们先来明确一下什么是数据驱动,在百度百科中数据驱动的解释是:数据驱动测试,即黑盒测试(Black-box Testing),又称为功能测试,是把测试对象看作一个黑盒子.利用黑盒测试法进行 ...

  4. python 数据驱动ddt使用,需要调用下面的代码,请挨个方法调试,把不用的注释掉

    #!/usr/bin/env/python # -*- coding: utf-8 -*- # @Time : 2018/12/15 15:27 # @Author : ChenAdong # @Em ...

  5. Python基础笔记1-Python读写yaml文件(使用PyYAML库)

    最近在搭建自动化测试项目过程中经常遇到yaml文件的读写,为了方便后续使用,决定记下笔记. 一,YAML 简介 YAML,Yet Another Markup Language的简写,通常用来编写项目 ...

  6. Python 数据驱动ddt 使用

    准备工作: pip install ddt 知识点: 一,数据驱动和代码驱动: 数据驱动的意思是  根据你提供的数据来测试的  比如 ATP框架 需要excel里面的测试用例 代码驱动是必须得写代码  ...

  7. Python - 通过PyYaml库操作YAML文件

    PyYaml简单介绍 Python的PyYAML模块是Python的YAML解析器和生成器 它有个版本分水岭,就是5.1 读取YAML5.1之前的读取方法 def read_yaml(self, pa ...

  8. python 数据驱动(ddt)

    DDT包含类的装饰器ddt和两个方法装饰器data(直接输入测试数据),file_data(可以从json或者yaml中获取测试数据) 实例代码: import ddt import unittest ...

  9. unittest---unittest数据驱动(ddt)

    在做测试的时候,有些地方无论是接口还是UI只是参数数据的输入不一样,操作过程是一样的.重复去写操作过程会增加代码量,我们可以通过参数化的方式解决这个问题,也叫做数据驱动,我们通过python做参数化的 ...

随机推荐

  1. Oracle树查询及相关函数

    Oracle树查询的最重要的就是select...start with... connect by ...prior 语法了.依托于该语法,我们可以将一个表形结构的中以树的顺序列出来.在下面列述了Or ...

  2. Keepalived配置文件详解

    global_defs { router_id LVS_$prio #节点唯一标识,通常为hostname } local_address_group laddr_g1 { ${lvs_node} # ...

  3. js 对url进行某个参数的删除,并返回url

    两种情况 1对当前页面的url进行操作 function funcUrlDel(name){ var loca = window.location; var baseUrl = loca.origin ...

  4. 【记录】Linux环境安装mysql8.0

    话说mysql8.0版本比5.7版本要快2倍以上,这么看宣传怎么能不装8.0呢,但是新版本和旧版本有不少不同导致若使用以前的一些安装方法会导致安到一半就由于各种找不到文件卡住. 尝试了不少次,只有使用 ...

  5. MyRedis

    目录 其他 其他 Redis面试题集

  6. bzoj4665 小w的喜糖(dp+容斥)

    4665: 小w的喜糖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 222  Solved: 130[Submit][Status][Discuss ...

  7. 论文笔记:Progressive Neural Architecture Search

    Progressive Neural Architecture Search 2019-03-18 20:28:13 Paper:http://openaccess.thecvf.com/conten ...

  8. Javascript 中的数据类型判断

    (迁移自旧博客2017 09 25) typeof 我们常使用typeof来判断数据类型,在常规场景中足以应付数据类型判断的需要: var obj = { name: 'zhangxiang' }; ...

  9. 关于djangorestframework相关源码分析

    CBV APIView Request 局部全局钩子 认证组件 权限组件 频率组件 分页器组件

  10. ssh连接所生成的known_hosts出现的问题

    问题现场及解析 用OpenSSH的人都知ssh会把你每个你访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts.当下次访问相同计算机时,OpenSSH会核对公钥.如果 ...