content
Common interface testing tools
Environment variables and global variables
jenkins continuous integration
concept
What is interface testing?
The explanation given by Baidu Encyclopedia is : Interface test is a kind of test to test the interface between system components. Interface testing is mainly used to detect the interaction points between the external system and the system and between the various internal subsystems. The focus of the test is to check the data exchange, transfer and control management process, as well as the mutual logical dependencies between systems.
Interfaces include internal and external interfaces:
Internal interface : The interface provided by the developer to his own system.
External interface : The development system calls external ones, such as WeChat, Alipay, other interfaces, etc.
In software testing work, the interface is collectively referred to as API, which is used to realize the interaction of data. The essence of interface testing is to test whether the interface can normally interact with data, permission control, and abnormal scenarios.
Common interface testing tools
Commonly used interface testing tools are postman, jmeter
interface returns data
The format of the data returned by the interface is generally: json, html, xml
1. json format ( more than 85% of the market )
Generally, there are three sets of data: {"code":200,"message":"Request successful","dataMap":{"..."}
code: return status code
message: an explanation of the returned information or status error
dataMap: the real return data
2. html format
<html>
<title></title>
<body>
<code>200</code>
......
</body>
</html>
复制代码
3. xml format
<?xml?version="1.0"encoding="utf-8">
<code>200</code>
......
</xml>
复制代码
Interface Test Protocol
1. webservice protocol:
Interface address: http://......?wsdl
2.dubbo protocol:
The interface address starts with dubbo://......
Suitable for the transfer of small amounts of data
3. http protocol (90%) :
Interface address: http://.....
http port is : 80
https = http+ssl secure transport protocol, port 443
The HTTP protocol is mainly used for data transmission on the market, so we mainly learn this protocol.
What is the http protocol?
HTTP is a hypertext transfer protocol, which is mainly used to transmit data between browsers and servers. The interaction has two parts: request and response.
Request: get, post, put, delete
The content of the request part generally includes :
1. Request line : request method, request address and protocol
2. Request headers : The HTTP protocol uses HTTP headers to convey meta-information about the request. An HTTP header is a colon-separated name/value pair preceded by the HTTP header name and followed by the HTTP value, for example:
accept: application/json --- the data format that the client can receive
X-Requested-with: XMLHttpRequest -- asynchronous request
user-agent: -- the user of the client
Host:-- the requested host address
cookie: --cookie information (requested)
accept-encoding: gzip, deflate, br--compression method
3. Empty line : Send carriage return and back line to notify the server that there is no more request header;
4. Message body: When there is a query string in the HTTP request, if it is the GET method, the query character or the form data additional value request line, there is no content in the message body; if it is the POST method, the query string or form data and added to the message body.
Response: Here are the response status codes
2xx: Indicates that the request was sent successfully;
3xx: indicates that the resource is transferred, commonly known as redirection;
4xx: Indicates that the path of the interface cannot be found, and the client is wrong;
5xx: Indicates an internal abnormality in the system, generally there is an interface defect or the request content is incorrect, and the server is wrong.
Part of the response:
1. Response line: protocol, response code, response information
2. Response header:
server:nginx --service
date --- time
content-type: application/json;charset=UTF-8
set-cookie: --response
3. Empty line
4. The specific content of the response
install postman
1. Enter the postman official website www.postman.com/downloads/
2. Download the relevant installation package according to your computer configuration
3. The installation is successfully opened
Refine the page content of postman:
Params : used to pass parameters in the get request
Authorization : The authentication function that comes with postman
Headers : request headers
Body : post request parameters
-- none: no arguments
--form-data: both files and key-value pairs
-- x-www-form-urlencoded: only transmit key-value pairs
-- raw: create JSon, TXT, xml, HTML, js transfers
-- binary: transfer the file in binary mode
Pre-request Script : The script before the interface request, written in js format
Tests : the code for the assertion
response section
Body : the returned data
-- Pretty: Display in json format
-- Raw: Display in text format
-- Preview: Display in web page format
cookie : returned cookie information
Header : Response header
Test Results : Assertion result
Built-in dynamic parameters
Timestamp: { {$timestamp}}
Generate random integers from 0-1000: { {$randomInt}}
Generate a GUID string: { {$guid}}
Environment variables and global variables
In enterprises, there are generally development environments, test environments and online environments, and some companies also have pre-release environments. When we do interface testing, we will find that the addresses of interfaces in these environments are different except for the ip address. Other The places are the same. In the actual test, it is impossible to write a set of test logic for each environment. At this time, we need to use environment variables, which are global variables.
Add three new environments and add the corresponding ip address variables to the environment variables:
Parameterize the IP address in the interface address as { {ip}} and try to run it once:
interface association
In actual work, the values of some variables change dynamically, and the value of the parameter, such as token, needs to be passed in when the next interface request is made.
We need to obtain the value of the token in the response of the previous interface, set the value as a global variable, and then pass in the value in the form of parameterization in the next interface, which can realize the association of the interface.
1. Interface association method 1: json extractor
//把responseBody转为json字符串
var jsvalue = JSON.parse(responseBody);
//将jsvalue的值打印到控制台上
console.log(jsvalue)
// 提取token的值,并保存到全局变量
pm.globals.set("token", jsvalue.token);
复制代码
Postman also provides common Tests writing methods, as shown in the following figure:
2. Interface Association Method 2: Regular Expression Extractor
{
"tag":{
"id":100,
"name":"张三"
}
}
复制代码
If the content of the response data is as above, and we want to extract 100 now, the rules for using regular expressions in Tests to extract are responseBody.match(new RegExp('"id":(.+?),'))
, this method means to match "id":
the following and ,
previous content, if the result is printed to the console, it is an array: [" "id":100,","100"]
, If you only get 100, you need to add an index responseBody.match(new RegExp('"timestamp":(.+?),'))[1]
.
3. Interface association method 2: cookie extractor
As shown in the image below, we need to extract the value in the response result Cookies and print the result to the console.
Methods as below:
//提取Cookies中SERVERID中的Value值并保存在cookies_serverid变量中
var cookies_serverid = postman.getResponseCookie('SERVERID').value
//将变量cookies_serverid中的值打印到控制台
console.log(cookies_serverid)
复制代码
batch execution
Through the above operations, we can realize the association between interfaces. To achieve complete automation, batch execution is required.
Affirmation
In order to fully implement interface testing, assertions are naturally indispensable. In postman, the content of assertions is written in Tests, and the system provides eight assertion methods:
Eight assertion methods:
//1、返回的状态码为200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
//2、返回的结果中包含一个指定的字符串
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
//3、对返回结果做json字段检查
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
//4、断言返回的结果等于一个字符串
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
//5、断言响应头中包含有指定的响应头
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
});
//6、断言接口请求的时间少于200ms
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
//7、断言一个post请求返回的状态码是否在指定的范围之间
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
//8、断言返回的状态码信息中包含指定的字符串
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
复制代码
global assertion
In general, each interface will assert that the returned status code is 200, so we can set this assertion as a global assertion.
Newman
What is Newman?
Newman is the classic brand of Newman mobile phone, haha, just kidding. Simply put, Newman is the command line version ( CLI mode (Command-line Interface: command line mode) ) of Postman.
Newman can use the collection file exported by Postman to run directly on the command line, replacing the operation of Postman interface operation with the command line. Because it is a command line operation, it can be used with jenkins to do interface automation testing.
Install Newman
Newman is written in pure JS, so its operation requires the support of the NodeJS runtime library, so the premise of downloading this version is that NodeJS must be installed on your computer in advance, and then use NodeJS to download Newman.
Order:npm install -g newman
how to use?
To run the interface test cases in postman, you need to export the interface test case set, export environment variables and global variables, and save the exported files locally.
Go to the command line window and run the command:
newman run "E:\testApi\yongli.json" -e "E:\testApi\hunjingbl.json" -g "E:\testApi\quanjubl.json" -r cli,html,json,junit --reporter-html-export "E:\testApi\report.html"
Description: newman run
followed by the test case set file directly, -e followed by the environment variable file, -g followed by the global variable file, -r followed by the output format of the test report and the file path of the output report.
This is the successful command window display:
Open the output test report file:
jenkins continuous integration
The installation of Jenkins can be searched by Baidu
After the installation is successful, create a new project
Enter the project configuration page and configure related information
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")
postscript
There is still a lot about interface testing, such as the Python automated test interface, and the corresponding testing framework, etc. If you want to be invincible in the workplace, you must continue to learn. The above content is for me as a test novice A simple overview, I hope the great gods can give more advice.
If the article is a little useful to you who are currently reading, please give it a like! ! ! come on