Exemple #1
0
def test_mock_get_patcher():

    # Create a patcher object against the endpoint to patch.
    # The requests GET is defined in endpoint_method.py
    mock_get_patcher = patch('lib.endpoint_method.requests.get')

    print("Start patching request GET method call.")
    mock_get = mock_get_patcher.start()

    # Configure mocked GET response as some arbit JSON, this time.
    mock_get.return_value = [{
        'ClusterID': '514-794-6957',
        'Disk_usage': 90,
        'Fatal_errors': 1,
        'Discovery': 0
    }, {
        'ClusterID': '772-370-0117',
        'Disk_usage': 99,
        'Fatal_errors': 3,
        'Discovery': 1
    }, {
        'ClusterID': '176-290-7637',
        'Disk_usage': 10,
        'Fatal_errors': 0,
        'Discovery': 2
    }]

    # Call the function to get mocked response
    response = get_items()

    print("Stop patching the GET method, continue with normal logic flow.")
    mock_get_patcher.stop()

    print("The response is : {}".format(response))
Exemple #2
0
def test_response_ok():
    """
	Check the response as OK for a given endpoint.
	Returns True Or asserts false.
	Input: Valid REST API URI
	"""

    # Send GET request to API given endpoint and store the response.
    response = get_items()

    # Confirm that the request-response cycle completed successfully.
    #assert_true(response.ok)
    if ('None' in response):
        print("Failed calling REST API: {}".format(response))
    else:
        print("TC Passed, Response OK: {}".format(response))
Exemple #3
0
def test_mock_get_ok(mock_get):
    """
    Checks for the same response, as without mock call.
    This examplifies mocking a REST API and sending back mocked
    response, without actual call to API server.
    """

    # Configure the mock to return response/OK status code.
    mock_get.return_value.ok = True

    # Send GET request to API given endpoint and store the response.
    response = get_items()

    # Confirm that the request-response cycle completed successfully.
    if ('None' in response):
        print("Failed calling REST API: {}".format(response))
    else:
        print("TC Passed, Response OK".format(response))
Exemple #4
0
    def test_mock_get_ok(self, mock_get):
        """
        Checks for the same response, as without mock call.
        This examplifies mocking a REST API and sending back mocked
        response, without actual call to API server.
        """
        print("----------------------------------------")
        print(
            "Example of Mocking an API method, like GET. This test does make the"
        )
        print("actual but returns the expected response for TC to Pass.")
        # Configure the mock to return response/OK status code.
        mock_get.return_value.ok = True

        # Send GET request to API given endpoint and store the response.
        response = get_items()

        # Confirm that the request-response cycle completed successfully.
        if ('None' in response):
            print("Failed calling REST API: {}".format(response))
        else:
            print("TC Passed, Response OK".format(response))