How To Test Web APIs with Perl and Cucumber

  • Create a cpanfile:
  • Add Mojolicious web-framework to the cpanfile:
  • Install Mojolicious using carton:
  • Generate a Mojolicious lite app:
  • Edit the mock.pl to return a json:
  • Start the server:
  • Create the Test::BDD::Cucumber file structure:
  • Add Cucumber and test modules to cpanfile file:
  • Install Cucumber and test modules:
  • Given is used for a predefined action.
  • When is used for an action done by the user (making a request).
  • Then is used for evaluating the user action.
  • And duplicates the verb above
  • But duplicates the verb above
  • The Background code in the feature file executed the Before block in the step file which adds Mojo::UserAgent object and stores it in S.
  • Given qr/I query for the (\w+) data center/ This step adds the {dc => 'ro' } hash on S. Later this is used to add this ?dc=ro part to the request query.
  • When I make a GET request to the url /json
  1. reads the http verb and the url passed to him from the feature file and uses them to create Mojo::URL->new();
  2. makes a request using the object Mojo::UserAgent object stored in S.
  3. uses the Test2::V0 note() subroutine to document what happened.
  4. stores the request response in S->{result};
  • Then http code is 200
    This step reads the expect status code sent in from the feature file using a regex and compares it with whatever value we have stored in the response object.
  • Call the json scenario:
  • Run all scenarios, except the xml one:

--

--

--

Designing Experiences. Engineering outcomes.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

All about the Category

Google Anthos : First Look

Flip-flops in computer architecture

Open Source CMS vs. Proprietary CMS

Top 10 C Sharp projects on github

🦉 10x curiosity — Issue #83 — The Secret to how Toyota Innovate — Set-Based Concurrent Engineering

Golang: Move The Configuration To An External File For Better Maintainability

How to enable SSL/HTTPS on a Joomla site (detailed guideline)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cognizant Softvision

Cognizant Softvision

Designing Experiences. Engineering outcomes.

More from Medium

How to automate a SOAPUI’s XML request/response testing using SOAPUI’s Maven Library + Java + Junit…

Postman — Understanding Basics of CRUD in API Endpoints Testing

What is Software Testing and What is SonarQube ?!

Automation Testing Tutorial: A Starter’s Guide With Example