十年测试帮您解析:ddt数据驱动实现自动化测试入门基础应用

一、什么是DDT数据驱动框架
全称:data driver test数据驱动测试框架,可以完美的应用于unittest框架实现数据驱动。ddt使用简介:

1.测试数据为多个字典的list类型

2.测试类前加修饰@ddt.ddt

3.用例前加修饰@ddt.data() 或 @ddt.file_data()

4.运行后测试会自动加载成三个单独的用例

注意:@data装饰符可以把参数当成测试数据,参数可以是单个值、列表、元祖、字典,对于列表和元祖需要使用@unpack装饰符把元祖和列表解析成多个参数。

二、ddt数据驱动框架的安装和使用方式
1.安装方式一:在dos窗口中使用命令:pip install ddt 安装 (建议用这种)
在这里插入图片描述
验证是否安装成功:在dos命令中输入python进入python环境,然后输入import ddt,如果没有报错则说明安装成功!如下图所示:
在这里插入图片描述
2.安装方式二:直接访问: https://pypi.org/project/ddt/#files 下载ddt源码( ddt-1.2.2.tar.gz )。下载后解压,然后在dos命令中切换到setup.py所在目录,执行python setup.py install 进行安装。

3.把ddt导入项目中。

File->Settings->project:项目名->加号->输入ddt->install package

4.ddt使用方式和常用模块

使用方式:

ddt是通过装饰器的形式来调用的

ddt常用模块:

@ddt(申明当前类使用ddt框架),

@data(用于传参),

@unpack(将参数解包,一般针对元组和列表),

@data_file(ddt读取yaml/json文件)
三、@ddt,@data,@unpack的基本使用方法。
1.分别传入单个数字、字符串、元组、列表、集合、字典做一个案例。如:

第一组案例:
@ddt(100)

def testcase1(self,name):

print(name)

@ddt(‘诸葛’)

def testcase1(self,name):

print(name)

@ddt((‘诸葛’,‘司马懿’,‘韩信’))

def testcase1(self,name):

print(name)

@ddt([‘诸葛’,‘司马懿’,‘韩信’])

def testcase1(self,name):

print(name)

@ddt({‘诸葛’,‘魏国’:‘司马懿’,‘楚国’:‘韩信’})

def testcase1(self,name):

print(name)

@ddt({‘蜀国’:‘诸葛’,‘司马懿’,‘韩信’})

def testcase1(self,name):

print(name)
第二组案例:使用@unpack对单个元组和列表解包。
#unpack解析单个元组
@data((‘诸葛’,‘司马’))
@unpack
def test1_data(self,args1,args2):
print(args1,args2)

#unpack解析单个列表
@data([‘诸葛’,‘司马’])
@unpack
def test2_data(self,args1,args2):
print(args1,args2)
2.分别传入多个数字、字符串、元组、列表、集合、字典做一个案例。如:

第一组案例:

代码可自己尝试:注意:查看运行结果,用例运行了多次。

第二组案例:使用@unpack解包:多个元组和列表,如:
#data传入多个元组,unpack对每个元组解析
@data((‘诸葛’,‘司马’),(‘马超’,‘关羽’))
@unpack
def test1_data(self,args1,args2):
print(args1,args2)

#data传入多个列表,unpack对每个列表解析
@data([‘诸葛’,‘司马’],[‘马超’,‘关羽’])
@unpack
def test1_data(self,args1,args2):
print(args1,args2)
注意:

(1) 如果传入:[(‘诸葛’,‘司马’),(‘马超’,‘关羽’)],则@unpack解析后得到(‘诸葛’,‘司马’)和(‘马超’,‘关羽’),需要两个参数接收,用例只执行一次,所以得出@unpack解析最外层的元祖或列表。

(2) 如果传入的是字典列表(list of dict) 分解的参数名必须跟键名一致。

四、请继续关注[ddt02篇]
[ddt02篇]十年测试老鸟帮您解析:ddt结合txt,excel,csv,mysql实现自动化测试数据驱动

如果你此文你也有同感,如果你觉得此文对你有帮助,如果你对软件测试、接口测试、自动化测试、面试经验交流感兴趣
欢迎加入软件测试技术群:642830685,群里发放的免费资料都是十多年测试生涯的精华。还有同行大神一起交流技术哦

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
资源宝贵,请及时下载 Book Description The Selenium WebDriver 3.x Technology is an open source API available to test both Browser and Mobile applications. It is completely platform independent in that tests built for one browser or mobile device, will also work on all other browsers and mobile devices. Selenium supports all major development languages which allow it to be tied directly into the technology used to develop the applications. This guide will provide a step-by-step approach to designing and building a data-driven test framework using Selenium WebDriver, Java, and TestNG. The book starts off by introducing users to the Selenium Page Object Design Patterns and D.R.Y Approaches to Software Development. In doing so, it covers designing and building a Selenium WebDriver framework that supports both Browser and Mobile Devices. It will lead the user through a journey of architecting their own framework with a scalable driver class, Java utility classes, JSON Data Provider, Data-Driven Test Classes, and support for third party tools and plugins. Users will learn how to design and build a Selenium Grid from scratch to allow the framework to scale and support different browsers, mobile devices, versions, and platforms, and how they can leverage third party grids in the Cloud like SauceLabs. Other topics covered include designing abstract base and sub-classes, inheritance, dual-driver support, parallel testing, testing multi-branded applications, best practices for using locators, and data encapsulation. Finally, you will be presented with a sample fully-functional framework to get them up and running with the Selenium WebDriver for browser testing. By the end of the book, you will be able to design your own automation testing framework and perform data-driven testing with Selenium WebDriver. Contents 1: BUILDING A SCALABLE SELENIUM TEST DRIVER CLASS FOR WEB AND MOBILE APPLICATIONS 2: SELENIUM FRAMEWORK UTILITY CLASSES 3: BEST PRACTICES FOR BUILDING SELENIUM PAGE OBJECT CLAS
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值