You don't know the simple start of interface testing


The current trend is that when it comes to testing, it must be "interface". In fact, the interface is not mysterious. There is no "interface" in today's applications. Let me talk about some superficial understanding of the interface from this article.

A simple interface (demo.php)

    // File name demo.php
    // Tell the browser to return as json type
    header('Content-Type:application/json; charset=utf-8');
    // Organizational data
    $data = array(
        'code' => 200,
        'msg' => 'Request succeeded',
        'result' => array(
            'key1' => 'value1',
            'key2' => 'value2'
        )
    );
    // PHP array to json
    $rst = json_encode($data,JSON_UNESCAPED_UNICODE);
    // Print to browser
    echo $rst;

Put the file in the specified directory of the server and access it through the browser to obtain the following results:

{
    "code": 200,
    "msg": "Request succeeded",
    "result": {
        "key1": "value1",
        "key2": "value2"
    }
}

This is a relatively simple interface.

What are we going to test

Using the above interface example, we want to test nothing more than (other uses, such as http_code):

json Object's key“ code",Is it equal to 200,
json Object's key“ msg",Is it equal to "Request succeeded",
result Object's key“ key1",Is it equal to "value1"
.....

The interface data will change according to certain rules during work, so this "rule" is the focus of our test. Let's look at the interface (demo1.php) as follows.

    // Tell the browser to return as json type
    header('Content-Type:application/json; charset=utf-8');

    // Gets a random number from 0 to 2
    $index = rand(0,2);

    $msgs = array('Request succeeded','request was aborted','Other errors');
    // Organizational data
    $data = array(
        'code' => 200,
        'msg' => $msgs[$index],
        'result' => array(
            'key1' => 'value1',
            'key2' => 'value2'
        )

    );
    // PHP array to json
    $rst = json_encode($data,JSON_UNESCAPED_UNICODE);

    // Print results
    echo $rst;

After multiple visits in the browser, we may get the following results:

{
    "code": 200,
    "msg": "Request succeeded",
    "result": {
        "key1": "value1",
        "key2": "value2"
    }
}
{
    "code": 200,
    "msg": "Request succeeded",
    "result": {
        "key1": "value1",
        "key2": "value2"
    }
}
{
    "code": 200,
    "msg": "request was aborted",
    "result": {
        "key1": "value1",
        "key2": "value2"
    }
}
{
    "code": 200,
    "msg": "Other errors",
    "result": {
        "key1": "value1",
        "key2": "value2"
    }
}

In this interface, we want to test the rule of "random". The test is whether the msg returned to the browser changes randomly. Then we want to test:

json Object's key“ msg",Is it "request succeeded","request was aborted","Other errors" One of them,
json Object's key“ msg",Is it "request succeeded","request was aborted","Other errors" One of them,
json Object's key“ msg",Is it "request succeeded","request was aborted","Other errors" One of them,
json Object's key“ msg",Is it "request succeeded","request was aborted","Other errors" One of them,
.....

Perform the above steps several times until all values are obtained!!!!

At this point, a question arises: what if all the situations are not returned?

Objectively speaking, such an interface is meaningless and not easy to test. Therefore, back-end engineers generally change according to the changes of the requester. How does the request change? These are the parameters we want to talk about next.

Request with parameters (demo2.php)

    // Tell the browser to return as json type
    header('Content-Type:application/json; charset=utf-8');
    // Get parameter index
    $index = $_GET['index'];

    // Gets a random number from 0 to 2
    //$index = rand(0,2);

    $msgs = array('Request succeeded','request was aborted','Other errors');
    // Organizational data
    $data = array(
        'code' => 200,
        'msg' => $msgs[$index],
        'result' => array(
            'key1' => 'value1',
            'key2' => 'value2'
        )
    );
    // PHP array to json
    $rst = json_encode($data,JSON_UNESCAPED_UNICODE);
    // Print results
    echo $rst;

We input the following contents in the browser:

http://localhost/about_test/demo2.php?index=0,
http://localhost/about_test/demo2.php?index=1,
http://localhost/about_test/demo2.php?index=2,

The returned results are as follows:

{
   "code": 200,
   "msg": "Request succeeded",
   "result": {
       "key1": "value1",
       "key2": "value2"
   }
}

{
   "code": 200,
   "msg": "request was aborted",
   "result": {
       "key1": "value1",
       "key2": "value2"
   }
}

{
   "code": 200,
   "msg": "Other errors",
   "result": {
       "key1": "value1",
       "key2": "value2"
   }
}

Does it look familiar? Yes, it is very similar to the return of demo1.php, but there are also differences. The difference is that as long as the index behind the url is fixed to a certain value, the return result is also fixed to a certain value. Similar interfaces with paging parameters are as follows (getList.php):

// Tell the browser to return as json type
header('Content-Type:application/json; charset=utf-8');

// Get parameter index
$page = $_GET['page'];
$size = $_GET['size'];

/**
 * In the real development process, the database will be operated. We temporarily use a multi-dimensional array instead
 */
$data_list = array(
    array('id'=>1,'name'=>'Jackie Chan'),
    array('id'=>2,'name'=>'Zhou Runfa'),
    array('id'=>3,'name'=>'Zhou Xingchi'),
    array('id'=>4,'name'=>'Jet Li'),
    array('id'=>5,'name'=>'Chao Wei Liang'),
    array('id'=>6,'name'=>'Zhang Manyu'),
    array('id'=>7,'name'=>'Hui Yinghong'),
    array('id'=>8,'name'=>'Lin Qingxia'),
    array('id'=>9,'name'=>'Wang Zuxian'),
    array('id'=>10,'name'=>'Gong Li')
);
foreach($data_list as $key=>$value){

    if($key >= ($page -1) * $size &&  $key < $page * $size){
        $temp[] = $value;
    }
}

// Organizational data
$data = array(
    'code' => 200,
    'msg' => 'Request succeeded',
    'result' => $temp

);

// PHP array to json
$rst = json_encode($data,JSON_UNESCAPED_UNICODE);

// Print results
echo $rst;

We open the following connection in the browser:

http://localhost/about_test/getList.php?page=4&size=2

The returned results are as follows:

{
    "code": 200,
    "msg": "Request succeeded",
    "result": [
        {
            "id": 7,
            "name": "Hui Yinghong"
        },
        {
            "id": 8,
            "name": "Lin Qingxia"
        }
    ]
}

If we use the packet capture tool (which will be described in detail later), we may often see that there are no parameters behind the url, but the returned data may also change. What's the matter? In fact, there is another way to access the interface - post request.

Pose change request (post request)

We slightly modify the getList.php file to get the following code (getList_post.php):

// Tell the browser to return as json type
header('Content-Type:application/json; charset=utf-8');

// Get parameter index
//$page = $_GET['page'];
//$size = $_GET['size'];

$page = $_POST['page'];
$size = $_POST['size'];

/**
 * In the real development process, the database will be operated. We temporarily use a multi-dimensional array instead
 */
$data_list = array(
    array('id'=>1,'name'=>'Jackie Chan'),
    array('id'=>2,'name'=>'Zhou Runfa'),
    array('id'=>3,'name'=>'Zhou Xingchi'),
    array('id'=>4,'name'=>'Jet Li'),
    array('id'=>5,'name'=>'Chao Wei Liang'),
    array('id'=>6,'name'=>'Zhang Manyu'),
    array('id'=>7,'name'=>'Hui Yinghong'),
    array('id'=>8,'name'=>'Lin Qingxia'),
    array('id'=>9,'name'=>'Wang Zuxian'),
    array('id'=>10,'name'=>'Gong Li')
);
foreach($data_list as $key=>$value){

    if($key >= ($page -1) * $size &&  $key < $page * $size){
        $temp[] = $value;
    }
}
// Organizational data
$data = array(
    'code' => 200,
    'msg' => 'Request succeeded',
    'result' => $temp

);
// PHP array to json
$rst = json_encode($data,JSON_UNESCAPED_UNICODE);

// Print results
echo $rst;

This time, we can't directly enter the access address + parameter in the browser to obtain the returned results. We can use the terminal provided by the mac to access:

curl localhost/about_test/getList_post.php -X POST -d 'page=4&size=2'

We have achieved the following results:

{"code":200,"msg":"Request succeeded","result":[{"id":7,"name":"Hui Yinghong"},{"id":8,"name":"Lin Qingxia"}]}

From the above comparison, you can probably feel that post requests are more troublesome than get!

This may also be in line with our simple cognition:

Simple and meaningless are always easy to get;

Complex and significant need you to pay to get (post itself means sending).

summary

  1. A simple interface, sending a request will return data.
  2. Testing is testing rules.
  3. The data returned by the interface will change according to the parameters.
  4. Interface request methods include get and post.

Finally, you can pay attention to the official account: the sad hot strip! There are many data sharing in the entrance! The information is all the knowledge points that the interviewer will ask when interviewed, and also includes many common knowledge in the testing industry, including basic knowledge, Linux essential, Shell, Internet program principle, Mysql database, grab tool topics, interface test tools, test advanced -Python programming, Web Automated testing, APP automated testing, interface automated testing, advanced continuous integration testing, test architecture development, test framework, performance testing, security testing, etc.

If my blog is helpful to you and you like my blog content, please click "like", "comment" and "collect" for three times! Recommended software testing communication learning group: 914172719, which will share some videos recorded by senior architects

Haowen recommendation

Job transfer interview, job hopping interview, these interview skills that software testers must know!

Interview experience: move bricks in first tier cities! Face software testing post again, 5000 will be satisfied

Interviewer: I've worked for three years and have a preliminary test? I'm afraid your title of software test engineer needs double quotation marks

What kind of person is suitable for software testing?

The man who left work on time was promoted before me

The test post changed jobs repeatedly and disappeared

Tags: Python Programmer unit testing software testing IT

Posted on Tue, 14 Sep 2021 15:32:06 -0400 by dthomas31uk