summaryrefslogtreecommitdiff
path: root/test-cli/test/runners/simple.py
blob: c110b1cd0497e5836390ce3aef37d4c6c83d3f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
Simple Test Runner for unittest module

"""

import sys
import unittest
from test.helpers.iseelogger import logObj


class SimpleTestRunner:
    """ A Test Runner that shows results in a simple human-readable format.  

    As example, a common output is:
        This is a test short description : PASS
        This is another test short description : FAIL
        ---------------------------------------------

    """
    __pgObj = None

    def __init__(self, pgObj, stream=sys.stderr, verbosity=0):
        self.stream = stream
        self.verbosity = verbosity
        self.__pgObj = pgObj

    def writeUpdate(self, message):
        self.stream.write(message)

    def run(self, test):
        """ Run the given test case or Test Suite. """
        result = TextTestResult(self, self.__pgObj)
        test(result)
        return result


class TextTestResult(unittest.TestResult):
    # Print in terminal with colors
    PASS = '\033[32mPASS\033[0m\n'
    FAIL = '\033[31mFAIL\033[0m\n'
    ERROR = '\033[31mERROR\033[0m\n'

    __pgObj = None

    def __init__(self, runner, pgObj):
        unittest.TestResult.__init__(self)
        self.runner = runner
        self.result = self.ERROR
        self.__pgObj = pgObj

    def startTest(self, test):
        unittest.TestResult.startTest(self, test)
        self.runner.writeUpdate("%s : " % test.shortDescription())
        # SEND TO DB THE UPDATE THAT WE RUN EACH TEST
        self.__pgObj.run_test(test.params["testidctl"], test.params["testid"])

    def addSuccess(self, test):
        unittest.TestResult.addSuccess(self, test)
        self.result = self.PASS

    def addError(self, test, err):
        unittest.TestResult.addError(self, test, err)
        test.longMessage = err[1]
        self.result = self.ERROR
        print(err)

    def addFailure(self, test, err):
        unittest.TestResult.addFailure(self, test, err)
        test.longMessage = err[1]
        self.result = self.FAIL
        logObj.getlogger().info('{}:{}'.format(test, err))

    def stopTest(self, test):
        try:
            unittest.TestResult.stopTest(self, test)
            # display: print test result
            self.runner.writeUpdate(self.result)
            # save result data
            for result in test.getresults():
                self.__pgObj.upload_result_file(test.params["testidctl"], test.params["testid"],
                                                result["description"], result["filepath"], result["mimetype"])
            # SEND TO DB THE RESULT OF THE TEST
            if self.result == self.PASS:
                status = "TEST_COMPLETE"
                resulttext = test.gettextresult()
                logObj.getlogger().info('{}:{}:{}:{}'.format(test.params["testidctl"], test.params["testid"], status, resulttext))
            else:
                status = "TEST_FAILED"
                resulttext = test.longMessage
                logObj.getlogger().info('{}:{}:{}:{}'.format(test.params["testidctl"], test.params["testid"], status, resulttext))
            if not self.__pgObj.finish_test(test.params["testidctl"], test.params["testid"], status, resulttext):
                self.__pgObj.finish_test(test.params["testidctl"], test.params["testid"], status, "")
        except Exception as E:
            logObj.getlogger().error('Exception: [{}]{}:{}:{}:{}'.format(E, test.params["testidctl"], test.params["testid"], status, resulttext))