• 引入DDT


    一、大致介绍:

    DDT-Data Driven Test 是Python的第三方库,提供了创建数据驱动的测试,在线安装为:pip install ddt

    @data 表示元祖的列表数据

    @unpack 表示解压列表里的数据到各个参数

    from ddt import *

    二、DDT的使用条件

    ddt只适合测试用例里的测试步骤一致的,比如登录页,输入账户--输入密码--点击登录

    以百度登录为例,测试用例代码

    文件目录如下:

    首先是webDri.py

    #!/usr/bin/env python
    #-*-coding:utf-8-*-
    from selenium.common.exceptions import NoSuchElementException
    import time
    
    class webDri():
    
        def driver(self,driver):
            self.driver=driver
    
    
        def findelement(self,*loc):
            try:
                return self.driver.find_element(*loc)
            except NoSuchElementException as e:
                print 'error details is %s'%(e.args[0])
    
        def findelements(self,*loc):
            try:
                return self.driver.find_elements(*loc)
            except NoSuchElementException as e:
                print 'error details is %s'%(e.args[0])

    对象层init.py

    #!/usr/bin/env python
    #-*-coding:utf-8-*-
    
    from selenium import webdriver
    import unittest
    
    class init(unittest.TestCase):
    
        def setUp(self):
            self.driver=webdriver.Firefox()
            self.driver.maximize_window()
            self.driver.implicitly_wait(30)
            self.driver.get('http://www.baidu.com/')
    
        def tearDown(self):
            self.driver.quit()

    对象层loginpage.py,跟以前的区别是,创建了一个login方法,封装了点登录链接、输入账户、输入密码、点登录按钮操作

    #!/usr/bin/env python
    #-*-coding:utf-8-*-
    from selenium.webdriver.common.by import By
    from webDDT.basePage.webDri import *
    
    class login(webDri):
    
        loginlink_loc=(By.LINK_TEXT,u'登录')
        username_loc = (By.ID, 'TANGRAM__PSP_10__userName')
        password_loc = (By.ID, 'TANGRAM__PSP_10__password')
        loginButton_loc = (By.ID, 'TANGRAM__PSP_10__submit')
        error_loc = (By.ID, 'TANGRAM__PSP_10__error')
    
        def clickLogin(self):
            self.findelement(*self.loginlink_loc).click()
    
        def typeUsername(self, username):
            self.findelement(*self.username_loc).send_keys(username)
    
        def typePassword(self, password):
            self.findelement(*self.password_loc).send_keys(password)
    
        def clickLoginButton(self):
            self.findelement(*self.loginButton_loc).click()
    
        def login(self, username, password):
            self.clickLogin()
            self.typeUsername(username)
            self.typePassword(password)
            self.clickLoginButton()
    
        @property
        def getError(self):
            return self.findelement(*self.error_loc).text

    测试层,loginTest.py,  演示引入DDT

    import unittest
    from webDDT.pageOBJ.init import *
    from webDDT.pageOBJ.loginPage import *
    from ddt import *
    
    @ddt
    class loginDdt(init,login):
        @data(('','',u'请您输入手机/邮箱/用户名'),('18291875606','',u'请您输入密码'),('18291875606','123456',u'请您输入验证码'))
        @unpack
        def test_all(self,name,pw,error):
            self.login(name,pw)
            self.assertEqual(self.getError,error)
    
    if __name__=='__main__':
        unittest.main(verbosity=2)

    将测试代码中的测试数据data的列表分离出去

    写一个helper.py文件,读取list的值

    def readlists():
        lists=[
            ['','',u'请您输入手机/邮箱/用户名'],
            ['admin','',u'请您输入密码'],
            ['','admin',u'请您输入手机/邮箱/用户名']]
        return lists

    修改后的loginTest.py

    @ddt
    class loginDdt(init,login):
         @data(*helper.readlist())
         @unpack
         def test_all(self,name,pw,error):
             self.login(name,pw)
             self.assertEqual(self.getError,error)
    
    if __name__=='__main__':
         unittest.main(verbosity=2)


  • 相关阅读:
    Linux tcpdump 命令详解与示例
    Linux 查看磁盘IO并找出占用IO读写很高的进程
    Rsync 服务部署与参数详解
    Linux curl 表单登录或提交与cookie使用
    Linux curl 常用示例
    Linux curl 命令详解
    Linux下使用 github+hexo 搭建个人博客07-next主题接入搜索和站点管理
    Linux下使用 github+hexo 搭建个人博客06-next主题接入数据统计
    Linux下使用 github+hexo 搭建个人博客05-next主题接入评论系统
    Linux下使用 github+hexo 搭建个人博客04-next主题优化
  • 原文地址:https://www.cnblogs.com/sunny0/p/7866546.html
Copyright © 2020-2023  润新知