软件测试是这学期刚开始学习的,建议:
Python版本为3.7
pycharm版本无要求,我装的比较新,2021那一版的
MySQL5.7.26
太久没使用Navicat,打不开?连接不上?——>卸载MySQL重装
实验一:Postman+Fiddler实验
安装postman与fiddler工具并熟悉使用,了解接口的请求信息与响应信息,使用过程中出现个人信息并截几张图。
推荐链接——全网最全的 postman 工具使用教程
推荐链接——Fiddler抓包配置和使用(全网最详细教程)
实验二:Flask框架开发接口实验
基于Flask框架开发对个人信息增删改查接口,返回Json数据。要求出现个人信息,数据保存在数据库中,提交源代码,以及过程与结果截图
推荐链接——Python+Flask对MySQL数据库进行增删查改并在页面展示数据
我个人的使用和推荐博客差不多,修改了一点代码
1、安装flask
2、pycharm的源代码
(1)sql_select.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
head>
<body>
<div>
<form action="http://localhost:5000/query" >
请输入要查询的id:<input type="text" name="id" ><br/><br/>
<input type="submit" value="Submit">
form>
<a href="/"><button>返回button>a>
div>
body>
html>
(2)sql_insert.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div>
<h4>在该页面可以插入数据h4>
<form action="http://localhost:5000/add" method="POST">
name:<input type="text" name="name" autocomplete="off"><br/><br/>
age:<input type="text" name="age" autocomplete="off"><br/><br/>
<button onclick="myFunction()">提交button> <a href="/"><button>返回button>a>
<p id="demo">p>
form>
<script>
function myFunction(){
var txt;
confirm("Successfully inserted data!");
txt = "你已成功插入数据!点击查看数据可查看更新后的数据";
document.getElementById("demo").innerHTML = txt;
}
script>
div>
body>
html>
(3)sql_update.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div>
<h4>在该页面修改你的数据h4>
<form action="http://localhost:5000/update" method="POST">
请输入你需要修改的id:<input type="text" name="id" autocomplete="off"><br/>
请输入修改的name:<input type="text" name="name" autocomplete="off"><br/>
请输入修改的age:<input type="text" name="age" autocomplete="off"><br/>
<input type="submit" value="Submit">
form>
<a href="/"><button>返回button>a>
div>
body>
html>
(4)sql_delete.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div>
<form action="http://localhost:5000/delete" method="POST">
请输入要查删除的id:<input type="text" name="id" ><br/><br/>
<input type="submit" value="Submit">
<a href="/"><button>返回button>a>
form>
div>
body>
html>
(5)index.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> *** 作title>
head>
<body>
<div>
<div>请选择要进行的 *** 作!div><div> div>
<div><a href="/submit_select"><button>信息查询button>a>div>
<div><a href="/submit_update"><button>修改信息button>a>div>
<div><a href="/submit_insert"><button>添加信息button>a>div>
<div><a href="/submit_delete"><button>删除信息button>a>div>
div>
body>
html>
(6)manage.py
# coding=utf-8
from flask import Flask, render_template
import pymysql
from flask import request
import json
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def login():
return render_template("index.html")
# 添加数据
@app.route('/submit_insert', methods=['GET', 'POST'])
def submit_insert():
return render_template("sql_insert.html")
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'GET':
# 从数据库获取信息
name1 = request.values.get('name')
age1 = request.values.get('age')
elif request.method == 'POST':
# 添加数据
try:
# 从网页表单获取数据
name1 = request.form['name']
age1 = request.form['age']
except KeyError:
ret = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(ret, ensure_ascii=False)
# 数据非空
if name1 and age1:
# 打开数据库连接
db = pymysql.connect(host="localhost", port=3309, user="root", password="123456", database="test",
charset="utf8")
# 使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
# sql插入语句 拼接注意字符串要加引号
sql = "insert into test(name,age) values ('" + name1 + "' ," + age1 + ")"
sql1 = "alter table test drop id"
sql2 = "alter table test add id int not null first"
sql3 = "alter table test modify column id int not null auto_increment,add primary key(id)"
try:
cursor.execute(sql)
cursor.execute(sql1)
cursor.execute(sql2)
cursor.execute(sql3)
# 提交
db.commit()
# return '添加成功'
ret = {'name': name1, 'age': age1}
return json.dumps(ret)
except Exception as e:
# 错误回滚
db.rollback()
ret = {'code': 10002, 'message': '添加失败!'}
return json.dumps(ret)
finally:
db.close()
# 删除数据
@app.route('/submit_delete')
def submit_delete():
return render_template("sql_delete.html")
@app.route('/delete', methods=['GET', 'POST'])
def delete():
if request.method == 'GET':
id1 = request.values.get('id')
elif request.method == 'POST':
try:
# 从网页表单获取id
id1 = request.form['id']
except KeyError:
ret = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(ret, ensure_ascii=False)
if id1:
# 打开数据库连接
db = pymysql.connect(host="localhost", port=3309, user="root", password="123456", database="test")
# 使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
# sql删除语句
sql = "delete from test where id =" + id1
sql1 = "alter table test drop id"
sql2 = "alter table test add id int not null first"
sql3 = "alter table test modify column id int not null auto_increment,add primary key(id)"
try:
cursor.execute(sql)
cursor.execute(sql1)
cursor.execute(sql2)
cursor.execute(sql3)
# 提交
db.commit()
# return '删除成功'
ret = {'code': 10006, 'message': '删除成功!'}
return json.dumps(ret)
except Exception as e:
# 错误回滚
db.rollback()
ret = {'code': 10003, 'message': '删除失败!'}
return json.dumps(ret)
finally:
db.close()
# 修改数据
@app.route('/submit_update')
def submit_update():
return render_template("sql_update.html")
@app.route('/update', methods=['GET', 'POST'])
def modify():
if request.method == 'GET':
id1 = request.values.get('id')
name1 = request.values.get('name')
age1 = request.values.get('age')
elif request.method == 'POST':
try:
id1 = request.form['id']
name1 = request.values.get('name')
age1 = request.values.get('age')
except KeyError:
ret = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(ret, ensure_ascii=False)
if id1 and name1 and age1:
# 打开数据库连接
db = pymysql.connect(host="localhost", port=3309, user="root", password="123456", database="test")
# 使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
# sql修改语句
sql = "update test set age =" + age1 + ", name = '" + name1 + "'where id = " + id1
sql1 = "alter table test drop id"
sql2 = "alter table test add id int not null first"
sql3 = "alter table test modify column id int not null auto_increment,add primary key(id)"
try:
cursor.execute(sql)
cursor.execute(sql1)
cursor.execute(sql2)
cursor.execute(sql3)
# 提交
db.commit()
# return '修改成功'
ret = {'id': id1, 'name': name1, 'age': age1}
# print("id=%s,name=%s,age=%s" % (id1, name1, age1))
return json.dumps(ret)
except Exception as e:
# 错误回滚
db.rollback()
ret = {'code': 10004, 'message': '修改失败!'}
return json.dumps(ret)
finally:
db.close()
# 根据id查询数据
@app.route('/submit_select', methods=['GET', 'POST'])
def submit_select():
return render_template("sql_select.html")
@app.route('/query', methods=['GET', 'POST'])
def query():
if request.method == 'GET':
id1 = request.values.get('id')
elif request.method == 'POST':
try:
id1 = request.form['id']
except KeyError:
ret = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(ret, ensure_ascii=False)
if id1:
# 打开数据库连接
db = pymysql.connect(host="localhost", port=3309, user="root", password="123456", database="test")
# 使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
# sql查询语句
sql = "select * from test where id = " + id1
try:
cursor.execute(sql)
# 提交
db.commit()
results = cursor.fetchall()
# 遍历结果
for rows in results:
id1 = rows[0]
name1 = rows[1]
age1 = rows[2]
ret = {'id': id1, 'name': name1, 'age': age1}
# print("id=%s,name=%s,age=%s" % (id1, name1, age1))
return json.dumps(ret)
except Exception as e:
# 错误回滚
db.rollback()
ret = {'code': 10005, 'message': '查询失败!'}
return json.dumps(ret)
finally:
db.close()
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
# 指定端口和ip
app.run(debug=True, host='127.0.0.1', port=5000)
举个例子:查询一条数据
实验三:Unittest+requests接口测试实验
用Unittest+requests实现对实验二中开发的接口进行自动化测试。要求出现个人信息,提交源代码,以及过程与结果截图
unittest的基本语法网上有很多,我就不找链接出来给大家,大家可以自己去找并且学习学习,主要是我自己学的也很烂,o(╥﹏╥)o
1、安装需要的库(我的使用了HTMLTestRunner生成报告)
(1)pip install requests
我自己用pip安装的时候直接搜的这种带网址的,都可以,不影响。
(2)HTMLTestRunner直接pip安装有问题
推荐链接——python3+HTMLTestRunner生成测试报告
根据这个博客的步骤来就ok
2、基本框架
3、代码(只放了有改动的代码,前端代码没动)
(1)unittest_case.py
import os
import unittest
from HTMLTestRunner.HTMLTestRunner import HTMLTestRunner
from gevent import time
from test_case.test_add import Add
from test_case.test_delete import Delete
from test_case.test_query import Query
from test_case.test_update import Update
if __name__ == '__main__':
# unittest.main()
# suite1 = unittest.TestLoader().loadTestsFromTestCase(Add)
# suite1 = unittest.TestLoader().loadTestsFromTestCase(Delete)
# suite1 = unittest.TestLoader().loadTestsFromTestCase(Update)
suite1 = unittest.TestLoader().loadTestsFromTestCase(Query)
suite = unittest.TestSuite([suite1])
# 获取当前文件所在的路径
base_dir = os.path.dirname(os.path.realpath(__file__))
print(base_dir)
dir_path = base_dir + "\..\201901020216_陈彬彬\report\"
now = time.strftime("%Y-%m-%d-%H-%M-%S")
report_path = dir_path + now + "result.html"
with open(report_path,'wb')as f:
runner = HTMLTestRunner(stream=f,verbosity=2,title="unittest报告",description="html测试")
runner.run(suite)
(2)test_add.py
# "添加数据"
import unittest
import requests
class Add(unittest.TestCase):
def setUp(self):
self.base_url = "http://127.0.0.1:5000/add"
'''参数为空'''
def test_add_params_null(self):
r = requests.get(self.base_url, params={'name': ' ', 'age': ' '})
self.result = r.json()
self.assertEqual(self.result['code'], 10002)
self.assertEqual(self.result['message'], '添加失败!')
'''添加成功'''
def test_add_success(self):
r = requests.get(self.base_url, params={'name': '添加成功', 'age': 2022})
self.result = r.json()
self.assertEqual(self.result['code'], 200)
self.assertEqual(self.result['message'], '添加成功!')
def tearDown(self):
pass
(3)test_delete.py
# 删除数据
import unittest
import requests
class Delete(unittest.TestCase):
def setUp(self):
self.base_url = "http://127.0.0.1:5000/delete"
"""请求参数 值为空"""
def test_delete_get_id_null(self):
r = requests.get(self.base_url, params={'id': ' '})
self.result = r.json()
self.assertEqual(self.result['code'], 10003)
self.assertEqual(self.result['message'], '删除失败!')
"""id值类型错误"""
def test_delete_get_id_value_error(self):
r = requests.get(self.base_url, params={'id': "哈哈"})
self.result = r.json()
self.assertEqual(self.result["code"], 10003)
self.assertEqual(self.result["message"], "删除失败!")
'''删除成功'''
def test_delete_success(self):
r = requests.get(self.base_url, params={'id': 30})
self.result = r.json()
self.assertEqual(self.result["code"], 200)
self.assertEqual(self.result["message"], "删除成功!")
def tearDown(self):
pass
(4)test_update.py
# 修改数据
import unittest
import requests
class Update(unittest.TestCase):
def setUp(self):
self.base_url = "http://127.0.0.1:5000/update"
"""请求参数 值为空"""
def test_update_params_null(self):
r = requests.get(self.base_url, params={"id": " ", "name": " ", "age": " "})
self.result = r.json()
self.assertEqual(self.result["code"], 10004)
self.assertEqual(self.result["message"], "修改失败!")
'''修改成功'''
def test_add_success(self):
r = requests.get(self.base_url, params={"id": 20, "name": "我想运行成功", "age": 22})
self.result = r.json()
self.assertEqual(self.result["code"], 200)
self.assertEqual(self.result["message"], "修改成功!")
def tearDown(self):
pass
(5)test_query.py
# 查询数据
import unittest
import requests
class Query(unittest.TestCase):
def setUp(self):
self.base_url = "http://127.0.0.1:5000/query"
''' id= 查询结果为空 '''
def test_get_id_null(self):
r = requests.get(self.base_url, params={'id': ''})
self.result = r.json()
self.assertEqual(self.result['code'], 10001)
self.assertEqual(self.result['message'], '参数不能为空!')
''' id=901 id不存在 '''
def test_get_id_error(self):
r = requests.get(self.base_url, params={'id': 901})
self.result = r.json()
self.assertEqual(self.result['code'], 10005)
self.assertEqual(self.result['message'], '查询失败!')
"""id值类型错误"""
def test_get_id_value_error(self):
r = requests.get(self.base_url, params={'id': "哈哈"})
self.result = r.json()
self.assertEqual(self.result["code"], 10005)
self.assertEqual(self.result["message"], "查询失败!")
''' 根据 id 查询结果成功 '''
def test_get_id_success(self):
r = requests.get(self.base_url, params={'id': 1})
self.result = r.json()
self.assertEqual(self.result['code'], 200)
self.assertEqual(self.result['message'], '查询成功!')
def tearDown(self):
pass
(6)manage.py
# coding=utf-8
from flask import Flask, render_template
import pymysql
from flask import request
import json
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def login():
return render_template("index.html")
# 添加数据
@app.route('/submit_insert', methods=['GET', 'POST'])
def submit_insert():
return render_template("sql_insert.html")
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'GET':
# 从数据库获取信息
name1 = request.values.get('name')
age1 = request.values.get('age')
elif request.method == 'POST':
# 添加数据
try:
# 从网页表单获取数据
name1 = request.form['name']
age1 = request.form['age']
except KeyError:
ret = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(ret, ensure_ascii=False)
# 数据非空
if name1 and age1:
# 打开数据库连接
db = pymysql.connect(host="localhost", port=3309, user="root", password="123456", database="test",charset="utf8")
# 使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
# sql插入语句 拼接注意字符串要加引号
sql = "insert into test(name,age) values ('" + name1 + "' ," + age1 + ")"
try:
cursor.execute(sql)
# 提交
db.commit()
ret = {'code': 200, 'message': '添加成功!'}
return json.dumps(ret)
except Exception as e:
# 错误回滚
db.rollback()
ret = {'code': 10002, 'message': '添加失败!'}
return json.dumps(ret)
finally:
db.close()
# 删除数据
@app.route('/submit_delete')
def submit_delete():
return render_template("sql_delete.html")
@app.route('/delete', methods=['GET', 'POST'])
def delete():
if request.method == 'GET':
id1 = request.values.get('id')
elif request.method == 'POST':
try:
# 从网页表单获取id
id1 = request.form['id']
except KeyError:
ret = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(ret, ensure_ascii=False)
if id1:
# 打开数据库连接
db = pymysql.connect(host="localhost", port=3309, user="root", password="123456", database="test")
# 使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
# sql删除语句
sql = "delete from test where id =" + id1
sql1 = "alter table test drop id"
sql2 = "alter table test add id int not null first"
sql3 = "alter table test modify column id int not null auto_increment,add primary key(id)"
try:
cursor.execute(sql)
cursor.execute(sql1)
cursor.execute(sql2)
cursor.execute(sql3)
# 提交
db.commit()
ret = {'code': 200, 'message': '删除成功!'}
return json.dumps(ret)
except Exception as e:
# 错误回滚
db.rollback()
ret = {'code': 10003, 'message': '删除失败!'}
return json.dumps(ret)
finally:
db.close()
# 修改数据
@app.route('/submit_update')
def submit_update():
return render_template("sql_update.html")
@app.route('/update', methods=['GET', 'POST'])
def modify():
if request.method == 'GET':
id1 = request.values.get('id')
name1 = request.values.get('name')
age1 = request.values.get('age')
elif request.method == 'POST':
try:
id1 = request.form['id']
name1 = request.values.get('name')
age1 = request.values.get('age')
except KeyError:
ret = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(ret, ensure_ascii=False)
if name1 and age1:
# 打开数据库连接
db = pymysql.connect(host="localhost", port=3309, user="root", password="123456", database="test")
# 使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
# sql修改语句
sql = "update test set age =" + age1 + ", name = '" + name1 + "'where id = " + id1
try:
cursor.execute(sql)
# 提交
db.commit()
ret = {'code': 200, 'message': '修改成功!'}
return json.dumps(ret)
except Exception as e:
# 错误回滚
db.rollback()
ret = {'code': 10004, 'message': '修改失败!'}
return json.dumps(ret)
finally:
db.close()
# 根据id查询数据
@app.route('/submit_select', methods=['GET', 'POST'])
def submit_select():
return render_template("sql_select.html")
@app.route('/query', methods=['GET', 'POST'])
def query():
if request.method == 'GET':
id1 = request.values.get('id')
elif request.method == 'POST':
try:
id1 = request.form['id']
except KeyError:
ret = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(ret, ensure_ascii=False)
if id1:
# 打开数据库连接
db = pymysql.connect(host="localhost", port=3309, user="root", password="123456", database="test")
# 使用cursor()方法创建一个游标对象cursor
cursor = db.cursor()
# sql查询语句
sql = "select * from test where id = " + id1
try:
cursor.execute(sql)
# 提交
db.commit()
results = cursor.fetchall()
# 遍历结果
for rows in results:
id1 = rows[0]
name1 = rows[1]
age1 = rows[2]
if name1 and age1:
ret = {'code': 200, 'message': '查询成功!'}
return json.dumps(ret)
else:
ret = {'code': 10005, 'message': '查询失败!'}
return json.dumps(ret)
except Exception as e:
# 错误回滚
db.rollback()
ret = {'code': 10005, 'message': '查询失败!'}
return json.dumps(ret)
finally:
db.close()
else:
ret = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(ret, ensure_ascii=False)
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
# 指定端口和ip
app.run(debug=True, host='127.0.0.1', port=5000)
举个例子:增加一条数据
如果生成的HTML报告和我的有不同,那可能就是下载的HTMLTestRunner修改的不同吧,不影响。
实验四:Robotframework框架实验
安装Robotframework框架,对实验二中开发的接口进行自动化测试,要求出现个人信息,提交源代码,以及过程与结果截图
这篇写的东西太多了,我另外写了一篇,请移步:
实验四:Robotframework框架实验
实验五:持续集成实验
安装Jenkins持续集成平台,并配置实验三与实验四的自动化任务,测试报告能在Jenkins平台展示,实验三的自动化测试任务能定时执行,实验三任务执行完后自动触发实验四自动化测试任务执行。
这篇写的东西太多了,我另外写了一篇,请移步:
实验五:持续集成实验
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)