验证的Selenium语法适用于Hudson / jenkins
我已经通过各种教程和Stack Overflow帖子了解Selenium可以以Hudson可以以HTML格式读取/报告它们的方式输出XML测试结果。
我不明白的是在Python中使用的语法,使结果看起来像这样:Testcase_LoginPage.VerifyButton1Present 失败
Testcase_LoginPage.VerifyButton2Present 传递
目前,当我在Hudson中深入研究结果时,他们不会像我上面描述的那样以有用的方式进行格式化,并且它会报告它只运行一次测试,即使它运行了多个断言测试:
回溯(最近通话最后一个):文件 “d: TEMP 1TestingApps 硒脚本 SampleScripts SamCodeSample 测试 SOreports.py” 22行,在拆卸self.assertEqual([],self.verificationErrors)的AssertionError:列表不同:[]!= ['注册按钮issue2']
第二个列表包含1个附加元素。 第一个额外元素0:注册按钮issue2
在13.610s进行1次测试
失败(错误= 1)
生成XML报告...
代码如下。 先谢谢您的帮助!
从硒进口selenium进口unittest,xmlrunner,os,re
类Demo(unittest.TestCase):
def setUp(self):
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4444, "*chrome", "https://workflowy.com/")
    self.selenium.start()
def test_hh(self):
    sel = self.selenium
    sel.open("/accounts/register/")
    try: self.assertEqual("Sign Up FAIL", "Sign Up FAIL","Sign Up button issue1")
    except AssertionError, e: self.verificationErrors.append(str(e))
    try: self.assertEqual("Sign Up FAIL", "Sign Up FAIL1","Sign Up button issue2")
    except AssertionError, e: self.verificationErrors.append(str(e))
def tearDown(self):
    self.selenium.stop()
    self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
#have to format the code this way as SO is complaining about 'bad indent'
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
您只定义了一个测试,因此它只能报告一个测试。 一个测试是一种测试方法,而不是一个断言陈述。 在单个测试中可以有多个断言,因为您可能需要断言几个结果才能确认成功的测试结果。
所以,第一步到你想要的输出是将第二个断言放到第二个测试方法中,然后你应该看到两个测试结果。
  我终于想出了如何使验证和断言得到有用的格式,以满足我的需求。  问题是将Selenium IDE录制的脚本简单导出到Python RC文件时,测试的默认结构缺乏我需要的大量细节。 
  我改变了什么: - 在Setup和tearDown类中放置了Selenium start和stop方法,这些方法阻止了Selenium使用每个新定义的验证/断言方法重新启动浏览器 
   - 通过inspect.stack()添加了包含testcase名称的错误描述 
import inspect, unittest, xmlrunner
from selenium import selenium
class TESTVerifications(unittest.TestCase):
@classmethod
def setUpClass(self):
    self.selenium = selenium("localhost", 4444, "*iexplore", "https://workflowy.com/")
    self.selenium.start() 
    self.selenium.set_timeout("60000")
    print("setUpClass")      
    self.selenium.window_maximize()
    self.selenium.open("/")
def setUp(self):
    self.verificationErrors = []
def test_verification1_error(self):
    try: self.assertEqual("This application is designed", "This application is designedZZZZ",(inspect.stack()[0][3]) +" text missing 'This application is designed'")
    except AssertionError, e: self.verificationErrors.append(str(e))
def test_verification2_error_two_times(self): 
    sel = self.selenium
    ##No such element exception
    try: self.assertEqual("First failure", "First failureZZZZ",(inspect.stack()[0][3]) +" First failure'")
    except AssertionError, e: self.verificationErrors.append(str(e))
    try: self.assertEqual("Second Failure", "Second FailureZZZZ",(inspect.stack()[0][3]) +" Second failure'")
    except AssertionError, e: self.verificationErrors.append(str(e))
def tearDown(self):
    #self.selenium.stop()
    self.assertEqual([], self.verificationErrors,"Results: " + str(self.verificationErrors))
@classmethod    
def tearDownClass(self):
    self.selenium.stop()
    print("tearDownClass")
if __name__ == "__main__":
#    unittest.main()
unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
上一篇: Selenium syntax for Verifications that work with Hudson / jenkins
下一篇: Integration of automated testing in hudson using selenium framework
