API testing with cucumber


626
1 share, 626 points
api testing with cucumber
api testing with cucumber

API testing with cucumber

Performing API testing with cucumber it’s such a simple process , in general to me API testing is much simpler than GUI. Doing API testing you care about what the API is returning back to you, despite the UI that can have such complex UI.

So lets try to create a cucumber project from scratch :
cucumber --init
  create   features
  create   features/step_definitions
  create   features/support
  create   features/support/env.rb
Create the gem file:


source 'https://rubygems.org'
group :test do
  gem 'rest-client'
  gem 'cucumber'
  gem 'json'
end
Run bundle install :

You should see something like this as an output :

bundle install
Fetching gem metadata from https://rubygems.org/........
Fetching version metadata from https://rubygems.org/.
Resolving dependencies...
Installing backports 3.10.0
Using builder 3.2.3
Installing cucumber-tag_expressions 1.0.1
Using gherkin 4.1.3
Installing cucumber-expressions 4.0.4
Using cucumber-wire 0.0.1
Using diff-lcs 1.3
Installing multi_json 1.12.2
Using multi_test 0.1.2
Using unf_ext 0.0.7.4
Installing json 2.1.0 with native extensions
Using mime-types-data 3.2016.0521
Using netrc 0.11.0
Using bundler 1.13.6
Installing cucumber-core 3.0.0
Using unf 0.1.4
Using mime-types 3.1
Installing cucumber 3.0.1
Using domain_name 0.5.20170404
Using http-cookie 1.0.3
Using rest-client 2.0.2
Bundle complete! 3 Gemfile dependencies, 21 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
Require rest client and json into env.rb:
require 'rest-client'
require 'json'

We can actually use a sample api endpoint to write our test from  jsonplaceholder .

Lets write our feature file :
@api
Feature: I can see the comments for a post

  Scenario: Check comments from a post are fetched
    Given I call the api to get the comments from a post
    Then I should get the comments returned
And our step definition :
Given("I call the api to get the comments from a post") do
  @response =  RestClient.get "http://jsonplaceholder.typicode.com/posts/1/comments", content_type: :json, accept: :json
end

Then("I should get the comments returned") do

	parsed_respose =   JSON.parse(@response)
	all = parsed_respose[0].map do |key,value|
		value != nil
	end
	puts all
  if all.all?
		puts "all good"
  else
		raise "empty fields"
  end
end
Run your first cucumber API test:
cucumber
@api
Feature: I can see the comments for a post

  Scenario: Check comments from a post are fetched      # features/sample_project/sample_api_test.feature:4
    Given I call the api to get the comments from a post # features/step_definitions/post_comments_steps.rb:1
    Then I should get the comments returned             # features/step_definitions/post_comments_steps.rb:5
      [true, true, true, true, true]
      all good

1 scenario (1 passed)
2 steps (2 passed)

You can see the entire sample project pushed in gitlab here :

Remember this is just a sample cucumber project more articles will follow on this topic , API testing and we will improve the project structure as we go .

Happy testing!

 


Like it? Share with your friends!

626
1 share, 626 points
Test engineer

0 Comments

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

13 − six =