unittest框架的注意点

发表于:2018-7-02 13:14

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:经伟    来源:博客园

  
   # coding = utf-8
  import time
  import unittest
  import HTMLTestRunner
  import os
  import sys
  from selenium import webdriver
  class test():
  def add(self, x,y):
  return x+y
  class testClass(unittest.TestCase):
  count=0
  count_=0
  def setUp(self):
  self.count += 1
  print "setup...",self.count
  #self.driver=webdriver.Firefox()
  #time.sleep(3)
  #self.driver.get("https://www.baidu.com")
  #time.sleep(3)
  def verifyEquals(self,exp,act,msg):
  try:
  self.assertEquals(exp,act,msg)
  print 'assertion passed ',msg
  except:
  print 'catch exception here ',msg
  #assert in unittest will just show the msg content when it is failed, you can see its source code, so for pass situation, if also want to show msg content, need to write code yourself.
  def testsearch2(self):
  self.verifyEquals('123','1234','equals')
  self.assertIn("123","1234","assert in ---")
  '''
  time.sleep(30)
  input=self.driver.find_element_by_id('kw')
  search=self.driver.find_element_by_id('su')
  input.send_keys("byebye")
  search.click()
  self.assertIn(self, "123","1234","assert in ---")
  '''
  def testsearch(self):
  input=self.driver.find_element_by_id('kw')
  search=self.driver.find_element_by_id('su')
  input.send_keys("hello")
  search.click()
  print "assertion"
  self.assertTrue(search.is_displayed(),"baidu yixia should display")
  def tearDown(self):
  self.count +=1
  print 'test down...',self.count
  #self.driver.quit()
  #self.driver.close()
  if __name__ == '__main__':
  #unittest.main()
  #unittest.TestCase.assertTrue()
  a=test()
  print a.add(2,4)
  '''
  vp=testClass()
  vp.verifyEquals("123",'234','check equals or not')
  '''
  current_path=os.getcwd()
  print 'current path: ',current_path
  project_path=os.path.dirname(current_path)
  print "project path:",project_path
  testsuite=unittest.TestSuite()
  testsuite.addTest(testClass("testsearch2"))
  #testsuite.addTest(testClass("testsearch"))
  temp=str(time.time())
  filedir=project_path+"//report//"+temp
  os.makedirs(filedir)
  filename="//pyresult.html"
  filepath=filedir+filename
  fp=file(filepath,'wb')
  runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title='report',description='demo')
  runner.run(testsuite)
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号