How to implement TestNG Listeners in a Python test environment?

I am trying to learn python to work on a test project. Is there a way to implement TestNG Listeners, for example, functionality in a python test environment.

Listeners have a method like OnTestFailure (), OnTestSuccess, OnStart () and many others that are really useful when you want to do certain things.

Let's say the test case failed, and you want to perform some actions, for example, taking a screenshot. Then you can just write it in one place and not write it in every afterTest method.

+4
source share
2 answers

, TestStatus.mark('testName', 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")
+1

@Sunshine, . .

, "def test_accountSignup (self)"

self.test_status.mark("test_accountSignup", result,
                               "Signup was successful")

, - test_status.mark . : ,

self.accounts.signup(userName, password) 

, . ?

, , ?

def test_accountSignup(self):
  try:
   # Write all the test case specific code here
   self.test_status.mark("test_accountSignup", "Passed",
                               "test_accountSignup passed")
  except Exception as e:
     self.test_status.mark("test_accountSignup", "Failed",
                               "test_accountSignup failed. Exception is: "+e.message)

, , . !

0

Source: https://habr.com/ru/post/1613897/


All Articles