Example #1
0
def test_messages():
    """pytest: test set/get different log level messages"""

    for case_info in data:
        case = ReportCase()

        keys = ["debug", "info", "warn"]
        add = {
            "debug": case.add_debug_msg,
            "info": case.add_info_msg,
            "warn": case.add_warn_msg
        }
        get = {
            "debug": case.get_debug,
            "info": case.get_info,
            "warn": case.get_warn
        }

        for key in ["debug", "info", "warn"]:
            if key in case_info.keys():
                for msg in case_info[key]:
                    add[key](msg)
                logs = get[key]()
                assert len(logs) == len(case_info[key])
                assert logs[0] == case_info[key][0]

        if "error" in case_info.keys():
            case.set_error(case_info["error"])
            assert case.get_error() == case_info["error"]
Example #2
0
def test_as_json():
    """pytest: test as_json method"""

    for case_info in data:
        case = ReportCase()
        case.set_name(case_info["name"])
        set_status = {
            c.RESULT_SUCCESS: case.set_status_success,
            c.RESULT_WARNING: case.set_status_warning,
            c.RESULT_FAILURE: case.set_status_failure,
            c.RESULT_SKIPPED: case.set_status_skipped
        }
        add = {
            "debug": case.add_debug_msg,
            "info": case.add_info_msg,
            "warn": case.add_warn_msg
        }

        set_status[case_info["status"]]()

        for key in ["debug", "info", "warn"]:
            if key in case_info.keys():
                for msg in case_info[key]:
                    add[key](msg)

        if "error" in case_info.keys():
            case.set_error(case_info["error"])
Example #3
0
def test_name():
    """pytest: test set/get name"""

    case = ReportCase()
    for case_info in data:
        case.set_name(case_info["name"])
        assert case.get_name() == case_info["name"]
def test_api_response(test_case, kwargs):
    """Makes api request and tests response for compliance

    Args:
        test_case (dict): outlines test case name, target url, and expected
            results. expected results include response code.
        kwargs (dict): web-service specific information, provided on commandline

    Returns:
        ReportCase: reports on whether test case was successful or not, and why
    """

    # instantiate ReportCase
    report_case = ReportCase()
    report_case.set_name(test_case["name"])

    try:
        # setup target url from template
        url = test_case["urlfunc"](test_case, kwargs)

        response = requests.get(url)
        # validate the response status code matches expected according to
        # test case
        if test_case["resp_status"] != response.status_code:
            raise Exception("incorrect status code")

        # parse returned JSON from response, and validate it against the
        # schema
        response_json = response.json()
        sv = SchemaValidator()
        validation_result = sv.validate_instance(response_json)
        if validation_result["status"] == SchemaValidator.FAILURE:
            raise Exception(validation_result["message"])

        report_case.set_status_success()

    except Exception as e:
        # any raised exceptions will set the ReportCase status to failure
        report_case.set_status_failure()
        report_case.set_error(str(e))

    return report_case
Example #5
0
def test_status():
    """pytest: test set/get status"""

    case = ReportCase()
    assert case.get_status() == None
    case.set_status_success()
    assert case.get_status() == c.RESULT_SUCCESS
    case.set_status_warning()
    assert case.get_status() == c.RESULT_WARNING
    case.set_status_failure()
    assert case.get_status() == c.RESULT_FAILURE
    case.set_status_skipped()
    assert case.get_status() == c.RESULT_SKIPPED
Example #6
0
def test_add_case():
    """pytest: test add_case method"""

    for group_info in all_group_info:
        group = ReportGroup()
        for case_info in group_info["cases"]:
            case = ReportCase()
            case.set_name(case_info["name"])
            set_case_status(case, case_info["status"])
            group.add_case(case)
        assert len(group.cases) == len(group_info["cases"])
        assert group.cases[0].get_name() == group_info["cases"][0]["name"]
        assert group.cases[0].get_status() == group_info["cases"][0]["status"]
Example #7
0
    def execute_test(self):

        report_case = ReportCase()
        report_case.set_name(self.get_name())

        try:
            url = self.get_formatted_url()
            params = self.get_url_params()
            report_case.add_debug_msg("URL: " + url)
            report_case.add_debug_msg("PARAMS: " + str(params))
            response = requests.get(url, params=params)
            self.validate_response_code(response)
            self.validate_response_schema(response)
            self.validate_file_contents(response)
            report_case.set_status_success()

        except Exception as e:
            # any raised exceptions will set the ReportCase status to failure
            report_case.set_status_failure()
            report_case.set_error(str(e))

        return report_case