首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于与Hudson / jenkins一起工作的验证的Selenium语法

用于与Hudson / jenkins一起工作的验证的Selenium语法
EN

Stack Overflow用户
提问于 2011-10-04 22:26:26
回答 2查看 1.8K关注 0票数 1

我已经阅读了各种教程和堆栈溢出帖子,并了解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

  • []
  • 'Sign Up按钮issue2'

Ran 1测试,13.610s

失败(errors=1)

生成XML报告..。

密码在下面。提前感谢您的帮助!

从selenium进口进口unittest,xmlrunner,os,re

类演示(unittest.TestCase):

代码语言:javascript
复制
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'))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-10 19:24:30

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

我更改的内容:-将Selenium start和stop方法放置在安装程序和tearDown类中,从而防止Selenium使用每个新定义的验证/断言方法重新启动浏览器

inspect.stack()

  • 添加了包含测试用例名称的错误描述。

代码语言:javascript
复制
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'))
票数 0
EN

Stack Overflow用户

发布于 2011-10-05 09:22:36

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

因此,达到所需输出的第一步是将第二个断言放入第二个测试方法中,然后您将看到两个测试结果。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7654729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档