1.一般的な紹介:
DDTデータ駆動型テストは、データ駆動型テストの作成を提供するPythonのサードパーティライブラリです。オンラインインストールは次のとおりです:pip install ddt
@dataは元津のリストデータを表します
@unpackは、リスト内のデータを各パラメーターに解凍することを意味します
DDTインポートから*
第二に、DDTの使用条件
ddtは、ログインページなどのテストケースの同じテストステップにのみ適しています。アカウントを入力し、パスワードを入力して、ログインをクリックしてログインします。
Baiduログインを例として、テストケースコード
ファイルのディレクトリは次のとおりです。
最初は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)No 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、前のものとの違いは、ログインメソッドが作成されることです。これは、ログインリンクをカプセル化し、アカウントを入力し、パスワードを入力し、ログインボタンをクリックします。
#!/ 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 _'__________ RAM_P_10_______RAM_P_RAM_SP ) 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の導入を示します
unittestのインポート webDDT.pageOBJ.init *インポートから webDDT.pageOBJ.loginPage *インポートから のインポート* DDTから @ddtの クラスloginDdt(INIT、ログイン): @data(( ''、 ''、U「を入力してくださいお使いの携帯電話/メール/ユーザー名 ')、(' 18291875606 '、' '、u'please your password')、( '18291875606'、 '123456'、 u'please your authentication 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)
テストコード内のテストデータデータのリストを分離する
helper.pyファイルを書き込み、リストの値を読み取ります
def readlists(): リスト= [ ["、"、u '携帯電話/メール/ユーザー名を入力してください']、 ['admin'、 "、u 'パスワードを入力してください']、 ["、 'admin'、u '電話/メール/ユーザー名を入力してください']] リターンリスト
変更されたloginTest.py
@ddt クラスloginDdt(init、login): @data(* helper.readlist()) @unpack def test_all(self、name、pw、error): self.login(name、pw) self.assertEqual(self.getError、エラー) __name __ == '__ main__'の場合:unittest.main (verbosity = 2)
毎日少しずつ進歩する