1. Variable extraction and reference
Variable extraction and reference are mainly to solve the problem of parameter dependence between interfaces .
Usage scenario: If the parameters of interface A need to use some data returned by interface B, then after requesting interface B, extract the data and save it for use when requesting interface A.
1. Variable extraction
In the use case set or use case data, specify the variables to be extracted through the extract field.
grammar:
"extract": { "Variable name": ("Variable storage level", "Extract method", "Extract expression"), }
-
variable name :
The name of the variable that holds the data
-
Variable storage level :
EVN: global variable, EVN env in the settings file : local variable (only valid for the current use case set): the env field of the use case set
-
Extraction method :
re: regular expression to extract
jsonpath: extract by jsonpath
case:
"extract": {
# 通过jsonpath提取
"token": ("env", "jsonpath", "$..token"),
# 通过正则表达式提取
'member_id': ("env", "re", r'"id":(.+?),')
}
2. Variable reference
Variable reference expressions:
$}
Citation priority:
The local variable of the use case (the variable in the env of the use case) is referenced first. If the local variable does not exist, the global variable (the variable saved in ENV in setting.py) will be referenced.
If you want to learn automated testing, here I recommend a set of videos for you. This video can be said to be the first interface automation testing tutorial on the entire network at station B. At the same time, the number of online users has reached 1,000, and there are notes to collect and various Lu Dashen Technical Exchange: 798478386
2. Use case assertion
Regarding the comparison between the expected result and the actual result of the test case, a verification field is encapsulated in the apin. You only need to define the expected result, the actual result extraction expression, and the assertion method in the verification to realize the assertion of the use case!
1. Basic grammar
verification = [
[断言方式, 预期结果, 实际结果]
]
2. Assertion method
There are currently two assertion methods supported in apin:
assert equality: eq
Expected and actual results are equal
verification = [
['eq', 预期结果, 实际结果]
]
Assertion contains: contains
Actual results contain content of expected results
verification = [
['contains', 预期结果, 实际结果]
]
3. Acquisition of actual results
Regarding the actual result extraction of the assertion, you need to use V{ {expression}} to extract, and the expression supports two extraction methods: jsonpath and regular expression .
Regular expression extraction
# 通过正则表达式来提取实际结果中的msg字段。
verification = [
['eq', {'msg':"OK"}, {"msg": "V{
{msg:'(.+?)'}}"}]
]
Extract via jsonpath
# 通过jsonpath来提取实际结果中的msg字段。
verification = [
['eq', {'msg':"OK"}, {"msg": 'V{
{$..msg}}']
]
4. Assertion of HTTP status code
The above two methods can already extract the data returned by the result, so what if you want to assert the status code of the interface http request?
Apin also provides a built-in field name to represent the HTTP status code.
# 断言接口请求的http状态码是否等于200
verification = [
['eq', 200, 'status_code']
]
3. The use of function tools
apin supports calling custom functions in test cases to process data, such as encrypting data, generating data randomly, and so on.
1. Custom functions
There is a funcTools.py in the project created by apin, in which you can define the function yourself, and then call it through F{xxx()} in the use case.
-
Example: funcTools.py file
import hashlib,random
def md5_encrypt(msg):
"""md5加密"""
md5 = hashlib.md5()
md5.update(msg.encode("utf8"))
return md5.hexdigest()
def rand_phone():
"""随机生成手机号的函数"""
import random
for i in range(8):
phone += str(random.randint(0, 9))
return str(phone)
def get_timestamp():
"""获取时间戳"""
return time.time()
-
Note: the data processed by the function needs to be returned
2. Reference functions in use cases
-
Quote expression: F
-
demo1: The user in the use case data refers to the previously defined rand_phone function
{
'title': "普通用户注册",
'interface': "member/register",
"method": "post",
'json': {"user": "F{rand_phone()}", "pwd": "lemon123"},
}
-
demo2: Use the pwd in the case data, refer to the md5_encrypt function defined earlier to encrypt the password with md
Note: If the quoted function and the passed parameter are variables, there is no need to add quotation marks around the variable application expression
{
'title': "普通用户登录",
'interface': "member/login",
"method": "post",
'json': {"user": "13109877890", "pwd": "F{md5_encrypt('lemon123')}"},
}
# 引用函数,变量作为参数传递
{
'title': "普通用户登录",
'interface': "member/login",
"method": "post",
'json': {"user": "${
{user}}", "pwd": "F{md5_encrypt(${
{pwd}})}"},
}
4. Project global configuration
The setting.py file in the project is the configuration file of the entire project. Next, we will introduce the configuration options of the project in detail.
1. Run in debug mode
After the project is created, the default operation is to enable the debug mode, and a detailed debug level log will be output during the operation.
If you don't want to see the running log, just set DEBUG in the settings to False.
# 是否开启debug模式:True为debug模式,False为关闭debug模式
DEBUG = False
2. ENV global variables
The ENV in settings.py can set the project global configuration
global domain name
It is recommended to set the global host in ENV. It is not recommended to set the host in each test case, and it is more convenient to switch the test environment (if the host is not defined in the test case data, the global host address will be automatically referenced).
ENV = {
"host":"http://WWW.XXX.com/",
}
global headers
If the project interface has required request header data, it can also be set directly in ENV (if it is not defined in the use case data, the global headers will also be automatically referenced).
ENV = {
"host":"http://WWW.XXX.com/",
"headers": {"UserAgent": "apin-test01"}
}
global test data
If the use case needs to refer to some test data prepared in advance, such as test account, password, etc.
Such as: define a test account, test password, user id
ENV = {
"host":"http://WWW.XXX.com/",
"headers": {"UserAgent": "apin-test01"},
"user":"[email protected]",
"pwd":"lemon123",
"user_id":111
}
Directly use ${ {}} in the test case to reference,
# 引用user和pwd
{
'title': "登录",
'interface': "member/register",
"method": "post",
'json': {"mobile_phone": "${
{user}}", "pwd": "${
{pwd}}"},
}
Note: If the local environment and the global variable have the same name, the local variable is referenced first.
3. Test report
Through TEST_RESULT in setting, you can configure the output information of the test report.
TEST_RESULT = {
# 测试报告文件名
"filename": "report.html",
# 测试人员
"tester": "测试员",
# 报告标题
"title": "测试报告",
# 报告样式 :有1,2,三个样式
"templates": 1,
# 报告描述信息
"desc": "XX项目测试生成的报告"
}
4. Email push test results
If you want to send the test results to the specified mailbox, just add the EMAIL configuration in settings.py.
EMAIL = {
# smtp服务器地址
"host": 'smtp.qq.com',
# smtp服务器端口
"port": 465,
# 邮箱账号
"user": "[email protected]",
# smtps授权码
"password": "xxxx",
# 收件人列表
"to_addrs": ['[email protected]','[email protected]'],
# 是否发送附件
"is_file": True
}
5. The test results are pushed to the DingTalk group
If you want to push the test results to the DingTalk group, just add the DINGTALK configuration in settings.py.
DINGTALK = {
# 钉钉机器人的Webhook地址
"url": "",
# 如果钉钉机器人安全设置了关键字,则需要传入对应的关键字
"key": None,
# 如果钉钉机器人安全设置了签名,则需要传入对应的密钥
"secret": None,
# 钉钉群中要@人的手机号列表,如:[137xxx,188xxx]
"atMobiles": [],
# 是否@所有人
"isatall": False
}
6. The test results are pushed to the enterprise WeChat group
If you want to push the test results to the enterprise WeChat group, just add WECHAT configuration in settings.py.
WECHAT = {
# 企业微信群ID
"chatid": "",
# 调用企业微信API接口的凭证
"access_token": ""
}