# 开发人员: hanhan丶
# 开发时间: 2020/11/12 14:36
import flask, json #Flask 一个轻量级的web框架
from flask_corsimport *
server = flask.Flask(__name__) # __name__代表当前的python文件。把当前的python文件当做一个服务启动
CORS(server, supports_credentials=True) # 解决跨域
@server.route('/login', methods=['post'])
# 第一个参数就是路径,第二个参数支持的请求方式,不写的话默认是get,
# 加了@server.route才是一个接口,不然就是一个普通函数
def login():
user = flask.request.values.to_dict()
for itemin user:
items = json.loads(item)
loginName = items.get("loginName")
password = items.get("password")
if loginNameand password:
res = {"code":0, "msg":"请求成功", "data": {"loginName": loginName, "password": password}}
else:
res = {'msg':'调用失败'}
# json.dumps 序列化时对中文默认使用的ascii编码,输出中文需要设置ensure_ascii=False
return json.dumps(res, ensure_ascii=False)
if __name__ =='__main__':
# port可以指定端口,默认端口是5000
# host默认是服务器,默认是127.0.0.1
# debug=True 修改时不关闭服务
server.run(debug=True)
前端:
<template>
<div>
账号:<input type="text" v-model="loginName">
<br>
密码:<input type="text" v-model="password">
<br>
<br>
<br>
<button @click="btn">点击</button>
</div>
</template>
<script>
import axios from "axios"
export default {
data() {
return {
loginName: "",
password: ""
}
},
methods: {
getDate() {
axios({
headers: {
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/x-www-form-urlencodedcharset=UTF-8"
},
url: "http://127.0.0.1:5000/login",
method: "post",
data: {
loginName: this.loginName,
password: this.password
}
}).then(res =>{
console.log(res)
})
},
btn() {
this.getDate()
}
}
}
</script>
<style>
</style>
# -*- coding:utf-8 -*-# Author Caoxlimport sys account_file='E:\user.txt'locked_file='E:\locked.txt' def deny_account(username): print('您的用户已被锁定') with file(locked_file,'a') as deny_f: deny_f.write('\n'+username) def main(): retry_count=0 retry_limit=3 #循环次数 while retry_count<retry_limit: #用户最多重复登陆3次 username=raw_input('请输入您的用户名:') #引导用户输入用户名 with file(locked_file,'r') as lock_f: #采用with打开方式,将locked_file赋值lock_f,防止忘记f.close()关闭文件 for line in lock_f.readlines(): #循环遍历每一行的内容if len(line)==0: #对每一行内容进行处理 continueif username == line.strip(): #利用.strip这个函数去掉换行符,来对username进行匹配。 sys.exit('用户已经被锁定!') if len(username)==0: #提示用户登录时,用户名不能为空! print('用户名不能为空,请重新输入') continue password= raw_input('请输入您的密码:') #引导用户输入密码 with file(account_file,'r') as account_f: flag= Falsefor line in account_f.readlines():user,pawd=line.strip().split() #将用户名和对应密码进行处理if username==user and password==pawd: #判断用户名和密码 print('success!') flag=True break #退出for循环 if flag==False: #避免用户在三次输入后,依然提示重新输入。 if retry_count<2:print('您输入的用户名或密码有误,请重新输入!')retry_count+=1 else: print('欢迎用户登陆成功!!') break # 加标志位是为了用户能够成功推出整个循环! deny_account(username) #对应上面的def函数把锁定账号加入文档中去 if __name__ == '__main__': main()1. 用Python封装被测试接口,对于HTTP接口我们通常会采用 GET和POST 2种调用方式去访问,所以必须把这2种方式都封装进去# -*- coding:gb2312 -*-
import urllib2,urllib
'''
函数说明:url 特殊字符编码转换
作者:xiaonan
输入参数:待转换的字符串数据
输出参数:转换完成后的字符串数据
'''
def urlcode(data):
return urllib2.quote(str(data))
'''
函数说明:获取用户信息的API接口
作者:xiaonan
输入参数:用户名(uname),HTTP接口调用方式(GET或者POST)
输出参数:HTTP接口调用返回数据
'''
def GetUserInfo(uname,method):
if method == 'GET':
url = 'http://ip:port/interface/GetUserInfo.php?uname='+urlcode(uname)
result = urllib2.urlopen(url).read()
return result
if method == 'POST':
url = 'http://ip:port/interface/GetUserInfo.php'
values = {'uname' : uname}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
return result
2. 编写、组织测试脚本, 准备测试数据
根据Testcase的具体业务逻辑用事先准备好的测试数据去调用封装好的API接口,验证实际返回结果是否与预期返回结果一致.
测试数据可以以各种形式存放,如Excel数据表:
TestCaseNameuname method Expected Result
TestCase1 aaaa GET....
TestCase2 aaaa POST ....
TestCase3 bbbb GET....
... ... .......
# -*- coding:gb2312 -*-
import xlrd
'''
函数说明: Testcase 脚本
作者:xiaonan
输入参数:测试数据,API接口
输出参数:测试日志,测试报告
'''
def GetUser():
bk = xlrd.open_workbook(excel文件名称) # 打开excel文件
sh = bk.sheet_by_name(excel表名)# 打开excel表
nrows = sh.nrows # 获取总行数
for i in range(1,nrows):
TestCase = sh.cell_value(i,0)
uname = sh.cell_value(i,1)
method = sh.cell_value(i,2)
EX_Result=sh.cell_value(i,3)
WriterLog('Testcase Name:'+TestCase+'TestData: uname = '+uname+' ,method = '+method+' ,EX_Result = ' + ,EX_Result) # 写测试日志
AC_result = GetUserInfo(uname,method) # 调用API接口
WriterLog('AC_result = ' + AC_result) # 写测试日志
if EX_Result == AC_result: #实际结果与预期结果对比
WriterLog(...) #写测试日志
WriterReport(...)#写测试报告
else
WriterLog(...)#写测试日志
WriterReport(...)#写测试报告
3. 组织测试套,用驱动文件去调用执行所有测试套件,完成相关测试,并生成测试日志及测试报告.
# -*- coding:gb2312 -*-
'''
函数说明: Testsuit Driver驱动脚本
作者:xiaonan
输入参数:TestCase 脚本
输出参数:测试日志,测试报告
'''
if __name__ == '__main__':
...
WriterLog() #写测试日志
GetUser() # TestCase 脚本
...
...
Report(....) # 统计汇总所有测试报告数据,以文件或页面形式呈现.
4. 执行测试脚本,分析测试结果. 根据测试报告,如果有Bug则提交.