background

* According to the arrangement of the company, Mu Bai will go to the testing department for a two-week rotation. Today is the first day of the rotation. Mu white very excited, because 😍, you understand, the test department has a lot of little sister, cough! Okay no kidding, ** mu White eyes only code! * 😠


Director: Mu bai, I send you a script, you see if you can optimize the script

I: 😱 heart OS (what? Just come a few minutes, the ass did not sit hot, let me optimize the script) but za also at least practiced for half a year, also be regarded as having seen the 🌊 of small wind waves, the small panic in the heart, can’t show in the face, “good good 😁, I immediately see next”

Open the supervisor to the script, leng, there are a few Excel, JSON files, confused face 😵, this this this is the script file? I don’t see the code… 😔, step by step, since the director says to use Postman, let’s try importing Postman

The script below is written by myself, not the actual project of the company. I have simplified the parameters of the interface for demonstration convenience For those of you who have never touched Postman before, let’s take a quick look at some of the features postman offers

  • Collection: a collection in which our interface is written. The add order record above is a collection

  • Folder: If there are a lot of interfaces and we need to classify them, we can create a new folder under the collection and write interfaces in the folder. Since there are few interfaces in the demo, I didn’t build the folder 🤭

  • Request: interface. This is our main concern. We can see that there are two interfaces in the figure, one for getting the item category Id and one for buying the item

    • Params query string parameter list
    • Authorization-request parameters can be set to ensure that the request is allowed by the server. For example, add token, or put Headers into the server
    • Headers request
    • Body The Body of the request, where the parameters of the POST request are written
    • As the name implies, the Script written here will be executed before the request is sent
    • The Tests script, which I like to call a post-script, will be executed after the request is complete and we can authenticate and manipulate the results of the request
    • There are some options in Settings that you can set the behavior of redirection
  • It’s kind of a scope effect, where we can define variables under it, and we can set the Environment that they’re in when the request runs, and it depends on whether the variables can be accessed

  • We can use variables instead of hard coding to improve script reuse


Now that I’ve covered the common features that Postman provides, let me formally demonstrate how to use Postman for automated testing

My goal is to insert 20,000 purchase records into the database

The process of adding a purchase record

  • call/getGoodsIdGets the item category Id
  • call/bugGoodsAdd the purchase record, which is a POST request, and the value in the request body comes from/getGoodsIdReturn result

Using the Tests module, we can obtain the goodsId from the response data and reset the goodsId variable in the environment to it, so that the next request will have the correct value of this parameter as follows:

pm.test("Get product category Id".function () {
    // Convert the response data to JSON format
    let { success, data } = pm.response.json();
    // Check whether the request is successful. If the validation fails, the following statement will not be executed, and the flow of the request will terminate, i.e., the following purchase request will not be sent
    pm.expect(success).to.eql(true);
    // If the request succeeds, the goodsId variable is set and the latest value is added to the request below
    pm.environment.set("goodsId", data.goodsId);
});
Copy the code

The pm.test method can set assertions, and if the callback fails, the execution is aborted, including subsequent requests, which will not be sent. This is important because most of the time, the request is sent with a precondition, as in this example, and then the retrieval of the item category fails, so the request to buy the item should not be sent

Finally, we just want this Collecion to run continuously20000Click the small arrow to the right of CollecionSet the number of times of execution, interval time, click the run button, it is OK ~

Finally, I attached the back-end code to write a simple server with KOA

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
	if(ctx.url === '/getGoodsId') ctx.body = { success: false.data: {goodsId: Math.ceil(Math.random() * 100)}}else next()
});

app.use(async (ctx, next) => {
	console.log(ctx.req)
	if(ctx.url === '/bugGoods') {
		ctx.body = {success: true.data: 'Purchase successful'}}else next();
});

app.use(async (ctx, next) => {
	ctx.body = '404';
});

app.listen(80);
Copy the code

🤦 Finally, let’s export this collecion and see what it isFound a json file, sou ga 😀, open the json file, found that there are I just write post scripts, this explains why the director gave me a script with json file, as for the inside of the excel file, it is similar in function and variable, is to meet the request parameter value transformation, but compared with the variable, There was less flexibility, so my optimization was to replace Excel files with variables and scripts

Well, the article is over here, I hope to help you to see again, 👋