多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

自动化测试框架思路简单分享

更新时间:2022-04-27 09:06:39 作者:多测师 浏览:255

  现在有许多的自动化测试框架可以使用,如 appium,xUnit,Cucumber 等,但很多时候单纯使用其中一个框架并不是十分好用,而且很多的框架名词,如 BDD,关键字驱动等也会让一些想接触这方面的人感到有点 Hold 不住。其实一个完整、实用的框架是有规律可循的,而且也并不是特别困难。

自动化测试框架思路简单分享

  测试框架分层

  一个完整的测试框架是怎样的?

  例如我们写 appium 的测试用例,如果只是做 Demo 的话我们会直接使用 WebDriver API 来写。这时候一个用例长这样:

  import os

  from time import sleep

  from appium import webdriver

  if __name__ == '__main__':

  # Returns abs path relative to this file and not cwd

  PATH = lambda p: os.path.abspath(

  os.path.join(os.path.dirname(__file__), p)

  )

  # init driver, open session

  desired_caps = {}

  desired_caps['platformName'] = 'Android'

  desired_caps['platformVersion'] = '4.2'

  desired_caps['deviceName'] = 'Android Emulator'

  desired_caps['app'] = PATH(

  '../../../sample-code/apps/ApiDemos/bin/ApiDemos-debug.apk'

  )

  driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

  # execute some action

  els = driver.find_elements_by_android_uiautomator("new UiSelector().clickable(true)")

  # check if result is as expected

  if len(els) != 12:

  print "Case Failed. There should be 12 elements but only {} elements exist".format(len(els))

  else:

  print "Case Passed."

  # end the session

  driver.quit()

  然后用例数目比较多了,每个 case 里面都要启动 appium 太麻烦了,而且测试结果也不够好看,所以我们开始使用一些单元测试框架,使用它们的 setUp,tearDown 或者测试报告。此时用例会长这样:

  import os

  from time import sleep

  import unittest

  from appium import webdriver

  # Returns abs path relative to this file and not cwd

  PATH = lambda p: os.path.abspath(

  os.path.join(os.path.dirname(__file__), p)

  )

  class SimpleAndroidTests(unittest.TestCase):

  def setUp(self):

  desired_caps = {}

  desired_caps['platformName'] = 'Android'

  desired_caps['platformVersion'] = '4.2'

  desired_caps['deviceName'] = 'Android Emulator'

  desired_caps['app'] = PATH(

  '../../../sample-code/apps/ApiDemos/bin/ApiDemos-debug.apk'

  )

  self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

  def tearDown(self):

  # end the session

  self.driver.quit()

  def test_check_clickable_element_count(self):

  els = self.driver.find_elements_by_android_uiautomator("new UiSelector().clickable(true)")

  self.assertEqual(12, len(els))

  if __name__ == '__main__':

  suite = unittest.TestLoader().loadTestsFromTestCase(SimpleAndroidTests)

  unittest.TextTestRunner(verbosity=2).run(suite)

  再后面我们要写更多的用例,单靠做自动化测试的几个人 hold 不住了,需要让一些技术方面不那么强的人来做。为了让他们容易上手而又不至于搞乱我们的框架,我们会把用例再封装,做成表格或者 BDD 这种不会代码的人也能比较容易写用例和读用例的形式。此时用例会长这样 (Cucumber):

  Feature: Simple android test

  Do some sample operations with android application

  Scenario: Check clickable element count

  When I opened the application

  Then 12 clickable buttons should occur

  以上内容为大家分享了自动化测试框架思路,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部