Python断言の使い方

3727 ワード

Python断言の使い方
自動化テストの一般的な断言の使用方法(python)
自動化テストでは要素を探して操作しますが、要素が探しやすい場合は、使用例スクリプトを上手に作成できると信じていますが、操作だけでは足りない可能性があります.予想される結果を判断する必要がある場合もあります.
ここでは、予想される結果をある程度判断するのに役立つ、よく使われる断言の使い方をいくつか紹介します.
ここでは、assertEqual assertNotEqual assertTrue assertFalse assertIsNone assertIsNotNotNone
(一)assertEqualとassertNotEqual assertEqual:2つの値が等しい場合、pass assertNotEqual:2つの値が等しくない場合、passの次に具体的な使用方法を見る
  self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.support.v7.app.ActionBar.e[2]").click()#    25tab
  sleep(3)
  self.assertEqual(self.driver.find_element_by_id('com.boohee.secret:id/tv_title').text,u'  25','    25tab  ')

(1)ここではid(com.boohee.secret:id/tv_title)によってtext値を取得し、予想される「スーパーモード25」と比較してpassを等しくする.等しくなければfail.(2)後の「型抜き25 tabに失敗」は、failの場合に印刷する必要がある情報であり、書くか書かないかである.assertNotEqualは逆に使えばいいと断言します.
(二)assertTrueとassertFalse assertTrue:bool値がTrueであると判断した場合、pass assertFalse:bool値がFalseであると判断した場合、Passは次に具体的な使用方法を見る
  self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.TextView[1]").click()#      
  sleep(2)
  self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.EditText[1]").send_keys("testq1")#        sleep(2)   self.assertTrue(self.find_element_by_id('com.boohee.secret:id/btn_login').is_enabled(),'              ,Fail')

(1)こちらはid(com.boohee.secret:id/btn_login)でそのアクティブ状態を取得し、Trueの場合pass;逆にfailです.(2)後の「未入力パスワード登録ボタンは不可点状態」はfailの場合に印刷する必要がある情報であり、書き込み可能か書き込み不可である.アサートFalseは逆に使えばいいと言い切る.
(3)assertIsNoneとassertIsNotNone assertIsNone:存在しない場合、pass assertIsNotNone:存在する場合、passは以下の具体的な使用方法を見る
  self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.TextView[1]").click()#      
  sleep(2)
  self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.EditText[1]").send_keys("testq1")#        sleep(2)   self.driver.find_element_by_xpath("//android.widget.LinearLayout[2]/android.widget.EditText[1]").send_keys("boohee")#       sleep(2)   self.driver.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.Button[1]").click()#         sleep(10)   self.assertIsNotNone(self.driver.find_element_by_id('com.boohee.secret:id/tv_edit_profile'),'       ,    ,Fail')

(1)こちらはid(com.boohee.secret:id/tv_edit_profile)の要素が存在するかどうかを探し、存在する場合pass;存在しない場合fail.(2)後の「資料編集ボタンなし、ログイン失敗、Fail」はfailの場合に印刷する必要がある情報であり、書くか書かないかである.assertIsNoneは逆に使えばいいと断言します.
転載先:https://www.cnblogs.com/wdbgqq/p/10162379.html