コード例 #1
0
    def _assert (self,**assertParam):
        '''
        It will take the arguments :
        expect:'Expected output'
        actual:'Actual output'
        onpass:'******'
        onfail:'Action or string to be triggered or displayed respectively when the assert failed'
        not:'optional argument to specify the negation of the each assertion type'
        operator:'assertion type will be defined by using operator. Like equal , greater, lesser, matches.'

        It will return the assertion result.

        '''

        arguments = self.parse_args(["EXPECT","ACTUAL","ONPASS","ONFAIL","NOT","OPERATOR"],**assertParam)

        result = 0
        valuetype = ''
        operation = "not "+ str(arguments["OPERATOR"]) if arguments['NOT'] and arguments['NOT'] == 1 else arguments["OPERATOR"]
        operators = {'equals':{'STR':'==','NUM':'=='}, 'matches' : '=~', 'greater':'>' ,'lesser':'<'}

        expectMatch = re.match('^\s*[+-]?0(e0)?\s*$', str(arguments["EXPECT"]), re.I+re.M)
        if not ((not expectMatch) and (arguments["EXPECT"]==0)):
            valuetype = 'NUM'
        else :
            if arguments["OPERATOR"] == 'greater' or arguments["OPERATOR"] == 'lesser':
                main.log.error("Numeric comparison on strings is not possibele")
                return main.ERROR

        valuetype = 'STR'
        arguments["ACTUAL"] = str(arguments["ACTUAL"])
        if arguments["OPERATOR"] != 'matches':
            arguments["EXPECT"] = str(arguments["EXPECT"])

        try :
            opcode = operators[str(arguments["OPERATOR"])][valuetype] if arguments["OPERATOR"] == 'equals' else operators[str(arguments["OPERATOR"])]

        except KeyError:
            print "Key Error in assertion"
            return main.FALSE

        if opcode == '=~':
            try:
                assert re.search(str(arguments["EXPECT"]),str(arguments["ACTUAL"]))
                result = main.TRUE
            except AssertionError:
                try :
                    assert re.match(str(arguments["EXPECT"]),str(arguments["ACTUAL"]))
                    result = main.TRUE
                except AssertionError:
                    main.log.error("Assertion Failed")
                    result = main.FALSE
        else :
            try:
                if str(opcode)=="==":
                    main.log.info("Verifying the Expected is equal to the actual or not using assert_equal")
                    if (arguments["EXPECT"] == arguments["ACTUAL"]):
                        result = main.TRUE
                    else :
                        result = main.FALSE
                elif str(opcode) == ">":
                    main.log.info("Verifying the Expected is Greater than the actual or not using assert_greater")
                    if (ast.literal_eval(arguments["EXPECT"]) > ast.literal_eval(arguments["ACTUAL"])) :
                        result = main.TRUE
                    else :
                        result = main.FALSE
                elif str(opcode) == "<":
                    main.log.info("Verifying the Expected is Lesser than the actual or not using assert_lesser")
                    if (ast.literal_eval(arguments["EXPECT"]) < ast.literal_eval(arguments["ACTUAL"])):
                        result = main.TRUE
                    else :
                        result = main.FALSE
            except AssertionError:
                main.log.error("Assertion Failed")
                result = main.FALSE
        result = result if result else 0
        result = not result if arguments["NOT"] and arguments["NOT"] == 1 else result
        resultString = ""
        if result :
            resultString = str(resultString) + "PASS"
            main.log.info(arguments["ONPASS"])
        else :
            resultString = str(resultString) + "FAIL"
            if not isinstance(arguments["ONFAIL"],str):
                eval(str(arguments["ONFAIL"]))
            else :
                main.log.error(arguments["ONFAIL"])
                main.log.report(arguments["ONFAIL"])
                main.onFailMsg = arguments[ 'ONFAIL' ]

        msg = arguments["ON" + str(resultString)]

        if not isinstance(msg,str):
            try:
                eval(str(msg))
            except SyntaxError:
                print "functin definition is not write"

        main.last_result = result
        return result
コード例 #2
0
ファイル: utilities.py プロジェクト: securedataplane/preacher
    def _assert(self, **assertParam):
        '''
        It will take the arguments :
        expect:'Expected output'
        actual:'Actual output'
        onpass:'******'
        onfail:'Action or string to be triggered or displayed respectively when the assert failed'
        not:'optional argument to specify the negation of the each assertion type'
        operator:'assertion type will be defined by using operator. Like equal , greater, lesser, matches.'

        It will return the assertion result.

        '''

        arguments = self.parse_args(
            ["EXPECT", "ACTUAL", "ONPASS", "ONFAIL", "NOT", "OPERATOR"],
            **assertParam)

        result = 0
        valuetype = ''
        operation = "not " + str(arguments["OPERATOR"]) if arguments[
            'NOT'] and arguments['NOT'] == 1 else arguments["OPERATOR"]
        operators = {
            'equals': {
                'STR': '==',
                'NUM': '=='
            },
            'matches': '=~',
            'greater': '>',
            'lesser': '<'
        }

        expectMatch = re.match('^\s*[+-]?0(e0)?\s*$', str(arguments["EXPECT"]),
                               re.I + re.M)
        if not ((not expectMatch) and (arguments["EXPECT"] == 0)):
            valuetype = 'NUM'
        else:
            if arguments["OPERATOR"] == 'greater' or arguments[
                    "OPERATOR"] == 'lesser':
                main.log.error(
                    "Numeric comparison on strings is not possibele")
                return main.ERROR

        valuetype = 'STR'
        arguments["ACTUAL"] = str(arguments["ACTUAL"])
        if arguments["OPERATOR"] != 'matches':
            arguments["EXPECT"] = str(arguments["EXPECT"])

        try:
            opcode = operators[str(
                arguments["OPERATOR"]
            )][valuetype] if arguments["OPERATOR"] == 'equals' else operators[
                str(arguments["OPERATOR"])]

        except KeyError as e:
            print "Key Error in assertion"
            print e
            return main.FALSE

        if opcode == '=~':
            try:
                assert re.search(str(arguments["EXPECT"]),
                                 str(arguments["ACTUAL"]))
                result = main.TRUE
            except AssertionError:
                try:
                    assert re.match(str(arguments["EXPECT"]),
                                    str(arguments["ACTUAL"]))
                    result = main.TRUE
                except AssertionError:
                    main.log.error("Assertion Failed")
                    result = main.FALSE
        else:
            try:
                if str(opcode) == "==":
                    main.log.info(
                        "Verifying the Expected is equal to the actual or not using assert_equal"
                    )
                    if (arguments["EXPECT"] == arguments["ACTUAL"]):
                        result = main.TRUE
                    else:
                        result = main.FALSE
                elif str(opcode) == ">":
                    main.log.info(
                        "Verifying the Expected is Greater than the actual or not using assert_greater"
                    )
                    if (ast.literal_eval(arguments["EXPECT"]) >
                            ast.literal_eval(arguments["ACTUAL"])):
                        result = main.TRUE
                    else:
                        result = main.FALSE
                elif str(opcode) == "<":
                    main.log.info(
                        "Verifying the Expected is Lesser than the actual or not using assert_lesser"
                    )
                    if (ast.literal_eval(arguments["EXPECT"]) <
                            ast.literal_eval(arguments["ACTUAL"])):
                        result = main.TRUE
                    else:
                        result = main.FALSE
            except AssertionError:
                main.log.error("Assertion Failed")
                result = main.FALSE
        result = result if result else 0
        result = not result if arguments["NOT"] and arguments[
            "NOT"] == 1 else result
        resultString = ""
        if result:
            resultString = str(resultString) + "PASS"
            main.log.info(arguments["ONPASS"])
        else:
            resultString = str(resultString) + "FAIL"
            if not isinstance(arguments["ONFAIL"], str):
                eval(str(arguments["ONFAIL"]))
            else:
                main.log.error(arguments["ONFAIL"])
                main.log.report(arguments["ONFAIL"])
                main.onFailMsg = arguments['ONFAIL']

        msg = arguments["ON" + str(resultString)]

        if not isinstance(msg, str):
            try:
                eval(str(msg))
            except SyntaxError as e:
                print "function definition is not right"
                print e

        main.last_result = result
        if main.stepResults[2]:
            main.stepResults[2][-1] = result
            try:
                main.stepResults[3][-1] = arguments['ONFAIL']
            except AttributeError:
                pass
        else:
            main.log.warn("Assertion called before a test step")
        return result
コード例 #3
0
    def _assert(self, **assertParam):
        """
        It will take the arguments :
        expect:'Expected output'
        actual:'Actual output'
        onpass:'******'
        onfail:'Action or string to be triggered or displayed respectively when the assert failed'
        not:'optional argument to specify the negation of the each assertion type'
        operator:'assertion type will be defined by using operator. Like equal , greater, lesser, matches.'

        It will return the assertion result.

        """

        arguments = self.parse_args(["EXPECT", "ACTUAL", "ONPASS", "ONFAIL", "NOT", "OPERATOR"], **assertParam)

        result = 0
        valuetype = ""
        operation = (
            "not " + str(arguments["OPERATOR"]) if arguments["NOT"] and arguments["NOT"] == 1 else arguments["OPERATOR"]
        )
        operators = {"equals": {"STR": "==", "NUM": "=="}, "matches": "=~", "greater": ">", "lesser": "<"}

        expectMatch = re.match("^\s*[+-]?0(e0)?\s*$", str(arguments["EXPECT"]), re.I + re.M)
        if not ((not expectMatch) and (arguments["EXPECT"] == 0)):
            valuetype = "NUM"
        else:
            if arguments["OPERATOR"] == "greater" or arguments["OPERATOR"] == "lesser":
                main.log.error("Numeric comparison on strings is not possibele")
                return main.ERROR

        valuetype = "STR"
        arguments["ACTUAL"] = str(arguments["ACTUAL"])
        if arguments["OPERATOR"] != "matches":
            arguments["EXPECT"] = str(arguments["EXPECT"])

        try:
            opcode = (
                operators[str(arguments["OPERATOR"])][valuetype]
                if arguments["OPERATOR"] == "equals"
                else operators[str(arguments["OPERATOR"])]
            )

        except KeyError as e:
            print "Key Error in assertion"
            print e
            return main.FALSE

        if opcode == "=~":
            try:
                assert re.search(str(arguments["EXPECT"]), str(arguments["ACTUAL"]))
                result = main.TRUE
            except AssertionError:
                try:
                    assert re.match(str(arguments["EXPECT"]), str(arguments["ACTUAL"]))
                    result = main.TRUE
                except AssertionError:
                    main.log.error("Assertion Failed")
                    result = main.FALSE
        else:
            try:
                if str(opcode) == "==":
                    main.log.info("Verifying the Expected is equal to the actual or not using assert_equal")
                    if arguments["EXPECT"] == arguments["ACTUAL"]:
                        result = main.TRUE
                    else:
                        result = main.FALSE
                elif str(opcode) == ">":
                    main.log.info("Verifying the Expected is Greater than the actual or not using assert_greater")
                    if ast.literal_eval(arguments["EXPECT"]) > ast.literal_eval(arguments["ACTUAL"]):
                        result = main.TRUE
                    else:
                        result = main.FALSE
                elif str(opcode) == "<":
                    main.log.info("Verifying the Expected is Lesser than the actual or not using assert_lesser")
                    if ast.literal_eval(arguments["EXPECT"]) < ast.literal_eval(arguments["ACTUAL"]):
                        result = main.TRUE
                    else:
                        result = main.FALSE
            except AssertionError:
                main.log.error("Assertion Failed")
                result = main.FALSE
        result = result if result else 0
        result = not result if arguments["NOT"] and arguments["NOT"] == 1 else result
        resultString = ""
        if result:
            resultString = str(resultString) + "PASS"
            main.log.info(arguments["ONPASS"])
        else:
            resultString = str(resultString) + "FAIL"
            if not isinstance(arguments["ONFAIL"], str):
                eval(str(arguments["ONFAIL"]))
            else:
                main.log.error(arguments["ONFAIL"])
                main.log.report(arguments["ONFAIL"])
                main.onFailMsg = arguments["ONFAIL"]

        msg = arguments["ON" + str(resultString)]

        if not isinstance(msg, str):
            try:
                eval(str(msg))
            except SyntaxError as e:
                print "function definition is not right"
                print e

        main.last_result = result
        if main.stepResults[2]:
            main.stepResults[2][-1] = result
            try:
                main.stepResults[3][-1] = arguments["ONFAIL"]
            except AttributeError:
                pass
        else:
            main.log.warn("Assertion called before a test step")
        return result