# 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),我们将立即处理。