软件测试技术之编写 Web 测试用例
白羽 2018-09-13 来源 :网络 阅读 1382 评论 0

摘要:本文将带你了解软件测试技术之编写 Web 测试用例,希望本文对大家学测试技术有所帮助。

        本文将带你了解软件测试技术之编写 Web 测试用例,希望本文对大家学测试技术有所帮助。


我在另一篇文章中花了比较长的篇幅介绍了unittest 单元测试框架。这篇文章就介绍如何应用 unittest 编写 Web 测试用例。
  还是以我之前写过的一个脚本为例进行更改:
  test_baidu.py
  from selenium import webdriver
  import unittest
  import time
  class MyTest(unittest.TestCase):
  def setUp(self):
  self.driver = webdriver.Firefox()
  self.driver.maximize_window()
  self.driver.implicitly_wait(5)
  self.base_url = "//www.baidu.com"
  def test_baidu(self):
  driver = self.driver
  driver.get(self.base_url + "/")
  driver.find_element_by_id("kw").clear()
  driver.find_element_by_id("kw").send_keys("unittest")
  driver.find_element_by_id("su").click()
  time.sleep(3)
  title = driver.title
  self.assertEqual(title, "unittest_百度搜索")
  def tearDown(self):
  self.driver.quit()
  if __name__ == '__main__':
  unittest.main()
     test_youdao.py
  from selenium import webdriver
  import unittest
  import time
  class MyTest(unittest.TestCase):
  def setUp(self):
  self.driver = webdriver.Firefox()
  self.driver.maximize_window()
  self.driver.implicitly_wait(10)
  self.base_url = "//www.youdao.com"
  def test_youdao(self):
  driver = self.driver
  driver.get(self.base_url+"/")
  driver.find_element_by_id("translateContent").clear()
  driver.find_element_by_id("translateContent").send_keys("unittest")
  driver.find_element_by_xpath("/html/body/div[5]/div/form/button").click()
  time.sleep(3)
  title = driver.title
  self.assertEqual(title, "unittest - 有道搜索")
  def tearDown(self):
  # self.driver.close()
  pass
  if __name__ == '__main__':
  unittest.main()
  上面两个脚本,第一个可以通过,第二个则会失败。失败的原因很简单,就是断言出错。现在我们组织这两个用例,使得可以一次性执行。
  假设上面两个脚本文件都在一个叫做 Webdriver 的文件夹里,那么我们在该文件夹内创建另一个文件 test.py。
  test.py
  import unittest
  test_dir = './'
  discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')
  if __name__ == '__main__':
  # 执行测试
  runner = unittest.TextTestRunner()
  runner.run(discover)
  test.py执行结果如下:
  /usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/guxuecheng/Desktop/Webdriver/test.py
  .F
  ======================================================================
  FAIL: test_youdao (test_youdao.MyTest)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
  File "/Users/guxuecheng/Desktop/Webdriver/test_youdao.py", line 22, in test_youdao
  self.assertEqual(title, "unittest - 有道搜索")
  AssertionError: '【unittest】什么意思_英语unittest的翻译_音标_读音_用法_例句_在线翻译_有道词典' != 'unittest - 有道搜索'
  - 【unittest】什么意思_英语unittest的翻译_音标_读音_用法_例句_在线翻译_有道词典
  + unittest - 有道搜索
  ----------------------------------------------------------------------
  Ran 2 tests in 28.429s
  FAILED (failures=1)
  Process finished with exit code 0

  从执行日志中我们可以看到,一共执行了2个用例(Ran 2 tests in 28.429s),其中有1个失败(FAILED (failures=1))。也可以看出是哪一个脚本失败以及失败原因。    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标软件测试之测试技术频道!

本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程