Using org mode to manage API test cases

Time:2021-4-21

What are API test cases?

In today’s Internet era, as a programmer of e-commerce platform, he must often deal with HTTP API. A common situation is API testing. Apart from what can be replaced by unit testing, it is often necessary to actually send HTTP requests. These things that are responsible for making HTTP requests may be one linecurlCommand, it could be a.jsFiles can also be an operation of clicking a button in postman, but no matter what the form, they are API test cases. Although the name has the word “use case”, it is often checked by people. Use cases pay more attention to the HTTP request.

Why manage them?

Unlike thousands of ordinary HTTP (or HTTPS) requests generated in the browser every day, API test cases are worth careful management, because:

  1. API test cases are often reused, so they must be persisted. Maybe it is saved as shell script, maybe it is saved as script language source file, maybe it is saved as data file of a certain software;
  2. Test cases need to be written for multiple APIs, so test cases corresponding to different APIs must be distinguished. For example, a service that is responsible for managing business resources (such as a service that manages commodity data and provides restful API) should at least provide the function of adding, deleting, checking and modifying, and then there should be corresponding API test cases for adding, deleting, checking and modifying;
  3. It is necessary to write use cases for API of multiple services, so it is necessary to distinguish test case sets corresponding to different services. For example, since there are commodity services, there are probably order services, coupon services, logistics services, and so on. Each service has the function of adding, deleting, checking, and modifying. The APIs of these different services also need their own test cases;
  4. Different operating environments need to be distinguished. Usually, the local, development, testing and production environments are isolated from each other, and the parameters in one use case cannot be transferred to another environment.

The differences in the three dimensions of API, service, and environment make the number of test cases increase significantly. If you do not manage them, when you want to use them, you either have to write a script again from scratch, or you have to search for a long time to find the required use cases.

Using ORG – Mode management

What is org mode?

org-modeEmacs is an extension of Emacs editor, which enables users to take notes, maintain to-do items, arrange plans, and write documents in a fast and efficient text only way.org-modeThe essence of Emacs lies in its ability to organize the outline and its ability to expand relying on Emacs, both of which make it competent to manage API test cases. One.orgAn example of the file is shown in the figure below

Using org mode to manage API test cases

How to use org mode to manage API test cases?

althoughorg-modeIt provides rich functions, but only manages API test cases. It doesn’t need too many fancy things. It just needs toorg-modeOutline and function oforg-babelThe characteristics are enough.

First, use different.orgFiles distinguish between different environments.

Using org mode to manage API test cases

Then, different levels of headers are used to distinguish different services, resource types, and APIs.

Using org mode to manage API test cases

Then useorg-modeOfCode blockSyntax to write HTTP requests. By requesthttps://httpbin.org/uuidtake as an example

#+BEGIN_SRC restclient
  GET https://httpbin.org/uuid
#+END_SRC

#+BEGIN_SRCand#+END_SRCOpen and end code blocks, respectively,restclientThe code in this code block can be represented by Emacsrestclient-modeTo edit. In the code block,GET https://httpbin.org/uuidRepresents a get method requesthttps://httpbin.org/uuid

InstalledrestclientAfter that, position the cursor over the code block and pressctrl-c 'You can enter a separate buffer to edit the source code

Using org mode to manage API test cases

Finally, if theorg-babel, or even directly on the code blockctrl-c ctrl-cTo make an HTTP request.

Using org mode to manage API test cases

As you can see, the content of the HTTP response will remain in this file.orgFile.

Postscript

I have used other tools to manage API test cases before

  1. When I first worked, I used postman, which was a plug-in of chrome at that time;
  2. Later, out of love for the Firefox browser, I found a plug-in called restclient to replace postman. As time passed, I found that there was a big gap between the two, which could not be replaced after all;
  3. Then we met the problem in Emacsrestclient.elSo it took a long time for the purerestclient-mode(no matchorg-mode);
  4. Later, I began to work with Mac. I began to look for this kind of tool under Mac, and I met some problemsInsomnia. If someone asked me to recommend a GUI tool for HTTP API testing, I would recommend it without hesitation.

Later, I went back to Emacs and used itorg-modeTo manage these API test cases. This is the best way for me at the moment.

At the end of the paper.

Read the original

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]