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

Sun*_*ine 5 python java testng pytest selenium-webdriver

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

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

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

Sun*_*ine 1

将从测试用例中调用此类,如下所示 TestStatus.mark('testName', result, 'message you Want to log') 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()
Run Code Online (Sandbox Code Playgroud)

这是测试用例类中的示例测试用例:

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")
Run Code Online (Sandbox Code Playgroud)