一、考试系统
1. 导入类
import unittest
import requests
from public.get_exam_cookie import get_exam_cookie
import time
2. 用例1-考试系统后台-登录
class MyTestCase(unittest.TestCase):
def test_008_exam_login(self):
'''考试系统后台-登录'''
url = 'http://182.92.178.83:8088/api/user/login'
payload_login = {"userName": "admin", "password": "123456", "remember": False}
login = requests.post(url, json=payload_login, timeout=30)
self.assertEqual('成功',login.json()['message'])
3. 用例2-考试系统后台-用户管理-管理员-添加
def test_009_new_admin(self):
'''考试系统后台-用户管理-管理员-添加'''
#添加
url = 'http://182.92.178.83:8088/api/admin/user/edit'
global now_time
now_time = int(time.time())
payload_new_admin ={"id":'null',
"userName":'大白猫%d'%now_time,
"password":now_time,
"realName":"大白猫",
"role":3,"status":1,"age":"2","sex":"1",
"birthDay":'2020-11-09 00:00:00',
"phone":'13434544567'}
global headers_admin
headers_admin ={'Cookie':get_exam_cookie()}
new_admin = requests.post(url,json=payload_new_admin,headers =headers_admin ,timeout =30)
response = new_admin.json()
global admin_id
admin_id = response['response']['id']
#断言
self.assertEqual('成功',response['message']) #返回信息为成功
global exp_userName
exp_userName = payload_new_admin['userName']
act_userName = response['response']['userName']
self.assertEqual(exp_userName, act_userName) #返回添加的用户名与实际添加的用户名相等
4. 用例3-考试系统后台-用户管理-管理员列表-查询
def test_010_admin_search(self):
'''考试系统后台-用户管理-管理员列表-查询'''
#查询
url = 'http://182.92.178.83:8088/api/admin/user/page/list'
payload_search = {"userName":exp_userName,"role":3,"pageIndex":1,"pageSize":10}
search = requests.post(url, json=payload_search, headers=headers_admin, timeout=30)
response = search.json()
#断言
self.assertEqual('成功', response['message'])
exp_search_name = payload_search['userName']
act_search_name = response['response']['list'][0]['userName']
self.assertEqual(exp_search_name, act_search_name)
5. 用例4-考试系统后台-用户管理-管理员-编辑
def test_011_admin_edit(self):
'''考试系统后台-用户管理-管理员-编辑'''
url = 'http://182.92.178.83:8088/api/admin/user/edit'
payload_admin_edit = {"id":admin_id,
"userUuid":"",
"userName":'胖白%d'%now_time,
"realName":"胖白",
"age":'3', "role":3,"sex":'2',
"birthDay":"2020-12-09 00:00:00",
"phone":'13412345567',
"lastActiveTime":"","createTime":"","modifyTime":"","status":1,
"userLevel":'null',
"imagePath":'null',
"password":"admin%d"%now_time}
admin_edit = requests.post(url,json=payload_admin_edit,headers=headers_admin ,timeout=30)
response = admin_edit.json()
# 断言
self.assertEqual('成功', response['message']) # 返回信息为成功
exp_userName = payload_admin_edit['userName']
act_userName = response['response']['userName']
self.assertEqual(exp_userName, act_userName) # 返回更改的用户名与实际更改的用户名相等
6. 用例5-考试系统后台-用户管理-管理员-更改状态
def test_012_change_status(self):
'''考试系统后台-用户管理-管理员-更改状态'''
#修改状态
url = 'http://182.92.178.83:8088/api/admin/user/changeStatus/'+str(admin_id)
change_status = requests.post(url,headers=headers_admin ,timeout=30)
#断言
response = change_status.json()
self.assertEqual('成功',response['message'] )
7. 用例6-考试系统后台-用户管理-管理员-删除
def test_013_admin_delete(self):
'''考试系统后台-用户管理-管理员-删除'''
#删除
url = 'http://182.92.178.83:8088/api/admin/user/delete/'+str(admin_id)
admin_delete = requests.post(url,headers=headers_admin ,timeout=30)
#断言
response = admin_delete.json()
self.assertEqual('成功', response['message'])
8. get_exam_cookie.py
import requests
def get_exam_cookie(): #获取考试系统后台的cookie
url = 'http://182.92.178.83:8088/api/user/login'
payload_login = {"userName": "admin", "password": "123456", "remember": False}
login = requests.post(url, json=payload_login, timeout=30)
dict_cookie = requests.utils.dict_from_cookiejar(login.cookies)
cookie = 'SESSION='+dict_cookie['SESSION']
return cookie
二、V部落
1. 导入类
import unittest
import requests
from public.get_vblog_cookie import get_vblog_cookie
import time
2. 用例1-V部落-登录
class MyTestCase(unittest.TestCase):
def test_001_login_vblog(self):
'''V部落-登录'''
url = 'http://182.92.178.83:8081/login'
payload = {'username': 'sang', 'password': '123'}
login = requests.post(url, data=payload,timeout=30)
exp = '登录成功'
act = login.json()['msg']
self.assertEqual(exp, act)
3. 用例2-V部落-发表文章
def test_002_new_article(self):
'''V部落-发表文章'''
# 新增
url1 = 'http://182.92.178.83:8081/article/'
global now_time
now_time = int(time.time())
payload = {'id': '-1', 'title': '咕噜咕噜%d' % now_time, 'mdContent': '春眠不觉晓',
'htmlContent': '', 'cid': '62','state': '1', 'dynamicTags': '猫'}
global vblog_headers
vblog_headers = {'Cookie': get_vblog_cookie()}
requests.post(url1, data=payload, headers=vblog_headers, timeout=30)
# 查询
url2 = 'http://182.92.178.83:8081/article/all'
result = requests.get(url2, headers=vblog_headers, timeout=30)
#断言
exp = payload['title']
act = '123'
for chaxun_info in result.json()['articles']:
if chaxun_info['title'] == exp:
global article_id
article_id = chaxun_info['id']
act = chaxun_info['title']
self.assertEqual(exp, act)
4. 用例3-V部落-文章列表-已发表-编辑
def test_003_edit_atrtical(self):
'''V部落-文章列表-已发表-编辑'''
# 编辑
url = 'http://182.92.178.83:8081/article/'
global payload_edit
payload_edit = {'id': article_id, 'title': '胖白%d' % now_time, 'mdContent': '处处闻啼鸟',
'htmlContent': '', 'cid': '62', 'state': '1', 'dynamicTags': '大白猫'}
requests.post(url, data=payload_edit, headers=vblog_headers)
# 查询
url2 = 'http://182.92.178.83:8081/article/all'
result = requests.get(url2, headers=vblog_headers, timeout=30)
#断言
exp = payload_edit['title']
act = '123'
for chaxun_info2 in result.json()['articles']:
if chaxun_info2['title'] == exp:
act = chaxun_info2['title']
self.assertEqual(exp, act)
5. 用例4-V部落-文章列表-已发表-删除
def test_004_delete_atrtical(self):
'''V部落-文章列表-已发表-删除'''
# 删除
url = 'http://182.92.178.83:8081/article/dustbin'
payload3 = {'aids': article_id, 'state': '1'}
requests.put(url, data=payload3, headers=vblog_headers, timeout=30)
# 查询
url2 = 'http://182.92.178.83:8081/admin/article/all?page=1&count=6&keywords='
result_delete = requests.get(url2, headers=vblog_headers, timeout=30)
#断言
act =True
delete_title = payload_edit['title']
for chaxun_info in result_delete.json()['articles']:
if chaxun_info['title'] == delete_title:
act=False
else:
continue
self.assertEqual(True, act)
6. 用例5-V部落-栏目管理-新增栏目
def test_005_new_catenames(self):
'''V部落-栏目管理-新增栏目'''
# 新增
url = 'http://182.92.178.83:8081/admin/category/'
payload = {'cateName': 'lxh%d' % now_time} # 将时间戳拼到新增栏目后面
requests.post(url, data=payload, headers=vblog_headers)
# 查询
url_chaxun = 'http://182.92.178.83:8081/admin/category/all'
chaxun = requests.get(url_chaxun, headers=vblog_headers)
# 断言
exp = payload['cateName'] # 预期输入值
act = '123' # 先随便赋给act一个值,如果在下面的列表中没有找到期望值,则act等于这个值
for catenameinfo in chaxun.json():
if catenameinfo['cateName'] == exp:
global cateName_id # 将cateName_id定义为全局变量
cateName_id = catenameinfo['id']
act = catenameinfo['cateName']
self.assertEqual(exp, act)
7. 用例6-V部落-栏目管理-编辑栏目
def test_006_edit_catename(self):
'''V部落-栏目管理-编辑栏目'''
#编辑
url = 'http://182.92.178.83:8081/admin/category/'
payload_edit_catename = {'id':cateName_id,'cateName':'啊啊啊啊%d'%now_time} #使用全局变量cateName_id
requests.put(url,data=payload_edit_catename,headers=vblog_headers) #使用全局变量vblog_headers
#查询
url_chaxun = 'http://182.92.178.83:8081/admin/category/all'
chaxun = requests.get(url_chaxun, headers=vblog_headers)
exp = payload_edit_catename['cateName'] #预期栏目
act = 'abc'
for catenameinfo_edit in chaxun.json():
if catenameinfo_edit['id']==cateName_id: #如果找到了编辑栏目的id,则将该栏目的cateName赋值给实际结果
act = catenameinfo_edit['cateName']
self.assertEqual(exp,act)
8. 用例7-V部落-栏目管理-删除栏目
def test_007_delete_catename(self):
'''V部落-栏目管理-删除栏目'''
#删除
url = 'http://182.92.178.83:8081/admin/category/'+str(cateName_id)
requests.delete(url,headers=vblog_headers)
#查询
url = 'http://182.92.178.83:8081/admin/category/all'
result_delete = requests.get(url,headers=vblog_headers)
#断言
act = True
delete_catename_id = cateName_id
for chaxun_info in result_delete.json():
if chaxun_info['id'] == delete_catename_id:
act = False
else:
continue
self.assertEqual(True, act)
9. get_vblog_cookie.py
import requests
def get_vblog_cookie(): #获取V部落cookie
url = 'http://182.92.178.83:8081/login'
payload = {'username': 'sang', 'password': '123'}
login = requests.post(url, data=payload)
cookies = login.cookies
dict_cookie = requests.utils.dict_from_cookiejar(cookies) # 从cookiejar里面返回一个字典
cookie = 'JSESSIONID=' + dict_cookie['JSESSIONID'] # 拿到需要的cookie信息,将其拼接起来
return cookie
三、testrunner.py
import unittest
import time
import os,sys
from report.HTMLTestRunner import HTMLTestRunner #HTMLTestRunner是一个开源的生成一个HTML报告的类
# 获取当前py文件路径地址,并进行路径分割(分割成目录路径和文件名称)
dirname,filename=os.path.split(os.path.abspath(sys.argv[0])) #分割TestRunner.py文件执行的绝对路径
#前一部分(路径)给dirname,后一部分(py文件名称)给filename
print(dirname,filename)
case_path = ".\\case\\" #定义case_path为当前路径下的case下的路径
result = dirname+"\\report\\" #定义result为当前路径下的report路径
def Creatsuite():
#定义单元测试容器
testunit = unittest.TestSuite() #初始化对象,TestSuite是测试套件
#定义搜索用例文件的方法,将所有py文件放到discover里面
discover = unittest.defaultTestLoader.discover(case_path, pattern='*.py', top_level_dir=None)
#将测试用例加入测试容器中
for test_suite in discover: #遍历所有discover的所有py文件
for casename in test_suite:
testunit.addTest(casename) #将py文件中的所有test方法加到测试套件中
#print testunit
return testunit
test_case = Creatsuite() #调用了上面的方法
#获取系统当前时间
now = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time())) #年月日时分秒
day = time.strftime('%Y-%m-%d', time.localtime(time.time())) #年月日
#定义个报告存放路径,支持相对路径
tdresult = result + day
if os.path.exists(tdresult): # 检验文件夹路径是否已经存在
filename = tdresult + "\\" + now + "_result.html" #定义报告名称,精确到秒
fp = open(filename, 'wb') #打开
#定义测试报告
runner = HTMLTestRunner(stream=fp, #以流的形式写
title='测试报告',
description='执行情况:')
#运行测试用例
runner.run(test_case)
fp.close() #关闭报告文件
else:
os.mkdir(tdresult) # 创建测试报告文件夹
filename = tdresult + "\\" + now + "_result.html"
fp = open(filename, 'wb')
#定义测试报告
runner = HTMLTestRunner(stream=fp,
title='Selenium测试报告',
description='执行情况:')
#运行测试用例
runner.run(test_case)
fp.close() #关闭报告文件