开发者社区> 问答> 正文

如何在Python测试框架中实现TestNG侦听器?

我正在尝试学习在测试项目上工作的python。有没有一种方法可以在Python测试框架中实现类似TestNG Listener的功能。

侦听器具有OnTestFailure(),OnTestSuccess,OnStart()等方法,这些方法在您要做某些事情时确实很有用。

假设一个测试用例失败,并且您想要执行一些操作,例如截屏。然后,您可以将其写在一个地方,而不必在每个afterTest方法中都写。

展开
收起
祖安文状元 2020-02-22 18:01:55 552 0
1 条回答
写回答
取消 提交回答
  • 该类将从像这样的测试用例中调用,例如TestStatus.mark('testName',result,'要记录的消息')result是一个布尔值

    class TestStatus(unittest.TestCase):
    
        def __init__(self):
            super(TestStatus, self).__init__()
    
        def mark(self, testName, result, resultMessage):
            testName = testName.lower()
            try:
                if result:
                    self.log.info("Verification successful :: " + resultMessage)
                else:
                    # If the test fails,
                    # this calls screenshot method from util class
                    self.util.screenShot("FAIL" + mapKey)
                    self.log.info("Verification failed :: " + resultMessage)
            except:
                self.log.info("### Exception Occurred !!!")
                traceback.print_stack()
    这是测试用例类中的一个示例测试用例:
    
    
    def test_accountSignup(self):
        # Generate a username and password to use in the test case
        userName = self.util.getUniqueName()
        password = self.util.getUniqueName()
        # You can ignore this, this is calling a method
        # signup from the account page (page object model)
        self.accounts.signup(userName, password)
        # This call is also from the page object, 
        # it checks if the sign up was successful
        # it returns a boolean
        result = isSignUpSuccessful()
        # test_status object was created in the setUp method
        #
        self.test_status.mark("test_accountSignup", result,
                                   "Signup was successful")
    
    2020-02-22 18:02:06
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载