API Automation Testing Guide

Table of contents

Foreword:

What is API testing?

Why is API testing important?

test pyramid

GUI testing

unit test

API testing

API load testing

How to choose an API testing tool

How to test web services

HTTP

About HTTP requests

Request line (HTTP method)

Header

request body

HTTP status code

What are REST APIs?

What is JSON


Foreword:

 API automated testing is a vital part of modern software development. It can help the development team quickly and accurately verify the function and performance of the API, improve software quality and development efficiency.

I believe that automation skills have become standard for the overall skills of senior test engineers. Agile and continuous testing disrupt traditional test automation practices, causing test engineers to rethink how automation is done. Today's automation engineers need to go below the GUI and drill down to the API level to complete software quality protection.

The second change leading to the move to API testing is the Internet of Things. IoT is an everyday object with embedded functionality that allows it to communicate over the web using HTTP or HTTPS to communicate with remote backend services.

Let's share the basic usage guidelines of API testing:

What is API testing?

An Application Programming Interface (API) is a specification that acts as an interface to software components. Most functional testing involves testing user interfaces such as web pages or forms, while API testing involves bypassing the user interface and communicating directly with the service program by calling its API.

API testing allows tests to bypass the GUI and send requests directly to the application's backend or service, and receive the response while validating the response content to ensure it works as expected.

The example above is often referred to as a client/server relationship. A client makes a request by asking for a resource, and the request goes out to find a server that will fulfill the request. The server finds the resource it needs, and sends a response back to the client.

Why is API testing important?

With agile development becoming the standard for most Internet companies, the way we develop software and automate testing has changed dramatically. Before agile development, most of the automation time was done through the graphical user interface (GUI). This is the part that tools like Selenium and UFT/QTP handle.

However, if you've been automating for a while, you know how time-consuming, brittle, and difficult to maintain these types of tests can be. Businesses invest a lot of money to create custom functional GUI test automation frameworks, but it is likely that they will eventually lose confidence in its reliability until people stop investing.

Likewise, GUI tests for user interfaces often take a long time to run. For some agile practices (such as continuous builds), the time it takes to receive feedback from the GUI regression test suite when migrating in new code is not acceptable.

  • API Quick Feedback

In these cases, faster feedback is required. The sooner a bug is found, the better, because developers will immediately know that a code change they made has broken the build and therefore needs to be checked. In a test-driven process, users need large sets of tests to run quickly and frequently, and must be able to integrate them into the development lifecycle.

GUI testing is still very important. It is the only type of testing that truly tests how users experience the application in production. Certain bugs can only be caught by GUI testing. In other words, while vital, GUIs should not be the only type of automation that users focus on, nor should they be the largest part of the total amount of automated testing.

The type of automation that Agile focuses on is the more reliable underlying testing of APIs and less on GUI automation.

test pyramid

GUI testing

GUI testing focuses on testing the application user interface to ensure it functions correctly. GUI tests are at the top of the pyramid and represent only a small fraction of the total number of automated test types that should be created.

unit test

Unit tests form the largest part of the pyramid, forming a solid foundation. Create unit tests to verify individual units of source code, such as methods. By doing this, developers can isolate the smallest testable parts of their code. Unit tests are the easiest to create and yield the most benefits. Since unit tests are usually written in the same language in which the application is written, developers can easily add them to the development process.

API testing

The middle service layer is the "sweet spot" for creating tools like Rest-Assured and Postman.

The point of service testing is to verify that the interactions of many small components can be integrated together without problems. Because API tests bypass the user interface, they tend to be faster and more reliable than GUI tests.

Best of all: Since API tests can be done independently of the UI, they can be created early in the development cycle.

API load testing

Another benefit of API testing is that you can leverage the same functional API automated testing that you use in your performance testing efforts. Many companies use JMeter for load testing, and these test cases are based on API functional testing.

The basic idea is that you're using a tool for performance testing, but before you run eg a load test against your API, you need to make sure it's actually working. Therefore, you want to do functional testing first, and then performance testing can be done with functional test scripts.

Therefore, API test scripts are a great advantage in the performance testing workflow.

How to choose an API testing tool

There are many tools you can use to help you with API test automation.

How to test web services

Test like any other app! In general, the best approach to normal functional testing is the same for Web services (except that, unlike most other applications, Web services do not have a GUI user interface).

Therefore, the functional testing techniques that have been used all the time still apply. Just treat web services as having no business process and write test cases accordingly.

Some good questions to ask when automating web services:

  • Does the service respond with the correct value?
  • Is the behavior expected by end users?
  • How quickly does the service send the response to the user?
  • Can the service handle expected and unexpected user load?
  • Can the service handle exceptions caused by invalid and bad data?
  • Web Services Testing Terminology

For most testers, the biggest hurdle is getting used to the terminology used when talking about Web services.

For example:

  • The XML format
    XML is a method of creating a markup language that you can use to define your own tags. XML allows users to share structured data with many systems, including over the Internet.

  • REST
    REST (Representational Transfer State) is a lightweight option for developing web services using the HTTP protocol.

HTTP

HTTP is a communication protocol for transferring messages over a network. HTTP is also known as a stateless protocol because each request it makes is independent of all previous requests.

Cookies are used to track the state of previous requests for a session. Cookies are files stored on the client with information added from HTTP header information. The information stored in cookies is sent back to the browser when a request is made to a website that the user has already visited. In this way, the website is able to remember the user's previous activity and current status.

  • Understanding HTTP will give us a good foundation for understanding most API testing tool features.

About HTTP requests

An HTTP client request consists of three main parts. they are:

Request line (HTTP method)

Tells the server what type of request is being made. In the example above we made a GET request, but you could use more requests depending on the type of request you need to make. HTTP methods have the following options (the first four are the most common):

GET – retrieve data from the specified source
POST – send new data to the specified source
PUT – update information on the specified source
DELETE – delete data from the specified source
TRACE – ask the agent to declare itself
OPTIONS – ask for information about options available on the server
HEAD – like a GET request, but only sends information about the document
CONNECT – used when the client must use an HTTPS server

Header

Contains additional information to be sent to the server, such as browser, operating system, acceptance and cookie information. The different types of headers are:

General - An optional header that contains information such as the current time
Request - Provides more information to the server about the client Entity
- Contains specific information about the document being sent, such as length and encoding scheme.

request body

Contains data for methods that need it, Get method is empty.

The response returned from the server also contains three parts, just like we saw in the HTTP request:

  • Response line (status code)
  • header information
  • body containing all text in the response

HTTP status code

In our example, the status code is 200, which means everything is fine. The status code will vary depending on what happened with the original request.

The status codes that can be returned from the server are:

1xx – Responses in the 100-199 range indicate that the server is processing the request.
2xx – Responses in the 200-299 range indicate a successful request.
3xx – Responses in the range 300-399 indicate that the request was not executed - further action is required.
4xx – Responses in the range 400-499 indicate that the request is incomplete and more information may be required.
5xx – Responses in the 500-599 range indicate that the server encountered an error.

What are REST APIs?

The fact that REST (Representational Transfer State) is a lightweight option for developing web services using the HTTP protocol makes it simpler and less overhead than web services using the SOAP protocol. When an API follows the REST architecture, it is called a REST API. When a service is designed around the REST standard, it is said to be made "RESTful".

The REST API consists of a large number of resources. This is called the resource model, and it utilizes Uniform Resource Identifiers (URIs). The URI syntax allows you to specify a query that returns the desired information from the REST API. The main elements of a REST system are:

  • A resource is information that a client requests from a host, such as a web page or a database record.
  • A resource identifier is a URI used to name a resource.
  • The representation is when the server sends a response with the resource in the completed format.
  • REST API tests (How to create REST API tests)

What is JSON

JSON stands for JavaScript Object Notation and is designed as a lightweight data interchange format. JSON is definitely becoming more popular and in some cases is replacing XML for API data exchange. The www.json.org website describes how to build JSON on two structures:

"A collection of name/value pairs. In various languages, this is implemented as an object, record, struct, dictionary, hashtable, list of keys, or associative array." An ordered list of values
. In most languages ​​this is done with arrays, vectors, lists or sequences. "

  As someone who has been here, I also hope that everyone will avoid some detours

Here I will share with you some necessities of the way forward in automated testing, hoping to help you.

(WEB automated testing, app automated testing, interface automated testing, continuous integration, automated test development, big factory interview questions, resume templates, etc.)

I believe it can make you better progress!

Click on the small card below

Guess you like

Origin blog.csdn.net/Free355/article/details/131722392