• Python 断言的使用方法


    Python 断言的使用方法

    自动化测试常用断言的使用方法(python)

      自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断。

    这里介绍几个常用断言的使用方法,可以一定程度上帮助大家对预期结果进行判断。

      这里介绍以下几个断言方法: 
        assertEqual 
        assertNotEqual 
        assertTrue 
        assertFalse 
        assertIsNone 
        assertIsNotNone

    (一)assertEqual 和 assertNotEqual 
      assertEqual:如两个值相等,则pass 
      assertNotEqual:如两个值不相等,则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)后面的“切到超模25tab失败”是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时需要打印的信息,可写可不写。 
      断言assertFalse反着用就可以了。

    (三)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反着用就可以了。

  • 相关阅读:
    Appdelegate 跳转其他页面 获取当前屏幕显示的viewcontroller 获取当前屏幕中present出来的viewcontroller 获取当前导航控制器
    React-Native 环境部署
    关于GCD的那些事
    二,Runtime进行动态添加方法
    一, Runtime 交换方法
    Runtime 概念
    Mac Office安装及破解
    iOS 规范之宏
    规范之UITableViewCell
    Linux 命令
  • 原文地址:https://www.cnblogs.com/wdbgqq/p/10162379.html
Copyright © 2020-2023  润新知