我已经阅读了各种教程和堆栈溢出帖子,并了解Selenium可以以Hudson可以以HTML格式读取/报告XML测试结果的方式输出XML测试结果。
我不明白的是在Python中使用的语法,以获得类似的结果: Testcase_LoginPage.VerifyButton1Present
Testcase_LoginPage.VerifyButton2Present pass
目前,当我在Hudson中深入研究结果时,它们不会像我前面描述的那样以一种有用的方式格式化,而且它还会报告它只运行了一个测试,尽管它运行了多个断言测试:
回溯(最近一次调用):文件"D:\Temp\1TestingApps\Selenium\Scripts\SampleScripts\SamCodeSample\test\SOreports.py",第22行,在tearDown self.assertEqual( [],self.verificationErrors) AssertionError中:列表不同:[] !=‘注册按钮issue2’
第二个列表包含1个额外的元素。第一个额外元素0:注册按钮issue2
Ran 1测试,13.610s
失败(errors=1)
生成XML报告..。
密码在下面。提前感谢您的帮助!
从selenium进口进口unittest,xmlrunner,os,re
类演示(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'))发布于 2011-10-10 19:24:30
我终于找到了如何使验证和断言以一种有用的格式报告给我的需求。问题是,当简单地将Selenium记录脚本导出到Python文件中时,测试的默认结构缺乏我所需要的很多细节。
我更改的内容:-将Selenium start和stop方法放置在安装程序和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'))发布于 2011-10-05 09:22:36
您只定义了一个测试,因此它只能报告一个测试。一个测试是一个测试方法,而不是一个assert语句。在一个测试中可以有多个断言,因为您可能需要断言几个结果才能确认成功的测试结果。
因此,达到所需输出的第一步是将第二个断言放入第二个测试方法中,然后您将看到两个测试结果。
https://stackoverflow.com/questions/7654729
复制相似问题