1. General introduction:
DDT-Data Driven Test is a third-party library of Python, which provides the creation of data-driven tests. The online installation is: pip install ddt
@data represents the list data of Yuanzu
@unpack means to decompress the data in the list to each parameter
from ddt import *
Second, the use conditions of DDT
ddt is only suitable for the same test steps in the test case, such as the login page, enter the account-enter the password-click login
Take Baidu login as an example, test case code
The file directory is as follows:
The first is 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])
Object layer 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()
The object layer loginpage.py, the difference from the previous is that a login method is created, which encapsulates the login link, enter account, enter password, click login button
#!/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
Test layer, loginTest.py, demonstrates the introduction of DDT
import unittest from webDDT.pageOBJ.init import * from webDDT.pageOBJ.loginPage import * from ddt import * @ddt class loginDdt (init, login): @data (('', '', u'Please enter your mobile phone / email / Username '), (' 18291875606 ',' ', u'please enter password'), ('18291875606', '123456', u'please enter verification code ')) @unpack def test_all (self, name , pw, error): self.login (name, pw) self.assertEqual (self.getError, error) if __name __ == '__ main__': unittest.main (verbosity = 2)
Separate the list of test data data in the test code
Write a helper.py file, read the value of list
def readlists (): lists = [ [",", u'Please enter your mobile phone / email / username '], [' admin ', ", u'Please enter your password'], [", 'admin', u 'please enter your phone / email / username']] return lists
The modified 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)
Make small but daily progress