Automated API integration testing


633
1 share, 633 points
api integration testing
api integration testing

Automated API integration testing

Automated API integration testing can save you guys for a lot of extra UI testing. Of course the UI can still brake from style changes but it can’t brake because of the data integration.

This can be easily done running a sequence of API requests, where each request depends on the output of the previous requests.

As an example:

  1. Create an API request to create an user
  2. New API to get user details for the new created user
  3. New API request to update your user details
  4. New API request to change user permissions
  5. New API request to check your permission call if user details permissions were updated
  6. New api request to delete your user

This might look a bit overwhelming but it not imagine , you have an api app called api-com-users and the second one api-com-permissions.

With this integration tests you tested the integration between those 2 and the only thing left is to test the UI separated.

You can easily get started with using cucumber with api testing and write your integration tests. If you are in a continuous integration environment you really need to have integration tests that are running when something gets merged into your master branch otherwise you’ll have to run your UI tests all the time and that can be a bit time consuming when we are talking about thousands of tests.

  Scenario: I can delete a new created user
    Given I call create user api with valid data
      | forename  | Miss |
      | job_title | Qa   |
      | surname   | Test |
    And I delete the user id new_created
    Then I get a 202 response

Api step definitions:

When(/^I call create user api with (.*) data$/) do |datatype, table|
  params = $api_pro2.api_com_users.new_user_data(datatype, table)
  $response = MainInstance.api_post_param("#{MainInstance.api_com_users}/v1/user", params)
end

Where new user data is :


      def new_user_data(datatype, table)
        $generated_email = "#{MainInstance.generate_random_string}@zpg.co.uk"
        new_user_data = table.rows_hash
        case datatype
        when 'valid'
          new_user_data['email'] = $generated_email
        else
          MainInstance.pass
        end
        new_user_data
      end

API request to delete just created user:

  user_id = $response['new'][0]['user_id']
      @response = MainInstance.api_delete("#{MainInstance.api_com_users}/v1/users/#{user_id}")

Assert your response code:

 expect(@response.code.to_s).to eq(expected_response_status.to_s)

This is the most simple example I can give to you , but this test should still go one , to fetch user , expect 404 as is delete , check delete user permissions and expect the right response code back.

For a most complex example of api integration testing check Graham Cox’s article here.

Happy testing!


Like it? Share with your friends!

633
1 share, 633 points
Test engineer

0 Comments

Your email address will not be published. Required fields are marked *

10 + 2 =