用于Hudson/jenkins的验证的Selenium语法

2024-05-19 22:11:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我看过各种教程和堆栈溢出帖子,了解到Selenium可以以Hudson可以HTML格式读取/报告的方式输出XML测试结果。在

我不明白的是Python中使用的语法,使结果看起来像: 测试用例_验证按钮1登录失败

测试用例_LoginPage.verifybutton2存在通过

目前,当我在Hudson中向下搜索结果时,它们不会像我上面描述的那样以一种有用的方式格式化,而且它会报告它只运行了一个测试,即使它运行了多个assert测试:

回溯(最近一次呼叫): 文件“D:\Temp\1TestingApps\Selenium\Scripts\SampleScripts\SamCodeSample\test\分类报告.py“,第22行,在tearDown 自我评估资格([], 自我验证错误) 断言错误:列表不同:[]!=['注册按钮问题2']

第二个列表包含1个附加元素。 第一个额外元素0: 注册按钮问题2

  • []
  • ['注册按钮问题2']

在13.610秒内进行了1次测试

失败(错误=1)

正在生成XML报表

代码如下。提前感谢您的帮助!在

从硒进口硒 导入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'))

Tags: testselfdef报告selenium错误unittest按钮
2条回答

我终于想出了如何使验证和断言以一种有用的格式报告,以满足我的需要。问题是,当简单地将Selenium IDE录制的脚本导出到Python RC文件中时,测试的默认结构缺少我需要的很多细节。

我改变了: -将Selenium start和stop方法放在Setup和tearDown类中,这会阻止Selenium使用每个新定义的验证/断言方法重新启动浏览器
-添加了错误描述,包括通过检查堆栈()


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'))

您只定义了一个测试,因此它只能报告一个测试。一个测试是一个测试方法,而不是一个assert语句。在一个测试中可以有多个断言,因为为了确认成功的测试结果,可能需要断言多个结果。在

所以,您想要的输出的第一步是将第二个assert放入第二个测试方法中,然后您应该看到两个测试结果。在

相关问题 更多 >