Fork me on GitHub

Python ErrorList object for use in Webdriver Testing

Here’s a bit of code from a post that was lost when my old site went down, data and all. I don’t recall if the original post was this python version or my original Java version (sorry if that’s what you’re here for, ask in comments and I can find and post that too) It’s an implementation of an ‘assert’ statement that allows for the test to continue on failure, storing the error. At any point I want the test to actually throw an exception, print a report and halt, I just ‘tester.assert_errors()’

# overly simple example.
tester = Tester()
# test something, and store the result to be seen and actually asserted later
tester.assert_true(some_result_boolean, '[FAIL] Aw, shoot. Test failed while checking for some_result')
#
# some more logic here
#
# Now print an error report of what has failed so far, and if there are failures, throw an exception
tester.assert_errors()

Here’s the tester class (with a missing send_email implementation)

from testutils.testutil import send_email

class Tester():
    '''
    A test helper that collects errors, prints reports, etc.
    '''

    def __init__(self):
        '''
        setup some instance vars
        '''
        self.errors = []
        self.is_error_free = True
        self.test_count = 0

    def assert_true(self, test_result, fail_message):
        '''
        Add failures to an error stack
        '''
        self.test_count += 1
        if not test_result:
            self.is_error_free = False
            self.errors.append(fail_message)

            print fail_message

    def print_report(self):
        '''
        Print the status of the error stack
        '''
        if self.is_error_free is True:
            print "[PASS] ** TESTER Test count (" + str(self.test_count) + ") PASS"
        else:
            error_printed_count = 0
            print "[FAIL] ** TESTER Failed Assertions Report - failures/total (" + str(len(self.errors)) + "/" + str(self.test_count) + ")"
            for error in self.errors:
                error_printed_count += 1
                print "\t" + str(error_printed_count) + ") " + str(error)
            print "[FAIL] ** TESTER report end."

    def send_email_on_failure(self, email_address_list):
        '''
        email_address_list: a list of email addresses to be sent a failure report

        '''
        if self.is_error_free:
            return False
        else:
            error_count = 0
            error_report = "[FAIL] ** TESTER Failed Assertions Report - failures/total (" + str(len(self.errors)) + "/" + str(self.test_count) + ")"
            for error in self.errors:
                error_count += 1
                error_report += "\t" + str(error_count) + ") " + str(error)
            error_report += "[FAIL] ** TESTER report end."
            print '[INFO] SENDING FAILURE EMAIL REPORT'
            return send_email(email_address_list, error_report)

    def assert_errors(self):
        '''
        Asserts that the errors stack is empty and prints a report
        '''
        self.print_report()
        assert self.is_error_free

    def cleanup(self):
        '''
        reset the tester object
        '''
        self.errors = []
        self.test_count = 0
        self.is_error_free = True

Comments !

social