Showing posts with label Test Tool. Show all posts
Showing posts with label Test Tool. Show all posts

Jan 26, 2021

API Testing 'A Beginners View': Newman API Testing

Hi Everyone!!! Let's check out some details about NEWMAN 😊

Newman gives the capability of running Postman collections through the command line. It basically helps you automate your Postman tests with ease.

Got some insight on this from my daily worklife. So why wait Let's get started! 


Basic Steps of NEWMAN Kickstart:

  1. Step 1: Install NodeJS
  2. Step 2: Install Postman's Newman
  3. Step3: Create a Collection and Running Tests Scripts
  4. Step4: Exporting Postman Collection and its associated Environment file if applicable
  5. Step5: Running the Collection in Newman

Step1: Install NodeJS


Installing NodeJS is pretty straight forward, visit their download page here, download your platform version to install.

Note: Kindly make sure to install the latest higher version.


After installing, go to your Terminal and enter the command below to check if everything is fine to go.

  • node -v 
  • npm -v

You should see an output similar to the one below:

 

Step2: Install Postman's Newman :


Now that NodeJS is set up, we need to install "Newman" and some NodeJS modules that will be useful for our test report generations.


Run the below command:

  • npm install newman request rimraf newman-reporter-html newman-reporter-htmlextra --save --force
After installation you will see similar output:

Step3: Create a Collection and Running Tests Scripts

In Postman select "New" >> "Collection". Give a name to the collection. I’ll be using a sample collection in my local which has Test Scripts designed already just for the purpose of the explanation.

Sample Collection with Test Scripts:

 

Step4: Exporting Postman Collection and its associated Environment file if applicable


Under the ‘View more actions' option of the collection, select ‘Export’.


Save the Collection to your local directory. The collection should be saved as a .postman_collection json file.

Note: To export the environment variables, Go to the ‘Manage Environment’ section from the top right corner and click on Download, environment file will be downloaded as .postman_environment json file


Step5: Running the Collection in Newman

Open the terminal and navigate to where the Postman Collection and associated environment json file is saved. To execute the tests run the below command:

newman run Jenkins-Testing.postman_collection.json -e Reltio-MDM-UCI.postman_environment.json --export-environment Reltio-MDM-UCI.postman_environment.json

Similar output will be prompted:


Collection executed successfully through the command line.

Now somebody might think why did I pass the environment file twice in a different way. Let me explain it why? 

The Collection that I used above for my explanation needs an access token to be generated freshly for each run. Could we not just attach a token to the tests, export the collection and put them into our CI? No we can’t. That would work for a while but the tests would fail once the access token expires. So any access token I attach to my Postman collection to run in CI is eventually going to be invalid.


We need a way of automatically generating a fresh access token and passing it into our Postman tests. In CI terms, we need to be able to generate an access token on every run of tests. In order to achieve this, I have added an environment variable to store the access token that gets generated by a request of Authentication. So now when we send a request for an access token we can now grab that access token from the response and attach it to any future responses we need. But wait a min by POSTMAN the scenario will work fine but NEWMAN does not update the environment variables file during a run of tests. So to save the new values of the environment variables after a collection run, simply add this argument when running the collection that updates the variables: --export-environment OurEnvironmentFile

In our case :

newman run Jenkins-Testing.postman_collection.json -e Reltio-MDM-UCI.postman_environment.json --export-environment Reltio-MDM-UCI.postman_environment.json

That’s it and now we are familiar with the basics of NEWMAN.

Hope the article was useful! Happy Learning! 😊


Oct 14, 2020

API Testing 'A Beginners View': "Mockaroo" - A Cool Test Data Generator Tool

Recently, while working on a project I came across a need to generate data in bulk for testing purposes. So, it was then my team lead suggested the usage of the ‘Mockaroo tool’, which proved to be a hassle-free way to generate test data as and when required using REST API callouts. 

In this post, you find details on:
  • How to create simple bulk mockup test data?
  • How to get the mockup data in postman to use as per your need?
Prior to the start:
You should create an account in ‘Mockaroo’. (https://mockaroo.com/users/sign_in)
 
Here we go for the steps:
  1. Using the link shown above, register on ‘Mockaroo’. Once done, an API key will get generated for your account. This key will be used in all the API calls made to mockaroo. To know the API key, go to the Account page after logging in to Mockaroo.

  2. Once a ‘mockaroo’ account has been set up, create a schema for the entity under the ‘Schemas’ tab. Mockaroo has its own set of data types which is needed while creating fields for the schema. (We can import schema details such as the name of fields and data types using CSV headers or JSON to prevent human error) NOTE: For field names in the schema, we must use the same name as that of the API field name.
  3. On the launch of new schema creation, you will see some auto-populated fields provided by mockaroo as a sample, follow the same manner and start entering your fields as per need using the Mockaroo available datatypes. On the launch ‘Schema Creation Page’ look is as below:

  4. Available Data Types:

  5. Once the necessary fields are set up as per the API , save the schema.
  6. After save of schema mockaroo will generate a ‘curl url’, which will be the endpoint that needs to be used in your collection the postman as needed.

  7. Copy the url and use the GET method to call the test data generator url in your postman collection. 

  8. In order to use the test data fetched, set a variable either globally or environment to hold the test data, as below;

  9. Call the variable in the next POST request ‘Body’ for the endpoint you want the test data to be used and hit the send your data would be created in the requested endpoint;

Few known important things about MOCKAROO:
  • 3 subscriptions of mockaroo are available:
    • Free (200 API calls/day)
    • Silver (no limit to number of API Calls, 1 million records/day)
    • Gold (no limit to number of API calls, 10 million records/day)
  • Data Types used in mockaroo schemas are intrinsic to mockaroo and any other data type cannot be used.