适用于Hudson/Jenkins的Selenium验证语法
我看了很多教程和Stack Overflow上的帖子,了解到Selenium可以输出XML格式的测试结果,这样Hudson就能以HTML格式来读取和报告这些结果。
不过我不太明白在Python中该用什么语法,才能让结果显示成这样: Testcase_LoginPage.VerifyButton1Present 失败
Testcase_LoginPage.VerifyButton2Present 通过
现在,当我在Hudson中查看结果时,它的格式并没有我想要的那样,而且它还只报告说只运行了一个测试,尽管实际上运行了多个断言测试:
错误追踪(最近的调用在最后): 文件 "D:\Temp\1TestingApps\Selenium\Scripts\SampleScripts\SamCodeSample\test\SOreports.py",第22行,在tearDown中 self.assertEqual([], self.verificationErrors) 断言错误:列表不同:[] != ['注册按钮问题2']
第二个列表包含1个额外的元素。 第一个额外元素 0: 注册按钮问题2
- []
- ['注册按钮问题2']
运行了1个测试,耗时13.610秒
失败(错误=1)
正在生成XML报告...
下面是代码。提前感谢你的帮助!
from selenium import selenium import unittest, xmlrunner, os, re
class 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'))
2 个回答
你现在只定义了一个测试,所以它只能报告一个测试结果。一个测试是一个测试方法,而不是一个断言语句。你可以在一个测试中有多个断言,因为你可能需要检查多个结果,才能确认这个测试是成功的。
所以,要达到你想要的结果,第一步就是把你的第二个断言放到一个新的测试方法里,这样你就能看到两个测试结果了。
我终于找到了一个方法,可以让验证和断言的结果以对我有用的格式报告出来。问题是,当我把Selenium IDE录制的脚本导出为Python RC文件时,测试的默认结构缺少了我需要的很多细节。
我做了以下改变:
- 把Selenium的启动和停止方法放到了Setup和tearDown类里,这样就避免了每次定义新的验证/断言方法时,Selenium都重新启动浏览器。
- 添加了错误描述,这些描述通过inspect.stack()包含了测试用例的名称。
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'))